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