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