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