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