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