2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
31 * Tests the behaviour when data is peeked from a queue when there are
\r
32 * multiple tasks blocked on the queue.
\r
38 /* Scheduler include files. */
\r
39 #include "FreeRTOS.h"
\r
44 /* Demo program include files. */
\r
47 #define qpeekQUEUE_LENGTH ( 5 )
\r
48 #define qpeekNO_BLOCK ( 0 )
\r
49 #define qpeekSHORT_DELAY ( 10 )
\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
56 /*-----------------------------------------------------------*/
\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
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
68 /*-----------------------------------------------------------*/
\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
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
78 /* Handles to the test tasks. */
\r
79 TaskHandle_t xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
80 /*-----------------------------------------------------------*/
\r
82 void vStartQueuePeekTasks( void )
\r
84 QueueHandle_t xQueue;
\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
89 if( xQueue != NULL )
\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
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
108 /*-----------------------------------------------------------*/
\r
110 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
112 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\r
117 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
119 const char * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
121 /* Queue a message for printing to say the task has started. */
\r
122 vPrintDisplayMessage( &pcTaskStartMsg );
\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
132 /* We expected to have received something by the time we unblock. */
\r
133 xErrorDetected = pdTRUE;
\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
142 if( ulValue != 0x11223344 )
\r
144 /* We did not receive the expected value. */
\r
145 xErrorDetected = pdTRUE;
\r
148 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
150 /* The message should have been left on the queue. */
\r
151 xErrorDetected = pdTRUE;
\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
158 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
160 /* We expected to receive the value. */
\r
161 xErrorDetected = pdTRUE;
\r
164 if( ulValue != 0x11223344 )
\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
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
175 /* We expected to have received something by the time we unblock. */
\r
176 xErrorDetected = pdTRUE;
\r
179 /* When we get here the low priority task should have again written to the
\r
181 if( ulValue != 0x01234567 )
\r
183 /* We did not receive the expected value. */
\r
184 xErrorDetected = pdTRUE;
\r
187 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
189 /* The message should have been left on the queue. */
\r
190 xErrorDetected = pdTRUE;
\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
197 vTaskSuspend( NULL );
\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
206 xErrorDetected = pdTRUE;
\r
209 if( ulValue != 0xaabbaabb )
\r
211 xErrorDetected = pdTRUE;
\r
214 vTaskSuspend( NULL );
\r
217 /*-----------------------------------------------------------*/
\r
219 static void prvHighPriorityPeekTask( void *pvParameters )
\r
221 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\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
231 /* We expected to have received something by the time we unblock. */
\r
232 xErrorDetected = pdTRUE;
\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
238 if( ulValue != 0x01234567 )
\r
240 /* We did not receive the expected value. */
\r
241 xErrorDetected = pdTRUE;
\r
244 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
246 /* The message should have been left on the queue. */
\r
247 xErrorDetected = pdTRUE;
\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
254 vTaskSuspend( NULL );
\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
261 xErrorDetected = pdTRUE;
\r
264 if( ulValue != 0xaabbaabb )
\r
266 xErrorDetected = pdTRUE;
\r
269 vTaskSuspend( NULL );
\r
272 /*-----------------------------------------------------------*/
\r
274 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
276 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\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
286 /* We expected to have received something by the time we unblock. */
\r
287 xErrorDetected = pdTRUE;
\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
293 if( ulValue != 0x01234567 )
\r
295 /* We did not receive the expected value. */
\r
296 xErrorDetected = pdTRUE;
\r
299 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
301 /* The message should have been left on the queue. */
\r
302 xErrorDetected = pdTRUE;
\r
305 /* Just so we know the test is still running. */
\r
308 /* Now we can suspend ourselves so the low priority task can execute
\r
310 vTaskSuspend( NULL );
\r
313 /*-----------------------------------------------------------*/
\r
315 static void prvLowPriorityPeekTask( void *pvParameters )
\r
317 QueueHandle_t xQueue = ( QueueHandle_t ) pvParameters;
\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
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
332 #if configUSE_PREEMPTION == 0
\r
336 /* By the time we get here the data should have been removed from
\r
338 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
340 xErrorDetected = pdTRUE;
\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
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
353 #if configUSE_PREEMPTION == 0
\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
360 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
362 /* We expected to receive the data. */
\r
363 xErrorDetected = pdTRUE;
\r
366 if( ulValue != 0x01234567 )
\r
368 /* We did not receive the expected value. */
\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
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
384 #if( configUSE_PREEMPTION == 0 )
\r
388 ulValue = 0xaabbaabb;
\r
389 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\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
396 #if configUSE_PREEMPTION == 0
\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
404 /* We expected to receive the data. */
\r
405 xErrorDetected = pdTRUE;
\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
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
419 /*-----------------------------------------------------------*/
\r
421 /* This is called to check that all the created tasks are still running. */
\r
422 BaseType_t xAreQueuePeekTasksStillRunning( void )
\r
424 static uint32_t ulLastLoopCounter = 0;
\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
430 xErrorDetected = pdTRUE;
\r
433 ulLastLoopCounter = ulLoopCounter;
\r
435 /* Errors detected in the task itself will have latched xErrorDetected
\r
438 return ( BaseType_t ) !xErrorDetected;
\r