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