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