2 FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
4 This file is part of the FreeRTOS.org distribution.
6 FreeRTOS.org is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 FreeRTOS.org is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with FreeRTOS.org; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 A special exception to the GPL can be applied should you wish to distribute
21 a combined work that includes FreeRTOS.org, without being obliged to provide
22 the source code for any proprietary components. See the licensing section
23 of http://www.FreeRTOS.org for full details of how and when the exception
26 ***************************************************************************
27 ***************************************************************************
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
30 * and even write all or part of your application on your behalf. *
31 * See http://www.OpenRTOS.com for details of the services we provide to *
32 * expedite your project. *
34 ***************************************************************************
35 ***************************************************************************
37 Please ensure to read the configuration and relevant port sections of the
40 http://www.FreeRTOS.org - Documentation, latest information, license and
43 http://www.SafeRTOS.com - A version that is certified for use in safety
46 http://www.OpenRTOS.com - Commercial support, development, porting,
47 licensing and training services.
52 * Tests the behaviour when data is peeked from a queue when there are
53 * multiple tasks blocked on the queue.
59 /* Scheduler include files. */
65 /* Demo program include files. */
68 #define qpeekQUEUE_LENGTH ( 5 )
69 #define qpeekNO_BLOCK ( 0 )
70 #define qpeekSHORT_DELAY ( 10 )
72 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
73 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
74 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
75 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
77 /*-----------------------------------------------------------*/
80 * The following three tasks are used to demonstrate the peeking behaviour.
81 * Each task is given a different priority to demonstrate the order in which
82 * tasks are woken as data is peeked from a queue.
84 static void prvLowPriorityPeekTask( void *pvParameters );
85 static void prvMediumPriorityPeekTask( void *pvParameters );
86 static void prvHighPriorityPeekTask( void *pvParameters );
87 static void prvHighestPriorityPeekTask( void *pvParameters );
89 /*-----------------------------------------------------------*/
91 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
92 detected in any of the tasks. */
93 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
95 /* Counter that is incremented on each cycle of a test. This is used to
96 detect a stalled task - a test that is no longer running. */
97 static volatile unsigned portLONG ulLoopCounter = 0;
99 /* Handles to the test tasks. */
100 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
101 /*-----------------------------------------------------------*/
103 void vStartQueuePeekTasks( void )
107 /* Create the queue that we are going to use for the test/demo. */
108 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
110 /* Create the demo tasks and pass it the queue just created. We are
111 passing the queue handle by value so it does not matter that it is declared
112 on the stack here. */
113 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
114 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
115 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
116 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
118 /*-----------------------------------------------------------*/
120 static void prvHighestPriorityPeekTask( void *pvParameters )
122 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
123 unsigned portLONG ulValue;
127 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
129 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
131 /* Queue a message for printing to say the task has started. */
132 vPrintDisplayMessage( &pcTaskStartMsg );
138 /* Try peeking from the queue. The queue should be empty so we will
139 block, allowing the high priority task to execute. */
140 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
142 /* We expected to have received something by the time we unblock. */
143 xErrorDetected = pdTRUE;
146 /* When we reach here the high and medium priority tasks should still
147 be blocked on the queue. We unblocked because the low priority task
148 wrote a value to the queue, which we should have peeked. Peeking the
149 data (rather than receiving it) will leave the data on the queue, so
150 the high priority task should then have also been unblocked, but not
152 if( ulValue != 0x11223344 )
154 /* We did not receive the expected value. */
155 xErrorDetected = pdTRUE;
158 if( uxQueueMessagesWaiting( xQueue ) != 1 )
160 /* The message should have been left on the queue. */
161 xErrorDetected = pdTRUE;
164 /* Now we are going to actually receive the data, so when the high
165 priority task runs it will find the queue empty and return to the
168 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
170 /* We expected to receive the value. */
171 xErrorDetected = pdTRUE;
174 if( ulValue != 0x11223344 )
176 /* We did not receive the expected value - which should have been
177 the same value as was peeked. */
178 xErrorDetected = pdTRUE;
181 /* Now we will block again as the queue is once more empty. The low
182 priority task can then execute again. */
183 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
185 /* We expected to have received something by the time we unblock. */
186 xErrorDetected = pdTRUE;
189 /* When we get here the low priority task should have again written to the
191 if( ulValue != 0x01234567 )
193 /* We did not receive the expected value. */
194 xErrorDetected = pdTRUE;
197 if( uxQueueMessagesWaiting( xQueue ) != 1 )
199 /* The message should have been left on the queue. */
200 xErrorDetected = pdTRUE;
203 /* We only peeked the data, so suspending ourselves now should enable
204 the high priority task to also peek the data. The high priority task
205 will have been unblocked when we peeked the data as we left the data
207 vTaskSuspend( NULL );
211 /* This time we are going to do the same as the above test, but the
212 high priority task is going to receive the data, rather than peek it.
213 This means that the medium priority task should never peek the value. */
214 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
216 xErrorDetected = pdTRUE;
219 if( ulValue != 0xaabbaabb )
221 xErrorDetected = pdTRUE;
224 vTaskSuspend( NULL );
227 /*-----------------------------------------------------------*/
229 static void prvHighPriorityPeekTask( void *pvParameters )
231 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
232 unsigned portLONG ulValue;
236 /* Try peeking from the queue. The queue should be empty so we will
237 block, allowing the medium priority task to execute. Both the high
238 and highest priority tasks will then be blocked on the queue. */
239 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
241 /* We expected to have received something by the time we unblock. */
242 xErrorDetected = pdTRUE;
245 /* When we get here the highest priority task should have peeked the data
246 (unblocking this task) then suspended (allowing this task to also peek
248 if( ulValue != 0x01234567 )
250 /* We did not receive the expected value. */
251 xErrorDetected = pdTRUE;
254 if( uxQueueMessagesWaiting( xQueue ) != 1 )
256 /* The message should have been left on the queue. */
257 xErrorDetected = pdTRUE;
260 /* We only peeked the data, so suspending ourselves now should enable
261 the medium priority task to also peek the data. The medium priority task
262 will have been unblocked when we peeked the data as we left the data
264 vTaskSuspend( NULL );
267 /* This time we are going actually receive the value, so the medium
268 priority task will never peek the data - we removed it from the queue. */
269 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
271 xErrorDetected = pdTRUE;
274 if( ulValue != 0xaabbaabb )
276 xErrorDetected = pdTRUE;
279 vTaskSuspend( NULL );
282 /*-----------------------------------------------------------*/
284 static void prvMediumPriorityPeekTask( void *pvParameters )
286 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
287 unsigned portLONG ulValue;
291 /* Try peeking from the queue. The queue should be empty so we will
292 block, allowing the low priority task to execute. The highest, high
293 and medium priority tasks will then all be blocked on the queue. */
294 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
296 /* We expected to have received something by the time we unblock. */
297 xErrorDetected = pdTRUE;
300 /* When we get here the high priority task should have peeked the data
301 (unblocking this task) then suspended (allowing this task to also peek
303 if( ulValue != 0x01234567 )
305 /* We did not receive the expected value. */
306 xErrorDetected = pdTRUE;
309 if( uxQueueMessagesWaiting( xQueue ) != 1 )
311 /* The message should have been left on the queue. */
312 xErrorDetected = pdTRUE;
315 /* Just so we know the test is still running. */
318 /* Now we can suspend ourselves so the low priority task can execute
320 vTaskSuspend( NULL );
323 /*-----------------------------------------------------------*/
325 static void prvLowPriorityPeekTask( void *pvParameters )
327 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
328 unsigned portLONG ulValue;
332 /* Write some data to the queue. This should unblock the highest
333 priority task that is waiting to peek data from the queue. */
334 ulValue = 0x11223344;
335 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
337 /* We were expecting the queue to be empty so we should not of
338 had a problem writing to the queue. */
339 xErrorDetected = pdTRUE;
342 /* By the time we get here the data should have been removed from
344 if( uxQueueMessagesWaiting( xQueue ) != 0 )
346 xErrorDetected = pdTRUE;
349 /* Write another value to the queue, again waking the highest priority
350 task that is blocked on the queue. */
351 ulValue = 0x01234567;
352 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
354 /* We were expecting the queue to be empty so we should not of
355 had a problem writing to the queue. */
356 xErrorDetected = pdTRUE;
359 /* All the other tasks should now have successfully peeked the data.
360 The data is still in the queue so we should be able to receive it. */
362 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
364 /* We expected to receive the data. */
365 xErrorDetected = pdTRUE;
368 if( ulValue != 0x01234567 )
370 /* We did not receive the expected value. */
373 /* Lets just delay a while as this is an intensive test as we don't
374 want to starve other tests of processing time. */
375 vTaskDelay( qpeekSHORT_DELAY );
377 /* Unsuspend the other tasks so we can repeat the test - this time
378 however not all the other tasks will peek the data as the high
379 priority task is actually going to remove it from the queue. Send
380 to front is used just to be different. As the queue is empty it
381 makes no difference to the result. */
382 vTaskResume( xMediumPriorityTask );
383 vTaskResume( xHighPriorityTask );
384 vTaskResume( xHighestPriorityTask );
386 ulValue = 0xaabbaabb;
387 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
389 /* We were expecting the queue to be empty so we should not of
390 had a problem writing to the queue. */
391 xErrorDetected = pdTRUE;
394 /* This time we should find that the queue is empty. The high priority
395 task actually removed the data rather than just peeking it. */
396 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
398 /* We expected to receive the data. */
399 xErrorDetected = pdTRUE;
402 /* Unsuspend the highest and high priority tasks so we can go back
403 and repeat the whole thing. The medium priority task should not be
404 suspended as it was not able to peek the data in this last case. */
405 vTaskResume( xHighPriorityTask );
406 vTaskResume( xHighestPriorityTask );
408 /* Lets just delay a while as this is an intensive test as we don't
409 want to starve other tests of processing time. */
410 vTaskDelay( qpeekSHORT_DELAY );
413 /*-----------------------------------------------------------*/
415 /* This is called to check that all the created tasks are still running. */
416 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
418 static unsigned portLONG ulLastLoopCounter = 0;
420 /* If the demo task is still running then we expect the loopcounter to
421 have incremented since this function was last called. */
422 if( ulLastLoopCounter == ulLoopCounter )
424 xErrorDetected = pdTRUE;
427 ulLastLoopCounter = ulLoopCounter;
429 /* Errors detected in the task itself will have latched xErrorDetected
432 return !xErrorDetected;