]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QPeek.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Minimal / QPeek.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /*\r
31  * Tests the behaviour when data is peeked from a queue when there are\r
32  * multiple tasks blocked on the queue.\r
33  */\r
34 \r
35 \r
36 #include <stdlib.h>\r
37 \r
38 /* Scheduler include files. */\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 #include "queue.h"\r
42 #include "semphr.h"\r
43 \r
44 /* Demo program include files. */\r
45 #include "QPeek.h"\r
46 \r
47 #define qpeekQUEUE_LENGTH               ( 5 )\r
48 #define qpeekNO_BLOCK                   ( 0 )\r
49 #define qpeekSHORT_DELAY                ( 10 )\r
50 \r
51 #define qpeekLOW_PRIORITY                       ( tskIDLE_PRIORITY + 0 )\r
52 #define qpeekMEDIUM_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
53 #define qpeekHIGH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
54 #define qpeekHIGHEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
55 \r
56 /*-----------------------------------------------------------*/\r
57 \r
58 /*\r
59  * The following three tasks are used to demonstrate the peeking behaviour.\r
60  * Each task is given a different priority to demonstrate the order in which\r
61  * tasks are woken as data is peeked from a queue.\r
62  */\r
63 static void prvLowPriorityPeekTask( void *pvParameters );\r
64 static void prvMediumPriorityPeekTask( void *pvParameters );\r
65 static void prvHighPriorityPeekTask( void *pvParameters );\r
66 static void prvHighestPriorityPeekTask( void *pvParameters );\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
71 detected in any of the tasks. */\r
72 static volatile BaseType_t xErrorDetected = pdFALSE;\r
73 \r
74 /* Counter that is incremented on each cycle of a test.  This is used to\r
75 detect a stalled task - a test that is no longer running. */\r
76 static volatile uint32_t ulLoopCounter = 0;\r
77 \r
78 /* Handles to the test tasks. */\r
79 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;\r
80 /*-----------------------------------------------------------*/\r
81 \r
82 void vStartQueuePeekTasks( void )\r
83 {\r
84 QueueHandle_t xQueue;\r
85 \r
86         /* Create the queue that we are going to use for the test/demo. */\r
87         xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( uint32_t ) );\r
88 \r
89         if( xQueue != NULL )\r
90         {\r
91                 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
92                 in use.  The queue registry is provided as a means for kernel aware\r
93                 debuggers to locate queues and has no purpose if a kernel aware debugger\r
94                 is not being used.  The call to vQueueAddToRegistry() will be removed\r
95                 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
96                 defined to be less than 1. */\r
97                 vQueueAddToRegistry( xQueue, "QPeek_Test_Queue" );\r
98 \r
99                 /* Create the demo tasks and pass it the queue just created.  We are\r
100                 passing the queue handle by value so it does not matter that it is declared\r
101                 on the stack here. */\r
102                 xTaskCreate( prvLowPriorityPeekTask, "PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );\r
103                 xTaskCreate( prvMediumPriorityPeekTask, "PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );\r
104                 xTaskCreate( prvHighPriorityPeekTask, "PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );\r
105                 xTaskCreate( prvHighestPriorityPeekTask, "PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );\r
106         }\r
107 }\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 static void prvHighestPriorityPeekTask( void *pvParameters )\r
111 {\r
112 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
113 uint32_t ulValue;\r
114 \r
115         #ifdef USE_STDIO\r
116         {\r
117                 void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
118 \r
119                 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";\r
120 \r
121                 /* Queue a message for printing to say the task has started. */\r
122                 vPrintDisplayMessage( &pcTaskStartMsg );\r
123         }\r
124         #endif\r
125 \r
126         for( ;; )\r
127         {\r
128                 /* Try peeking from the queue.  The queue should be empty so we will\r
129                 block, allowing the high priority task to execute. */\r
130                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
131                 {\r
132                         /* We expected to have received something by the time we unblock. */\r
133                         xErrorDetected = pdTRUE;\r
134                 }\r
135 \r
136                 /* When we reach here the high and medium priority tasks should still\r
137                 be blocked on the queue.  We unblocked because the low priority task\r
138                 wrote a value to the queue, which we should have peeked.  Peeking the\r
139                 data (rather than receiving it) will leave the data on the queue, so\r
140                 the high priority task should then have also been unblocked, but not\r
141                 yet executed. */\r
142                 if( ulValue != 0x11223344 )\r
143                 {\r
144                         /* We did not receive the expected value. */\r
145                         xErrorDetected = pdTRUE;\r
146                 }\r
147 \r
148                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
149                 {\r
150                         /* The message should have been left on the queue. */\r
151                         xErrorDetected = pdTRUE;\r
152                 }\r
153 \r
154                 /* Now we are going to actually receive the data, so when the high\r
155                 priority task runs it will find the queue empty and return to the\r
156                 blocked state. */\r
157                 ulValue = 0;\r
158                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
159                 {\r
160                         /* We expected to receive the value. */\r
161                         xErrorDetected = pdTRUE;\r
162                 }\r
163 \r
164                 if( ulValue != 0x11223344 )\r
165                 {\r
166                         /* We did not receive the expected value - which should have been\r
167                         the same value as was peeked. */\r
168                         xErrorDetected = pdTRUE;\r
169                 }\r
170 \r
171                 /* Now we will block again as the queue is once more empty.  The low\r
172                 priority task can then execute again. */\r
173                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
174                 {\r
175                         /* We expected to have received something by the time we unblock. */\r
176                         xErrorDetected = pdTRUE;\r
177                 }\r
178 \r
179                 /* When we get here the low priority task should have again written to the\r
180                 queue. */\r
181                 if( ulValue != 0x01234567 )\r
182                 {\r
183                         /* We did not receive the expected value. */\r
184                         xErrorDetected = pdTRUE;\r
185                 }\r
186 \r
187                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
188                 {\r
189                         /* The message should have been left on the queue. */\r
190                         xErrorDetected = pdTRUE;\r
191                 }\r
192 \r
193                 /* We only peeked the data, so suspending ourselves now should enable\r
194                 the high priority task to also peek the data.  The high priority task\r
195                 will have been unblocked when we peeked the data as we left the data\r
196                 in the queue. */\r
197                 vTaskSuspend( NULL );\r
198 \r
199 \r
200 \r
201                 /* This time we are going to do the same as the above test, but the\r
202                 high priority task is going to receive the data, rather than peek it.\r
203                 This means that the medium priority task should never peek the value. */\r
204                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
205                 {\r
206                         xErrorDetected = pdTRUE;\r
207                 }\r
208 \r
209                 if( ulValue != 0xaabbaabb )\r
210                 {\r
211                         xErrorDetected = pdTRUE;\r
212                 }\r
213 \r
214                 vTaskSuspend( NULL );\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void prvHighPriorityPeekTask( void *pvParameters )\r
220 {\r
221 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
222 uint32_t ulValue;\r
223 \r
224         for( ;; )\r
225         {\r
226                 /* Try peeking from the queue.  The queue should be empty so we will\r
227                 block, allowing the medium priority task to execute.  Both the high\r
228                 and highest priority tasks will then be blocked on the queue. */\r
229                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
230                 {\r
231                         /* We expected to have received something by the time we unblock. */\r
232                         xErrorDetected = pdTRUE;\r
233                 }\r
234 \r
235                 /* When we get here the highest priority task should have peeked the data\r
236                 (unblocking this task) then suspended (allowing this task to also peek\r
237                 the data). */\r
238                 if( ulValue != 0x01234567 )\r
239                 {\r
240                         /* We did not receive the expected value. */\r
241                         xErrorDetected = pdTRUE;\r
242                 }\r
243 \r
244                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
245                 {\r
246                         /* The message should have been left on the queue. */\r
247                         xErrorDetected = pdTRUE;\r
248                 }\r
249 \r
250                 /* We only peeked the data, so suspending ourselves now should enable\r
251                 the medium priority task to also peek the data.  The medium priority task\r
252                 will have been unblocked when we peeked the data as we left the data\r
253                 in the queue. */\r
254                 vTaskSuspend( NULL );\r
255 \r
256 \r
257                 /* This time we are going actually receive the value, so the medium\r
258                 priority task will never peek the data - we removed it from the queue. */\r
259                 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
260                 {\r
261                         xErrorDetected = pdTRUE;\r
262                 }\r
263 \r
264                 if( ulValue != 0xaabbaabb )\r
265                 {\r
266                         xErrorDetected = pdTRUE;\r
267                 }\r
268 \r
269                 vTaskSuspend( NULL );\r
270         }\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void prvMediumPriorityPeekTask( void *pvParameters )\r
275 {\r
276 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
277 uint32_t ulValue;\r
278 \r
279         for( ;; )\r
280         {\r
281                 /* Try peeking from the queue.  The queue should be empty so we will\r
282                 block, allowing the low priority task to execute.  The highest, high\r
283                 and medium priority tasks will then all be blocked on the queue. */\r
284                 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )\r
285                 {\r
286                         /* We expected to have received something by the time we unblock. */\r
287                         xErrorDetected = pdTRUE;\r
288                 }\r
289 \r
290                 /* When we get here the high priority task should have peeked the data\r
291                 (unblocking this task) then suspended (allowing this task to also peek\r
292                 the data). */\r
293                 if( ulValue != 0x01234567 )\r
294                 {\r
295                         /* We did not receive the expected value. */\r
296                         xErrorDetected = pdTRUE;\r
297                 }\r
298 \r
299                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
300                 {\r
301                         /* The message should have been left on the queue. */\r
302                         xErrorDetected = pdTRUE;\r
303                 }\r
304 \r
305                 /* Just so we know the test is still running. */\r
306                 ulLoopCounter++;\r
307 \r
308                 /* Now we can suspend ourselves so the low priority task can execute\r
309                 again. */\r
310                 vTaskSuspend( NULL );\r
311         }\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvLowPriorityPeekTask( void *pvParameters )\r
316 {\r
317 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;\r
318 uint32_t ulValue;\r
319 \r
320         for( ;; )\r
321         {\r
322                 /* Write some data to the queue.  This should unblock the highest\r
323                 priority task that is waiting to peek data from the queue. */\r
324                 ulValue = 0x11223344;\r
325                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
326                 {\r
327                         /* We were expecting the queue to be empty so we should not of\r
328                         had a problem writing to the queue. */\r
329                         xErrorDetected = pdTRUE;\r
330                 }\r
331 \r
332                 #if configUSE_PREEMPTION == 0\r
333                         taskYIELD();\r
334                 #endif\r
335 \r
336                 /* By the time we get here the data should have been removed from\r
337                 the queue. */\r
338                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
339                 {\r
340                         xErrorDetected = pdTRUE;\r
341                 }\r
342 \r
343                 /* Write another value to the queue, again waking the highest priority\r
344                 task that is blocked on the queue. */\r
345                 ulValue = 0x01234567;\r
346                 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
347                 {\r
348                         /* We were expecting the queue to be empty so we should not of\r
349                         had a problem writing to the queue. */\r
350                         xErrorDetected = pdTRUE;\r
351                 }\r
352 \r
353                 #if configUSE_PREEMPTION == 0\r
354                         taskYIELD();\r
355                 #endif\r
356 \r
357                 /* All the other tasks should now have successfully peeked the data.\r
358                 The data is still in the queue so we should be able to receive it. */\r
359                 ulValue = 0;\r
360                 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
361                 {\r
362                         /* We expected to receive the data. */\r
363                         xErrorDetected = pdTRUE;\r
364                 }\r
365 \r
366                 if( ulValue != 0x01234567 )\r
367                 {\r
368                         /* We did not receive the expected value. */\r
369                 }\r
370 \r
371                 /* Lets just delay a while as this is an intensive test as we don't\r
372                 want to starve other tests of processing time. */\r
373                 vTaskDelay( qpeekSHORT_DELAY );\r
374 \r
375                 /* Unsuspend the other tasks so we can repeat the test - this time\r
376                 however not all the other tasks will peek the data as the high\r
377                 priority task is actually going to remove it from the queue.  Send\r
378                 to front is used just to be different.  As the queue is empty it\r
379                 makes no difference to the result. */\r
380                 vTaskResume( xMediumPriorityTask );\r
381                 vTaskResume( xHighPriorityTask );\r
382                 vTaskResume( xHighestPriorityTask );\r
383 \r
384                 #if( configUSE_PREEMPTION == 0 )\r
385                         taskYIELD();\r
386                 #endif\r
387 \r
388                 ulValue = 0xaabbaabb;\r
389                 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )\r
390                 {\r
391                         /* We were expecting the queue to be empty so we should not of\r
392                         had a problem writing to the queue. */\r
393                         xErrorDetected = pdTRUE;\r
394                 }\r
395 \r
396                 #if configUSE_PREEMPTION == 0\r
397                         taskYIELD();\r
398                 #endif\r
399 \r
400                 /* This time we should find that the queue is empty.  The high priority\r
401                 task actually removed the data rather than just peeking it. */\r
402                 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )\r
403                 {\r
404                         /* We expected to receive the data. */\r
405                         xErrorDetected = pdTRUE;\r
406                 }\r
407 \r
408                 /* Unsuspend the highest and high priority tasks so we can go back\r
409                 and repeat the whole thing.  The medium priority task should not be\r
410                 suspended as it was not able to peek the data in this last case. */\r
411                 vTaskResume( xHighPriorityTask );\r
412                 vTaskResume( xHighestPriorityTask );\r
413 \r
414                 /* Lets just delay a while as this is an intensive test as we don't\r
415                 want to starve other tests of processing time. */\r
416                 vTaskDelay( qpeekSHORT_DELAY );\r
417         }\r
418 }\r
419 /*-----------------------------------------------------------*/\r
420 \r
421 /* This is called to check that all the created tasks are still running. */\r
422 BaseType_t xAreQueuePeekTasksStillRunning( void )\r
423 {\r
424 static uint32_t ulLastLoopCounter = 0;\r
425 \r
426         /* If the demo task is still running then we expect the loopcounter to\r
427         have incremented since this function was last called. */\r
428         if( ulLastLoopCounter == ulLoopCounter )\r
429         {\r
430                 xErrorDetected = pdTRUE;\r
431         }\r
432 \r
433         ulLastLoopCounter = ulLoopCounter;\r
434 \r
435         /* Errors detected in the task itself will have latched xErrorDetected\r
436         to true. */\r
437 \r
438         return ( BaseType_t ) !xErrorDetected;\r
439 }\r
440 \r