]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QPeek.c
aa473e5cfe6f305db81f82cb8ab009d4d144e110
[freertos] / FreeRTOS / Demo / Common / Minimal / QPeek.c
1 /*\r
2     FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /*\r
68  * Tests the behaviour when data is peeked from a queue when there are\r
69  * multiple tasks blocked on the queue.\r
70  */\r
71 \r
72 \r
73 #include <stdlib.h>\r
74 \r
75 /* Scheduler include files. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 #include "queue.h"\r
79 #include "semphr.h"\r
80 \r
81 /* Demo program include files. */\r
82 #include "QPeek.h"\r
83 \r
84 #define qpeekQUEUE_LENGTH               ( 5 )\r
85 #define qpeekNO_BLOCK                   ( 0 )\r
86 #define qpeekSHORT_DELAY                ( 10 )\r
87 \r
88 #define qpeekLOW_PRIORITY                       ( tskIDLE_PRIORITY + 0 )\r
89 #define qpeekMEDIUM_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
90 #define qpeekHIGH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
91 #define qpeekHIGHEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /*\r
96  * The following three tasks are used to demonstrate the peeking behaviour.\r
97  * Each task is given a different priority to demonstrate the order in which\r
98  * tasks are woken as data is peeked from a queue.\r
99  */\r
100 static void prvLowPriorityPeekTask( void *pvParameters );\r
101 static void prvMediumPriorityPeekTask( void *pvParameters );\r
102 static void prvHighPriorityPeekTask( void *pvParameters );\r
103 static void prvHighestPriorityPeekTask( void *pvParameters );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
108 detected in any of the tasks. */\r
109 static volatile portBASE_TYPE xErrorDetected = pdFALSE;\r
110 \r
111 /* Counter that is incremented on each cycle of a test.  This is used to\r
112 detect a stalled task - a test that is no longer running. */\r
113 static volatile unsigned long ulLoopCounter = 0;\r
114 \r
115 /* Handles to the test tasks. */\r
116 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vStartQueuePeekTasks( void )\r
120 {\r
121 QueueHandle_t xQueue;\r
122 \r
123         /* Create the queue that we are going to use for the test/demo. */\r
124         xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned long ) );\r
125 \r
126         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
127         in use.  The queue registry is provided as a means for kernel aware\r
128         debuggers to locate queues and has no purpose if a kernel aware debugger\r
129         is not being used.  The call to vQueueAddToRegistry() will be removed\r
130         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
131         defined to be less than 1. */\r
132         vQueueAddToRegistry( xQueue, "QPeek_Test_Queue" );\r
133 \r
134         /* Create the demo tasks and pass it the queue just created.  We are\r
135         passing the queue handle by value so it does not matter that it is declared\r
136         on the stack here. */\r
137         xTaskCreate( prvLowPriorityPeekTask, "PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );\r
138         xTaskCreate( prvMediumPriorityPeekTask, "PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );\r
139         xTaskCreate( prvHighPriorityPeekTask, "PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );\r
140         xTaskCreate( prvHighestPriorityPeekTask, "PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 static void prvHighestPriorityPeekTask( void *pvParameters )\r
145 {\r
146 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
147 unsigned long ulValue;\r
148 \r
149         #ifdef USE_STDIO\r
150         {\r
151                 void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
152 \r
153                 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";\r
154 \r
155                 /* Queue a message for printing to say the task has started. */\r
156                 vPrintDisplayMessage( &pcTaskStartMsg );\r
157         }\r
158         #endif\r
159 \r
160         for( ;; )\r
161         {\r
162                 /* Try peeking from the queue.  The queue should be empty so we will\r
163                 block, allowing the high priority task to execute. */\r
164                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
165                 {\r
166                         /* We expected to have received something by the time we unblock. */\r
167                         xErrorDetected = pdTRUE;\r
168                 }\r
169 \r
170                 /* When we reach here the high and medium priority tasks should still\r
171                 be blocked on the queue.  We unblocked because the low priority task\r
172                 wrote a value to the queue, which we should have peeked.  Peeking the\r
173                 data (rather than receiving it) will leave the data on the queue, so\r
174                 the high priority task should then have also been unblocked, but not\r
175                 yet executed. */\r
176                 if( ulValue != 0x11223344 )\r
177                 {\r
178                         /* We did not receive the expected value. */\r
179                         xErrorDetected = pdTRUE;\r
180                 }\r
181 \r
182                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
183                 {\r
184                         /* The message should have been left on the queue. */\r
185                         xErrorDetected = pdTRUE;\r
186                 }\r
187 \r
188                 /* Now we are going to actually receive the data, so when the high\r
189                 priority task runs it will find the queue empty and return to the\r
190                 blocked state. */\r
191                 ulValue = 0;\r
192                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
193                 {\r
194                         /* We expected to receive the value. */\r
195                         xErrorDetected = pdTRUE;\r
196                 }\r
197 \r
198                 if( ulValue != 0x11223344 )\r
199                 {\r
200                         /* We did not receive the expected value - which should have been\r
201                         the same value as was peeked. */\r
202                         xErrorDetected = pdTRUE;\r
203                 }\r
204 \r
205                 /* Now we will block again as the queue is once more empty.  The low\r
206                 priority task can then execute again. */\r
207                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
208                 {\r
209                         /* We expected to have received something by the time we unblock. */\r
210                         xErrorDetected = pdTRUE;\r
211                 }\r
212 \r
213                 /* When we get here the low priority task should have again written to the\r
214                 queue. */\r
215                 if( ulValue != 0x01234567 )\r
216                 {\r
217                         /* We did not receive the expected value. */\r
218                         xErrorDetected = pdTRUE;\r
219                 }\r
220 \r
221                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
222                 {\r
223                         /* The message should have been left on the queue. */\r
224                         xErrorDetected = pdTRUE;\r
225                 }\r
226 \r
227                 /* We only peeked the data, so suspending ourselves now should enable\r
228                 the high priority task to also peek the data.  The high priority task\r
229                 will have been unblocked when we peeked the data as we left the data\r
230                 in the queue. */\r
231                 vTaskSuspend( NULL );\r
232 \r
233 \r
234 \r
235                 /* This time we are going to do the same as the above test, but the\r
236                 high priority task is going to receive the data, rather than peek it.\r
237                 This means that the medium priority task should never peek the value. */\r
238                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
239                 {\r
240                         xErrorDetected = pdTRUE;\r
241                 }\r
242 \r
243                 if( ulValue != 0xaabbaabb )\r
244                 {\r
245                         xErrorDetected = pdTRUE;\r
246                 }\r
247 \r
248                 vTaskSuspend( NULL );\r
249         }\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvHighPriorityPeekTask( void *pvParameters )\r
254 {\r
255 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
256 unsigned long ulValue;\r
257 \r
258         for( ;; )\r
259         {\r
260                 /* Try peeking from the queue.  The queue should be empty so we will\r
261                 block, allowing the medium priority task to execute.  Both the high\r
262                 and highest priority tasks will then be blocked on the queue. */\r
263                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
264                 {\r
265                         /* We expected to have received something by the time we unblock. */\r
266                         xErrorDetected = pdTRUE;\r
267                 }\r
268 \r
269                 /* When we get here the highest priority task should have peeked the data\r
270                 (unblocking this task) then suspended (allowing this task to also peek\r
271                 the data). */\r
272                 if( ulValue != 0x01234567 )\r
273                 {\r
274                         /* We did not receive the expected value. */\r
275                         xErrorDetected = pdTRUE;\r
276                 }\r
277 \r
278                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
279                 {\r
280                         /* The message should have been left on the queue. */\r
281                         xErrorDetected = pdTRUE;\r
282                 }\r
283 \r
284                 /* We only peeked the data, so suspending ourselves now should enable\r
285                 the medium priority task to also peek the data.  The medium priority task\r
286                 will have been unblocked when we peeked the data as we left the data\r
287                 in the queue. */\r
288                 vTaskSuspend( NULL );\r
289 \r
290 \r
291                 /* This time we are going actually receive the value, so the medium\r
292                 priority task will never peek the data - we removed it from the queue. */\r
293                 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
294                 {\r
295                         xErrorDetected = pdTRUE;\r
296                 }\r
297 \r
298                 if( ulValue != 0xaabbaabb )\r
299                 {\r
300                         xErrorDetected = pdTRUE;\r
301                 }\r
302 \r
303                 vTaskSuspend( NULL );\r
304         }\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 static void prvMediumPriorityPeekTask( void *pvParameters )\r
309 {\r
310 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
311 unsigned long ulValue;\r
312 \r
313         for( ;; )\r
314         {\r
315                 /* Try peeking from the queue.  The queue should be empty so we will\r
316                 block, allowing the low priority task to execute.  The highest, high\r
317                 and medium priority tasks will then all be blocked on the queue. */\r
318                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
319                 {\r
320                         /* We expected to have received something by the time we unblock. */\r
321                         xErrorDetected = pdTRUE;\r
322                 }\r
323 \r
324                 /* When we get here the high priority task should have peeked the data\r
325                 (unblocking this task) then suspended (allowing this task to also peek\r
326                 the data). */\r
327                 if( ulValue != 0x01234567 )\r
328                 {\r
329                         /* We did not receive the expected value. */\r
330                         xErrorDetected = pdTRUE;\r
331                 }\r
332 \r
333                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
334                 {\r
335                         /* The message should have been left on the queue. */\r
336                         xErrorDetected = pdTRUE;\r
337                 }\r
338 \r
339                 /* Just so we know the test is still running. */\r
340                 ulLoopCounter++;\r
341 \r
342                 /* Now we can suspend ourselves so the low priority task can execute\r
343                 again. */\r
344                 vTaskSuspend( NULL );\r
345         }\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 static void prvLowPriorityPeekTask( void *pvParameters )\r
350 {\r
351 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
352 unsigned long ulValue;\r
353 \r
354         for( ;; )\r
355         {\r
356                 /* Write some data to the queue.  This should unblock the highest\r
357                 priority task that is waiting to peek data from the queue. */\r
358                 ulValue = 0x11223344;\r
359                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
360                 {\r
361                         /* We were expecting the queue to be empty so we should not of\r
362                         had a problem writing to the queue. */\r
363                         xErrorDetected = pdTRUE;\r
364                 }\r
365 \r
366                 #if configUSE_PREEMPTION == 0\r
367                         taskYIELD();\r
368                 #endif\r
369 \r
370                 /* By the time we get here the data should have been removed from\r
371                 the queue. */\r
372                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
373                 {\r
374                         xErrorDetected = pdTRUE;\r
375                 }\r
376 \r
377                 /* Write another value to the queue, again waking the highest priority\r
378                 task that is blocked on the queue. */\r
379                 ulValue = 0x01234567;\r
380                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
381                 {\r
382                         /* We were expecting the queue to be empty so we should not of\r
383                         had a problem writing to the queue. */\r
384                         xErrorDetected = pdTRUE;\r
385                 }\r
386 \r
387                 #if configUSE_PREEMPTION == 0\r
388                         taskYIELD();\r
389                 #endif\r
390 \r
391                 /* All the other tasks should now have successfully peeked the data.\r
392                 The data is still in the queue so we should be able to receive it. */\r
393                 ulValue = 0;\r
394                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
395                 {\r
396                         /* We expected to receive the data. */\r
397                         xErrorDetected = pdTRUE;\r
398                 }\r
399 \r
400                 if( ulValue != 0x01234567 )\r
401                 {\r
402                         /* We did not receive the expected value. */\r
403                 }\r
404 \r
405                 /* Lets just delay a while as this is an intensive test as we don't\r
406                 want to starve other tests of processing time. */\r
407                 vTaskDelay( qpeekSHORT_DELAY );\r
408 \r
409                 /* Unsuspend the other tasks so we can repeat the test - this time\r
410                 however not all the other tasks will peek the data as the high\r
411                 priority task is actually going to remove it from the queue.  Send\r
412                 to front is used just to be different.  As the queue is empty it\r
413                 makes no difference to the result. */\r
414                 vTaskResume( xMediumPriorityTask );\r
415                 vTaskResume( xHighPriorityTask );\r
416                 vTaskResume( xHighestPriorityTask );\r
417 \r
418                 #if( configUSE_PREEMPTION == 0 )\r
419                         taskYIELD();\r
420                 #endif\r
421 \r
422                 ulValue = 0xaabbaabb;\r
423                 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
424                 {\r
425                         /* We were expecting the queue to be empty so we should not of\r
426                         had a problem writing to the queue. */\r
427                         xErrorDetected = pdTRUE;\r
428                 }\r
429 \r
430                 #if configUSE_PREEMPTION == 0\r
431                         taskYIELD();\r
432                 #endif\r
433 \r
434                 /* This time we should find that the queue is empty.  The high priority\r
435                 task actually removed the data rather than just peeking it. */\r
436                 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )\r
437                 {\r
438                         /* We expected to receive the data. */\r
439                         xErrorDetected = pdTRUE;\r
440                 }\r
441 \r
442                 /* Unsuspend the highest and high priority tasks so we can go back\r
443                 and repeat the whole thing.  The medium priority task should not be\r
444                 suspended as it was not able to peek the data in this last case. */\r
445                 vTaskResume( xHighPriorityTask );\r
446                 vTaskResume( xHighestPriorityTask );\r
447 \r
448                 /* Lets just delay a while as this is an intensive test as we don't\r
449                 want to starve other tests of processing time. */\r
450                 vTaskDelay( qpeekSHORT_DELAY );\r
451         }\r
452 }\r
453 /*-----------------------------------------------------------*/\r
454 \r
455 /* This is called to check that all the created tasks are still running. */\r
456 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )\r
457 {\r
458 static unsigned long ulLastLoopCounter = 0;\r
459 \r
460         /* If the demo task is still running then we expect the loopcounter to\r
461         have incremented since this function was last called. */\r
462         if( ulLastLoopCounter == ulLoopCounter )\r
463         {\r
464                 xErrorDetected = pdTRUE;\r
465         }\r
466 \r
467         ulLastLoopCounter = ulLoopCounter;\r
468 \r
469         /* Errors detected in the task itself will have latched xErrorDetected\r
470         to true. */\r
471 \r
472         return ( portBASE_TYPE ) !xErrorDetected;\r
473 }\r
474 \r