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