2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 * Tests the behaviour when data is peeked from a queue when there are
\r
31 * multiple tasks blocked on the queue.
\r
37 /* Scheduler include files. */
\r
38 #include "FreeRTOS.h"
\r
43 /* Demo program include files. */
\r
46 #define qpeekQUEUE_LENGTH ( 5 )
\r
47 #define qpeekNO_BLOCK ( 0 )
\r
48 #define qpeekSHORT_DELAY ( 10 )
\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
55 /*-----------------------------------------------------------*/
\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
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
67 /*-----------------------------------------------------------*/
\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
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
77 /* Handles to the test tasks. */
\r
78 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
79 /*-----------------------------------------------------------*/
\r
81 void vStartQueuePeekTasks( void )
\r
83 QueueHandle_t xQueue;
\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
88 if( xQueue != NULL )
\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
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
107 /*-----------------------------------------------------------*/
\r
109 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
111 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
116 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
118 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
120 /* Queue a message for printing to say the task has started. */
\r
121 vPrintDisplayMessage( &pcTaskStartMsg );
\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
131 /* We expected to have received something by the time we unblock. */
\r
132 xErrorDetected = pdTRUE;
\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
141 if( ulValue != 0x11223344 )
\r
143 /* We did not receive the expected value. */
\r
144 xErrorDetected = pdTRUE;
\r
147 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
149 /* The message should have been left on the queue. */
\r
150 xErrorDetected = pdTRUE;
\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
157 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
159 /* We expected to receive the value. */
\r
160 xErrorDetected = pdTRUE;
\r
163 if( ulValue != 0x11223344 )
\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
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
174 /* We expected to have received something by the time we unblock. */
\r
175 xErrorDetected = pdTRUE;
\r
178 /* When we get here the low priority task should have again written to the
\r
180 if( ulValue != 0x01234567 )
\r
182 /* We did not receive the expected value. */
\r
183 xErrorDetected = pdTRUE;
\r
186 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
188 /* The message should have been left on the queue. */
\r
189 xErrorDetected = pdTRUE;
\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
196 vTaskSuspend( NULL );
\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
205 xErrorDetected = pdTRUE;
\r
208 if( ulValue != 0xaabbaabb )
\r
210 xErrorDetected = pdTRUE;
\r
213 vTaskSuspend( NULL );
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvHighPriorityPeekTask( void *pvParameters )
\r
220 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\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
230 /* We expected to have received something by the time we unblock. */
\r
231 xErrorDetected = pdTRUE;
\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
237 if( ulValue != 0x01234567 )
\r
239 /* We did not receive the expected value. */
\r
240 xErrorDetected = pdTRUE;
\r
243 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
245 /* The message should have been left on the queue. */
\r
246 xErrorDetected = pdTRUE;
\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
253 vTaskSuspend( NULL );
\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
260 xErrorDetected = pdTRUE;
\r
263 if( ulValue != 0xaabbaabb )
\r
265 xErrorDetected = pdTRUE;
\r
268 vTaskSuspend( NULL );
\r
271 /*-----------------------------------------------------------*/
\r
273 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
275 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\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
285 /* We expected to have received something by the time we unblock. */
\r
286 xErrorDetected = pdTRUE;
\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
292 if( ulValue != 0x01234567 )
\r
294 /* We did not receive the expected value. */
\r
295 xErrorDetected = pdTRUE;
\r
298 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
300 /* The message should have been left on the queue. */
\r
301 xErrorDetected = pdTRUE;
\r
304 /* Just so we know the test is still running. */
\r
307 /* Now we can suspend ourselves so the low priority task can execute
\r
309 vTaskSuspend( NULL );
\r
312 /*-----------------------------------------------------------*/
\r
314 static void prvLowPriorityPeekTask( void *pvParameters )
\r
316 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\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
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
331 #if configUSE_PREEMPTION == 0
\r
335 /* By the time we get here the data should have been removed from
\r
337 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
339 xErrorDetected = pdTRUE;
\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
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
352 #if configUSE_PREEMPTION == 0
\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
359 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
361 /* We expected to receive the data. */
\r
362 xErrorDetected = pdTRUE;
\r
365 if( ulValue != 0x01234567 )
\r
367 /* We did not receive the expected value. */
\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
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
383 #if( configUSE_PREEMPTION == 0 )
\r
387 ulValue = 0xaabbaabb;
\r
388 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\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
395 #if configUSE_PREEMPTION == 0
\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
403 /* We expected to receive the data. */
\r
404 xErrorDetected = pdTRUE;
\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
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
418 /*-----------------------------------------------------------*/
\r
420 /* This is called to check that all the created tasks are still running. */
\r
421 BaseType_t xAreQueuePeekTasksStillRunning( void )
\r
423 static uint32_t ulLastLoopCounter = 0;
\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
429 xErrorDetected = pdTRUE;
\r
432 ulLastLoopCounter = ulLoopCounter;
\r
434 /* Errors detected in the task itself will have latched xErrorDetected
\r
437 return ( BaseType_t ) !xErrorDetected;
\r