]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/QPeek.c
Add volatile qualifier to loop counters used to detect stalled tasks.
[freertos] / Demo / Common / Minimal / QPeek.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 /* 
52  * Tests the behaviour when data is peeked from a queue when there are
53  * multiple tasks blocked on the queue.
54  */
55
56
57 #include <stdlib.h>
58
59 /* Scheduler include files. */
60 #include "FreeRTOS.h"
61 #include "task.h"
62 #include "queue.h"
63 #include "semphr.h"
64
65 /* Demo program include files. */
66 #include "QPeek.h"
67
68 #define qpeekQUEUE_LENGTH               ( 5 )
69 #define qpeekNO_BLOCK                   ( 0 )
70 #define qpeekSHORT_DELAY                ( 10 )
71
72 #define qpeekLOW_PRIORITY                       ( tskIDLE_PRIORITY + 0 )
73 #define qpeekMEDIUM_PRIORITY            ( tskIDLE_PRIORITY + 1 )
74 #define qpeekHIGH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )
75 #define qpeekHIGHEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )
76
77 /*-----------------------------------------------------------*/
78
79 /*
80  * The following three tasks are used to demonstrate the peeking behaviour.
81  * Each task is given a different priority to demonstrate the order in which
82  * tasks are woken as data is peeked from a queue.
83  */
84 static void prvLowPriorityPeekTask( void *pvParameters );
85 static void prvMediumPriorityPeekTask( void *pvParameters );
86 static void prvHighPriorityPeekTask( void *pvParameters );
87 static void prvHighestPriorityPeekTask( void *pvParameters );
88
89 /*-----------------------------------------------------------*/
90
91 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
92 detected in any of the tasks. */
93 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
94
95 /* Counter that is incremented on each cycle of a test.  This is used to
96 detect a stalled task - a test that is no longer running. */
97 static volatile unsigned portLONG ulLoopCounter = 0;
98
99 /* Handles to the test tasks. */
100 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
101 /*-----------------------------------------------------------*/
102
103 void vStartQueuePeekTasks( void )
104 {
105 xQueueHandle xQueue;
106
107         /* Create the queue that we are going to use for the test/demo. */
108         xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
109
110         /* Create the demo tasks and pass it the queue just created.  We are
111         passing the queue handle by value so it does not matter that it is declared
112         on the stack here. */
113         xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
114         xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
115         xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
116         xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
117 }
118 /*-----------------------------------------------------------*/
119
120 static void prvHighestPriorityPeekTask( void *pvParameters )
121 {
122 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
123 unsigned portLONG ulValue;
124
125         #ifdef USE_STDIO
126         {
127                 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
128         
129                 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
130
131                 /* Queue a message for printing to say the task has started. */
132                 vPrintDisplayMessage( &pcTaskStartMsg );
133         }
134         #endif
135
136         for( ;; )
137         {
138                 /* Try peeking from the queue.  The queue should be empty so we will
139                 block, allowing the high priority task to execute. */
140                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
141                 {
142                         /* We expected to have received something by the time we unblock. */
143                         xErrorDetected = pdTRUE;
144                 }
145
146                 /* When we reach here the high and medium priority tasks should still
147                 be blocked on the queue.  We unblocked because the low priority task
148                 wrote a value to the queue, which we should have peeked.  Peeking the
149                 data (rather than receiving it) will leave the data on the queue, so
150                 the high priority task should then have also been unblocked, but not
151                 yet executed. */
152                 if( ulValue != 0x11223344 )
153                 {
154                         /* We did not receive the expected value. */
155                         xErrorDetected = pdTRUE;
156                 }
157
158                 if( uxQueueMessagesWaiting( xQueue ) != 1 )
159                 {
160                         /* The message should have been left on the queue. */
161                         xErrorDetected = pdTRUE;
162                 }
163
164                 /* Now we are going to actually receive the data, so when the high
165                 priority task runs it will find the queue empty and return to the
166                 blocked state. */
167                 ulValue = 0;
168                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
169                 {
170                         /* We expected to receive the value. */
171                         xErrorDetected = pdTRUE;
172                 }
173
174                 if( ulValue != 0x11223344 )
175                 {
176                         /* We did not receive the expected value - which should have been
177                         the same value as was peeked. */
178                         xErrorDetected = pdTRUE;
179                 }
180
181                 /* Now we will block again as the queue is once more empty.  The low 
182                 priority task can then execute again. */
183                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
184                 {
185                         /* We expected to have received something by the time we unblock. */
186                         xErrorDetected = pdTRUE;
187                 }
188
189                 /* When we get here the low priority task should have again written to the
190                 queue. */
191                 if( ulValue != 0x01234567 )
192                 {
193                         /* We did not receive the expected value. */
194                         xErrorDetected = pdTRUE;
195                 }
196
197                 if( uxQueueMessagesWaiting( xQueue ) != 1 )
198                 {
199                         /* The message should have been left on the queue. */
200                         xErrorDetected = pdTRUE;
201                 }
202
203                 /* We only peeked the data, so suspending ourselves now should enable
204                 the high priority task to also peek the data.  The high priority task
205                 will have been unblocked when we peeked the data as we left the data
206                 in the queue. */
207                 vTaskSuspend( NULL );
208
209
210
211                 /* This time we are going to do the same as the above test, but the
212                 high priority task is going to receive the data, rather than peek it.
213                 This means that the medium priority task should never peek the value. */
214                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
215                 {
216                         xErrorDetected = pdTRUE;
217                 }
218
219                 if( ulValue != 0xaabbaabb )
220                 {
221                         xErrorDetected = pdTRUE;
222                 }
223
224                 vTaskSuspend( NULL );           
225         }
226 }
227 /*-----------------------------------------------------------*/
228
229 static void prvHighPriorityPeekTask( void *pvParameters )
230 {
231 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
232 unsigned portLONG ulValue;
233
234         for( ;; )
235         {
236                 /* Try peeking from the queue.  The queue should be empty so we will
237                 block, allowing the medium priority task to execute.  Both the high
238                 and highest priority tasks will then be blocked on the queue. */
239                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
240                 {
241                         /* We expected to have received something by the time we unblock. */
242                         xErrorDetected = pdTRUE;
243                 }
244
245                 /* When we get here the highest priority task should have peeked the data
246                 (unblocking this task) then suspended (allowing this task to also peek
247                 the data). */
248                 if( ulValue != 0x01234567 )
249                 {
250                         /* We did not receive the expected value. */
251                         xErrorDetected = pdTRUE;
252                 }
253
254                 if( uxQueueMessagesWaiting( xQueue ) != 1 )
255                 {
256                         /* The message should have been left on the queue. */
257                         xErrorDetected = pdTRUE;
258                 }
259
260                 /* We only peeked the data, so suspending ourselves now should enable
261                 the medium priority task to also peek the data.  The medium priority task
262                 will have been unblocked when we peeked the data as we left the data
263                 in the queue. */
264                 vTaskSuspend( NULL );
265
266
267                 /* This time we are going actually receive the value, so the medium
268                 priority task will never peek the data - we removed it from the queue. */
269                 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
270                 {
271                         xErrorDetected = pdTRUE;
272                 }
273
274                 if( ulValue != 0xaabbaabb )
275                 {
276                         xErrorDetected = pdTRUE;
277                 }
278
279                 vTaskSuspend( NULL );                           
280         }
281 }
282 /*-----------------------------------------------------------*/
283
284 static void prvMediumPriorityPeekTask( void *pvParameters )
285 {
286 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
287 unsigned portLONG ulValue;
288
289         for( ;; )
290         {
291                 /* Try peeking from the queue.  The queue should be empty so we will
292                 block, allowing the low priority task to execute.  The highest, high
293                 and medium priority tasks will then all be blocked on the queue. */
294                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
295                 {
296                         /* We expected to have received something by the time we unblock. */
297                         xErrorDetected = pdTRUE;
298                 }
299
300                 /* When we get here the high priority task should have peeked the data
301                 (unblocking this task) then suspended (allowing this task to also peek
302                 the data). */
303                 if( ulValue != 0x01234567 )
304                 {
305                         /* We did not receive the expected value. */
306                         xErrorDetected = pdTRUE;
307                 }
308
309                 if( uxQueueMessagesWaiting( xQueue ) != 1 )
310                 {
311                         /* The message should have been left on the queue. */
312                         xErrorDetected = pdTRUE;
313                 }
314
315                 /* Just so we know the test is still running. */
316                 ulLoopCounter++;
317
318                 /* Now we can suspend ourselves so the low priority task can execute
319                 again. */
320                 vTaskSuspend( NULL );
321         }
322 }
323 /*-----------------------------------------------------------*/
324
325 static void prvLowPriorityPeekTask( void *pvParameters )
326 {
327 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
328 unsigned portLONG ulValue;
329
330         for( ;; )
331         {
332                 /* Write some data to the queue.  This should unblock the highest 
333                 priority task that is waiting to peek data from the queue. */
334                 ulValue = 0x11223344;
335                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
336                 {
337                         /* We were expecting the queue to be empty so we should not of
338                         had a problem writing to the queue. */
339                         xErrorDetected = pdTRUE;
340                 }
341
342                 /* By the time we get here the data should have been removed from
343                 the queue. */
344                 if( uxQueueMessagesWaiting( xQueue ) != 0 )
345                 {
346                         xErrorDetected = pdTRUE;
347                 }
348
349                 /* Write another value to the queue, again waking the highest priority
350                 task that is blocked on the queue. */
351                 ulValue = 0x01234567;
352                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
353                 {
354                         /* We were expecting the queue to be empty so we should not of
355                         had a problem writing to the queue. */
356                         xErrorDetected = pdTRUE;
357                 }
358
359                 /* All the other tasks should now have successfully peeked the data.
360                 The data is still in the queue so we should be able to receive it. */
361                 ulValue = 0;
362                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
363                 {
364                         /* We expected to receive the data. */
365                         xErrorDetected = pdTRUE;
366                 }
367
368                 if( ulValue != 0x01234567 )
369                 {
370                         /* We did not receive the expected value. */
371                 }
372                 
373                 /* Lets just delay a while as this is an intensive test as we don't
374                 want to starve other tests of processing time. */
375                 vTaskDelay( qpeekSHORT_DELAY );
376
377                 /* Unsuspend the other tasks so we can repeat the test - this time
378                 however not all the other tasks will peek the data as the high
379                 priority task is actually going to remove it from the queue.  Send
380                 to front is used just to be different.  As the queue is empty it
381                 makes no difference to the result. */
382                 vTaskResume( xMediumPriorityTask );
383                 vTaskResume( xHighPriorityTask );
384                 vTaskResume( xHighestPriorityTask );
385
386                 ulValue = 0xaabbaabb;
387                 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
388                 {
389                         /* We were expecting the queue to be empty so we should not of
390                         had a problem writing to the queue. */
391                         xErrorDetected = pdTRUE;
392                 }
393
394                 /* This time we should find that the queue is empty.  The high priority
395                 task actually removed the data rather than just peeking it. */
396                 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
397                 {
398                         /* We expected to receive the data. */
399                         xErrorDetected = pdTRUE;
400                 }
401
402                 /* Unsuspend the highest and high priority tasks so we can go back
403                 and repeat the whole thing.  The medium priority task should not be
404                 suspended as it was not able to peek the data in this last case. */
405                 vTaskResume( xHighPriorityTask );
406                 vTaskResume( xHighestPriorityTask );            
407
408                 /* Lets just delay a while as this is an intensive test as we don't
409                 want to starve other tests of processing time. */
410                 vTaskDelay( qpeekSHORT_DELAY );
411         }
412 }
413 /*-----------------------------------------------------------*/
414
415 /* This is called to check that all the created tasks are still running. */
416 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
417 {
418 static unsigned portLONG ulLastLoopCounter = 0;
419
420         /* If the demo task is still running then we expect the loopcounter to
421         have incremented since this function was last called. */
422         if( ulLastLoopCounter == ulLoopCounter )
423         {
424                 xErrorDetected = pdTRUE;
425         }
426
427         ulLastLoopCounter = ulLoopCounter;
428
429         /* Errors detected in the task itself will have latched xErrorDetected
430         to true. */
431
432         return !xErrorDetected;
433 }
434