]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/AltBlock.c
Add volatile qualifier to loop counters used to detect stalled tasks.
[freertos] / Demo / Common / Minimal / AltBlock.c
1 /*\r
2         FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /*\r
51  * This is a version of BlockTim.c that uses the light weight API.\r
52  *\r
53  * This file contains some test scenarios that ensure tasks do not exit queue\r
54  * send or receive functions prematurely.  A description of the tests is\r
55  * included within the code.\r
56  */\r
57 \r
58 /* Kernel includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "queue.h"\r
62 \r
63 /* Demo includes. */\r
64 #include "AltBlock.h"\r
65 \r
66 /* Task priorities. */\r
67 #define bktPRIMARY_PRIORITY                     ( 3 )\r
68 #define bktSECONDARY_PRIORITY           ( 2 )\r
69 \r
70 /* Task behaviour. */\r
71 #define bktQUEUE_LENGTH                         ( 5 )\r
72 #define bktSHORT_WAIT                           ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )\r
73 #define bktPRIMARY_BLOCK_TIME           ( 10 )\r
74 #define bktALLOWABLE_MARGIN                     ( 12 )\r
75 #define bktTIME_TO_BLOCK                        ( 175 )\r
76 #define bktDONT_BLOCK                           ( ( portTickType ) 0 )\r
77 #define bktRUN_INDICATOR                        ( ( unsigned portBASE_TYPE ) 0x55 )\r
78 \r
79 /* The queue on which the tasks block. */\r
80 static xQueueHandle xTestQueue;\r
81 \r
82 /* Handle to the secondary task is required by the primary task for calls\r
83 to vTaskSuspend/Resume(). */\r
84 static xTaskHandle xSecondary;\r
85 \r
86 /* Used to ensure that tasks are still executing without error. */\r
87 static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;\r
88 static portBASE_TYPE xErrorOccurred = pdFALSE;\r
89 \r
90 /* Provides a simple mechanism for the primary task to know when the\r
91 secondary task has executed. */\r
92 static volatile unsigned portBASE_TYPE xRunIndicator;\r
93 \r
94 /* The two test tasks.  Their behaviour is commented within the files. */\r
95 static void vPrimaryBlockTimeTestTask( void *pvParameters );\r
96 static void vSecondaryBlockTimeTestTask( void *pvParameters );\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void vCreateAltBlockTimeTasks( void )\r
101 {\r
102         /* Create the queue on which the two tasks block. */\r
103     xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );\r
104 \r
105         /* Create the two test tasks. */\r
106         xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"FBTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );\r
107         xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"FBTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );\r
108 }\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 static void vPrimaryBlockTimeTestTask( void *pvParameters )\r
112 {\r
113 portBASE_TYPE xItem, xData;\r
114 portTickType xTimeWhenBlocking;\r
115 portTickType xTimeToBlock, xBlockedTime;\r
116 \r
117         #ifdef USE_STDIO\r
118         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
119         \r
120                 const portCHAR * const pcTaskStartMsg = "Alt primary block time test started.\r\n";\r
121 \r
122                 /* Queue a message for printing to say the task has started. */\r
123                 vPrintDisplayMessage( &pcTaskStartMsg );\r
124         #endif\r
125 \r
126         ( void ) pvParameters;\r
127 \r
128         for( ;; )\r
129         {\r
130                 /*********************************************************************\r
131         Test 1\r
132 \r
133         Simple block time wakeup test on queue receives. */\r
134                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
135                 {\r
136                         /* The queue is empty. Attempt to read from the queue using a block\r
137                         time.  When we wake, ensure the delta in time is as expected. */\r
138                         xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;\r
139 \r
140                         /* A critical section is used to minimise the jitter in the time\r
141                         measurements. */\r
142                         portENTER_CRITICAL();\r
143                         {\r
144                                 xTimeWhenBlocking = xTaskGetTickCount();\r
145                                 \r
146                                 /* We should unblock after xTimeToBlock having not received\r
147                                 anything on the queue. */\r
148                                 if( xQueueAltReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )\r
149                                 {\r
150                                         xErrorOccurred = pdTRUE;\r
151                                 }\r
152 \r
153                                 /* How long were we blocked for? */\r
154                                 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
155                         }\r
156                         portEXIT_CRITICAL();\r
157 \r
158                         if( xBlockedTime < xTimeToBlock )\r
159                         {\r
160                                 /* Should not have blocked for less than we requested. */\r
161                                 xErrorOccurred = pdTRUE;\r
162                         }\r
163 \r
164                         if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )\r
165                         {\r
166                                 /* Should not have blocked for longer than we requested,\r
167                                 although we would not necessarily run as soon as we were\r
168                                 unblocked so a margin is allowed. */\r
169                                 xErrorOccurred = pdTRUE;\r
170                         }\r
171                 }\r
172 \r
173 \r
174                 #if configUSE_PREEMPTION == 0\r
175                         taskYIELD();\r
176                 #endif\r
177 \r
178 \r
179                 /*********************************************************************\r
180         Test 2\r
181 \r
182         Simple block time wakeup test on queue sends.\r
183 \r
184                 First fill the queue.  It should be empty so all sends should pass. */\r
185                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
186                 {\r
187                         if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
188                         {\r
189                                 xErrorOccurred = pdTRUE;\r
190                         }\r
191                 }\r
192 \r
193                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
194                 {\r
195                         /* The queue is full. Attempt to write to the queue using a block\r
196                         time.  When we wake, ensure the delta in time is as expected. */\r
197                         xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;\r
198 \r
199                         portENTER_CRITICAL();\r
200                         {\r
201                                 xTimeWhenBlocking = xTaskGetTickCount();\r
202                                 \r
203                                 /* We should unblock after xTimeToBlock having not received\r
204                                 anything on the queue. */\r
205                                 if( xQueueAltSendToBack( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )\r
206                                 {\r
207                                         xErrorOccurred = pdTRUE;\r
208                                 }\r
209 \r
210                                 /* How long were we blocked for? */\r
211                                 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
212                         }\r
213                         portEXIT_CRITICAL();\r
214 \r
215                         if( xBlockedTime < xTimeToBlock )\r
216                         {\r
217                                 /* Should not have blocked for less than we requested. */\r
218                                 xErrorOccurred = pdTRUE;\r
219                         }\r
220 \r
221                         if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )\r
222                         {\r
223                                 /* Should not have blocked for longer than we requested,\r
224                                 although we would not necessarily run as soon as we were\r
225                                 unblocked so a margin is allowed. */\r
226                                 xErrorOccurred = pdTRUE;\r
227                         }\r
228                 }\r
229 \r
230                 #if configUSE_PREEMPTION == 0\r
231                         taskYIELD();\r
232                 #endif\r
233 \r
234                 \r
235                 /*********************************************************************\r
236         Test 3\r
237 \r
238                 Wake the other task, it will block attempting to post to the queue.\r
239                 When we read from the queue the other task will wake, but before it\r
240                 can run we will post to the queue again.  When the other task runs it\r
241                 will find the queue still full, even though it was woken.  It should\r
242                 recognise that its block time has not expired and return to block for\r
243                 the remains of its block time.\r
244 \r
245                 Wake the other task so it blocks attempting to post to the already\r
246                 full queue. */\r
247                 xRunIndicator = 0;\r
248                 vTaskResume( xSecondary );\r
249 \r
250                 /* We need to wait a little to ensure the other task executes. */\r
251                 while( xRunIndicator != bktRUN_INDICATOR )\r
252                 {\r
253                         /* The other task has not yet executed. */\r
254                         vTaskDelay( bktSHORT_WAIT );\r
255                 }\r
256                 /* Make sure the other task is blocked on the queue. */\r
257                 vTaskDelay( bktSHORT_WAIT );\r
258                 xRunIndicator = 0;\r
259 \r
260                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
261                 {\r
262                         /* Now when we make space on the queue the other task should wake\r
263                         but not execute as this task has higher priority. */                            \r
264                         if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
265                         {\r
266                                 xErrorOccurred = pdTRUE;\r
267                         }\r
268 \r
269                         /* Now fill the queue again before the other task gets a chance to\r
270                         execute.  If the other task had executed we would find the queue\r
271                         full ourselves, and the other task have set xRunIndicator. */\r
272                         if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
273                         {\r
274                                 xErrorOccurred = pdTRUE;\r
275                         }\r
276 \r
277                         if( xRunIndicator == bktRUN_INDICATOR )\r
278                         {\r
279                                 /* The other task should not have executed. */\r
280                                 xErrorOccurred = pdTRUE;\r
281                         }\r
282 \r
283                         /* Raise the priority of the other task so it executes and blocks\r
284                         on the queue again. */\r
285                         vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );\r
286 \r
287                         /* The other task should now have re-blocked without exiting the\r
288                         queue function. */\r
289                         if( xRunIndicator == bktRUN_INDICATOR )\r
290                         {\r
291                                 /* The other task should not have executed outside of the\r
292                                 queue function. */\r
293                                 xErrorOccurred = pdTRUE;\r
294                         }\r
295 \r
296                         /* Set the priority back down. */\r
297                         vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );                  \r
298                 }\r
299 \r
300                 /* Let the other task timeout.  When it unblockes it will check that it\r
301                 unblocked at the correct time, then suspend itself. */\r
302                 while( xRunIndicator != bktRUN_INDICATOR )\r
303                 {\r
304                         vTaskDelay( bktSHORT_WAIT );\r
305                 }\r
306                 vTaskDelay( bktSHORT_WAIT );\r
307                 xRunIndicator = 0;\r
308 \r
309                 #if configUSE_PREEMPTION == 0\r
310                         taskYIELD();\r
311                 #endif\r
312 \r
313                 /*********************************************************************\r
314         Test 4\r
315 \r
316                 As per test 3 - but with the send and receive the other way around.\r
317                 The other task blocks attempting to read from the queue.\r
318 \r
319                 Empty the queue.  We should find that it is full. */\r
320                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
321                 {\r
322                         if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
323                         {\r
324                                 xErrorOccurred = pdTRUE;\r
325                         }\r
326                 }\r
327                 \r
328                 /* Wake the other task so it blocks attempting to read from  the\r
329                 already empty queue. */\r
330                 vTaskResume( xSecondary );\r
331 \r
332                 /* We need to wait a little to ensure the other task executes. */\r
333                 while( xRunIndicator != bktRUN_INDICATOR )\r
334                 {\r
335                         vTaskDelay( bktSHORT_WAIT );\r
336                 }\r
337                 vTaskDelay( bktSHORT_WAIT );\r
338                 xRunIndicator = 0;\r
339 \r
340                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
341                 {\r
342                         /* Now when we place an item on the queue the other task should\r
343                         wake but not execute as this task has higher priority. */                               \r
344                         if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
345                         {\r
346                                 xErrorOccurred = pdTRUE;\r
347                         }\r
348 \r
349                         /* Now empty the queue again before the other task gets a chance to\r
350                         execute.  If the other task had executed we would find the queue\r
351                         empty ourselves, and the other task would be suspended. */\r
352                         if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
353                         {\r
354                                 xErrorOccurred = pdTRUE;\r
355                         }\r
356 \r
357                         if( xRunIndicator == bktRUN_INDICATOR )\r
358                         {\r
359                                 /* The other task should not have executed. */\r
360                                 xErrorOccurred = pdTRUE;\r
361                         }\r
362 \r
363                         /* Raise the priority of the other task so it executes and blocks\r
364                         on the queue again. */\r
365                         vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );\r
366 \r
367                         /* The other task should now have re-blocked without exiting the\r
368                         queue function. */\r
369                         if( xRunIndicator == bktRUN_INDICATOR )\r
370                         {\r
371                                 /* The other task should not have executed outside of the\r
372                                 queue function. */\r
373                                 xErrorOccurred = pdTRUE;\r
374                         }\r
375                         vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );                  \r
376                 }\r
377 \r
378                 /* Let the other task timeout.  When it unblockes it will check that it\r
379                 unblocked at the correct time, then suspend itself. */\r
380                 while( xRunIndicator != bktRUN_INDICATOR )\r
381                 {\r
382                         vTaskDelay( bktSHORT_WAIT );\r
383                 }\r
384                 vTaskDelay( bktSHORT_WAIT );\r
385 \r
386                 xPrimaryCycles++;\r
387         }\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 static void vSecondaryBlockTimeTestTask( void *pvParameters )\r
392 {\r
393 portTickType xTimeWhenBlocking, xBlockedTime;\r
394 portBASE_TYPE xData;\r
395 \r
396         #ifdef USE_STDIO\r
397         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
398         \r
399                 const portCHAR * const pcTaskStartMsg = "Alt secondary block time test started.\r\n";\r
400 \r
401                 /* Queue a message for printing to say the task has started. */\r
402                 vPrintDisplayMessage( &pcTaskStartMsg );\r
403         #endif\r
404 \r
405         ( void ) pvParameters;\r
406 \r
407         for( ;; )\r
408         {\r
409                 /*********************************************************************\r
410         Test 1 and 2\r
411 \r
412                 This task does does not participate in these tests. */\r
413                 vTaskSuspend( NULL );\r
414 \r
415                 /*********************************************************************\r
416         Test 3\r
417 \r
418                 The first thing we do is attempt to read from the queue.  It should be\r
419                 full so we block.  Note the time before we block so we can check the\r
420                 wake time is as per that expected. */\r
421                 portENTER_CRITICAL();\r
422                 {\r
423                         xTimeWhenBlocking = xTaskGetTickCount();\r
424                         \r
425                         /* We should unblock after bktTIME_TO_BLOCK having not received\r
426                         anything on the queue. */\r
427                         xData = 0;\r
428                         xRunIndicator = bktRUN_INDICATOR;\r
429                         if( xQueueAltSendToBack( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )\r
430                         {\r
431                                 xErrorOccurred = pdTRUE;\r
432                         }\r
433 \r
434                         /* How long were we inside the send function? */\r
435                         xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
436                 }\r
437                 portEXIT_CRITICAL();\r
438 \r
439                 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */\r
440                 if( xBlockedTime < bktTIME_TO_BLOCK )\r
441                 {\r
442                         xErrorOccurred = pdTRUE;\r
443                 }\r
444 \r
445                 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN\r
446                 either.  A margin is permitted as we would not necessarily run as\r
447                 soon as we unblocked. */\r
448                 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )\r
449                 {\r
450                         xErrorOccurred = pdTRUE;\r
451                 }\r
452 \r
453                 /* Suspend ready for test 3. */\r
454                 xRunIndicator = bktRUN_INDICATOR;\r
455                 vTaskSuspend( NULL );\r
456 \r
457                 /*********************************************************************\r
458         Test 4\r
459 \r
460                 As per test three, but with the send and receive reversed. */\r
461                 portENTER_CRITICAL();\r
462                 {\r
463                         xTimeWhenBlocking = xTaskGetTickCount();\r
464                         \r
465                         /* We should unblock after bktTIME_TO_BLOCK having not received\r
466                         anything on the queue. */\r
467                         xRunIndicator = bktRUN_INDICATOR;\r
468                         if( xQueueAltReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )\r
469                         {\r
470                                 xErrorOccurred = pdTRUE;\r
471                         }\r
472 \r
473                         xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
474                 }\r
475                 portEXIT_CRITICAL();\r
476 \r
477                 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */\r
478                 if( xBlockedTime < bktTIME_TO_BLOCK )\r
479                 {\r
480                         xErrorOccurred = pdTRUE;\r
481                 }\r
482 \r
483                 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN\r
484                 either.  A margin is permitted as we would not necessarily run as soon\r
485                 as we unblocked. */\r
486                 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )\r
487                 {\r
488                         xErrorOccurred = pdTRUE;\r
489                 }\r
490 \r
491                 xRunIndicator = bktRUN_INDICATOR;\r
492 \r
493                 xSecondaryCycles++;\r
494         }\r
495 }\r
496 /*-----------------------------------------------------------*/\r
497 \r
498 portBASE_TYPE xAreAltBlockTimeTestTasksStillRunning( void )\r
499 {\r
500 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;\r
501 portBASE_TYPE xReturn = pdPASS;\r
502 \r
503         /* Have both tasks performed at least one cycle since this function was\r
504         last called? */\r
505         if( xPrimaryCycles == xLastPrimaryCycleCount )\r
506         {\r
507                 xReturn = pdFAIL;\r
508         }\r
509 \r
510         if( xSecondaryCycles == xLastSecondaryCycleCount )\r
511         {\r
512                 xReturn = pdFAIL;\r
513         }\r
514 \r
515         if( xErrorOccurred == pdTRUE )\r
516         {\r
517                 xReturn = pdFAIL;\r
518         }\r
519 \r
520         xLastSecondaryCycleCount = xSecondaryCycles;\r
521         xLastPrimaryCycleCount = xPrimaryCycles;\r
522 \r
523         return xReturn;\r
524 }\r