2 FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
52 * Tests the behaviour when data is peeked from a queue when there are
\r
53 * multiple tasks blocked on the queue.
\r
59 /* Scheduler include files. */
\r
60 #include "FreeRTOS.h"
\r
65 /* Demo program include files. */
\r
68 #define qpeekQUEUE_LENGTH ( 5 )
\r
69 #define qpeekNO_BLOCK ( 0 )
\r
70 #define qpeekSHORT_DELAY ( 10 )
\r
72 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
73 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
74 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
75 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
77 /*-----------------------------------------------------------*/
\r
80 * The following three tasks are used to demonstrate the peeking behaviour.
\r
81 * Each task is given a different priority to demonstrate the order in which
\r
82 * tasks are woken as data is peeked from a queue.
\r
84 static void prvLowPriorityPeekTask( void *pvParameters );
\r
85 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
86 static void prvHighPriorityPeekTask( void *pvParameters );
\r
87 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
89 /*-----------------------------------------------------------*/
\r
91 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
92 detected in any of the tasks. */
\r
93 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
95 /* Counter that is incremented on each cycle of a test. This is used to
\r
96 detect a stalled task - a test that is no longer running. */
\r
97 static volatile unsigned portLONG ulLoopCounter = 0;
\r
99 /* Handles to the test tasks. */
\r
100 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
101 /*-----------------------------------------------------------*/
\r
103 void vStartQueuePeekTasks( void )
\r
105 xQueueHandle xQueue;
\r
107 /* Create the queue that we are going to use for the test/demo. */
\r
108 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
110 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
111 in use. The queue registry is provided as a means for kernel aware
\r
112 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
113 is not being used. The call to vQueueAddToRegistry() will be removed
\r
114 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
115 defined to be less than 1. */
\r
116 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "QPeek_Test_Queue" );
\r
118 /* Create the demo tasks and pass it the queue just created. We are
\r
119 passing the queue handle by value so it does not matter that it is declared
\r
120 on the stack here. */
\r
121 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
122 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
123 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
124 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
126 /*-----------------------------------------------------------*/
\r
128 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
130 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
131 unsigned portLONG ulValue;
\r
135 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
137 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
139 /* Queue a message for printing to say the task has started. */
\r
140 vPrintDisplayMessage( &pcTaskStartMsg );
\r
146 /* Try peeking from the queue. The queue should be empty so we will
\r
147 block, allowing the high priority task to execute. */
\r
148 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
150 /* We expected to have received something by the time we unblock. */
\r
151 xErrorDetected = pdTRUE;
\r
154 /* When we reach here the high and medium priority tasks should still
\r
155 be blocked on the queue. We unblocked because the low priority task
\r
156 wrote a value to the queue, which we should have peeked. Peeking the
\r
157 data (rather than receiving it) will leave the data on the queue, so
\r
158 the high priority task should then have also been unblocked, but not
\r
160 if( ulValue != 0x11223344 )
\r
162 /* We did not receive the expected value. */
\r
163 xErrorDetected = pdTRUE;
\r
166 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
168 /* The message should have been left on the queue. */
\r
169 xErrorDetected = pdTRUE;
\r
172 /* Now we are going to actually receive the data, so when the high
\r
173 priority task runs it will find the queue empty and return to the
\r
176 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
178 /* We expected to receive the value. */
\r
179 xErrorDetected = pdTRUE;
\r
182 if( ulValue != 0x11223344 )
\r
184 /* We did not receive the expected value - which should have been
\r
185 the same value as was peeked. */
\r
186 xErrorDetected = pdTRUE;
\r
189 /* Now we will block again as the queue is once more empty. The low
\r
190 priority task can then execute again. */
\r
191 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
193 /* We expected to have received something by the time we unblock. */
\r
194 xErrorDetected = pdTRUE;
\r
197 /* When we get here the low priority task should have again written to the
\r
199 if( ulValue != 0x01234567 )
\r
201 /* We did not receive the expected value. */
\r
202 xErrorDetected = pdTRUE;
\r
205 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
207 /* The message should have been left on the queue. */
\r
208 xErrorDetected = pdTRUE;
\r
211 /* We only peeked the data, so suspending ourselves now should enable
\r
212 the high priority task to also peek the data. The high priority task
\r
213 will have been unblocked when we peeked the data as we left the data
\r
215 vTaskSuspend( NULL );
\r
219 /* This time we are going to do the same as the above test, but the
\r
220 high priority task is going to receive the data, rather than peek it.
\r
221 This means that the medium priority task should never peek the value. */
\r
222 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
224 xErrorDetected = pdTRUE;
\r
227 if( ulValue != 0xaabbaabb )
\r
229 xErrorDetected = pdTRUE;
\r
232 vTaskSuspend( NULL );
\r
235 /*-----------------------------------------------------------*/
\r
237 static void prvHighPriorityPeekTask( void *pvParameters )
\r
239 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
240 unsigned portLONG ulValue;
\r
244 /* Try peeking from the queue. The queue should be empty so we will
\r
245 block, allowing the medium priority task to execute. Both the high
\r
246 and highest priority tasks will then be blocked on the queue. */
\r
247 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
249 /* We expected to have received something by the time we unblock. */
\r
250 xErrorDetected = pdTRUE;
\r
253 /* When we get here the highest priority task should have peeked the data
\r
254 (unblocking this task) then suspended (allowing this task to also peek
\r
256 if( ulValue != 0x01234567 )
\r
258 /* We did not receive the expected value. */
\r
259 xErrorDetected = pdTRUE;
\r
262 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
264 /* The message should have been left on the queue. */
\r
265 xErrorDetected = pdTRUE;
\r
268 /* We only peeked the data, so suspending ourselves now should enable
\r
269 the medium priority task to also peek the data. The medium priority task
\r
270 will have been unblocked when we peeked the data as we left the data
\r
272 vTaskSuspend( NULL );
\r
275 /* This time we are going actually receive the value, so the medium
\r
276 priority task will never peek the data - we removed it from the queue. */
\r
277 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
279 xErrorDetected = pdTRUE;
\r
282 if( ulValue != 0xaabbaabb )
\r
284 xErrorDetected = pdTRUE;
\r
287 vTaskSuspend( NULL );
\r
290 /*-----------------------------------------------------------*/
\r
292 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
294 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
295 unsigned portLONG ulValue;
\r
299 /* Try peeking from the queue. The queue should be empty so we will
\r
300 block, allowing the low priority task to execute. The highest, high
\r
301 and medium priority tasks will then all be blocked on the queue. */
\r
302 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
304 /* We expected to have received something by the time we unblock. */
\r
305 xErrorDetected = pdTRUE;
\r
308 /* When we get here the high priority task should have peeked the data
\r
309 (unblocking this task) then suspended (allowing this task to also peek
\r
311 if( ulValue != 0x01234567 )
\r
313 /* We did not receive the expected value. */
\r
314 xErrorDetected = pdTRUE;
\r
317 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
319 /* The message should have been left on the queue. */
\r
320 xErrorDetected = pdTRUE;
\r
323 /* Just so we know the test is still running. */
\r
326 /* Now we can suspend ourselves so the low priority task can execute
\r
328 vTaskSuspend( NULL );
\r
331 /*-----------------------------------------------------------*/
\r
333 static void prvLowPriorityPeekTask( void *pvParameters )
\r
335 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
336 unsigned portLONG ulValue;
\r
340 /* Write some data to the queue. This should unblock the highest
\r
341 priority task that is waiting to peek data from the queue. */
\r
342 ulValue = 0x11223344;
\r
343 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
345 /* We were expecting the queue to be empty so we should not of
\r
346 had a problem writing to the queue. */
\r
347 xErrorDetected = pdTRUE;
\r
350 /* By the time we get here the data should have been removed from
\r
352 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
354 xErrorDetected = pdTRUE;
\r
357 /* Write another value to the queue, again waking the highest priority
\r
358 task that is blocked on the queue. */
\r
359 ulValue = 0x01234567;
\r
360 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
362 /* We were expecting the queue to be empty so we should not of
\r
363 had a problem writing to the queue. */
\r
364 xErrorDetected = pdTRUE;
\r
367 /* All the other tasks should now have successfully peeked the data.
\r
368 The data is still in the queue so we should be able to receive it. */
\r
370 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
372 /* We expected to receive the data. */
\r
373 xErrorDetected = pdTRUE;
\r
376 if( ulValue != 0x01234567 )
\r
378 /* We did not receive the expected value. */
\r
381 /* Lets just delay a while as this is an intensive test as we don't
\r
382 want to starve other tests of processing time. */
\r
383 vTaskDelay( qpeekSHORT_DELAY );
\r
385 /* Unsuspend the other tasks so we can repeat the test - this time
\r
386 however not all the other tasks will peek the data as the high
\r
387 priority task is actually going to remove it from the queue. Send
\r
388 to front is used just to be different. As the queue is empty it
\r
389 makes no difference to the result. */
\r
390 vTaskResume( xMediumPriorityTask );
\r
391 vTaskResume( xHighPriorityTask );
\r
392 vTaskResume( xHighestPriorityTask );
\r
394 ulValue = 0xaabbaabb;
\r
395 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
397 /* We were expecting the queue to be empty so we should not of
\r
398 had a problem writing to the queue. */
\r
399 xErrorDetected = pdTRUE;
\r
402 /* This time we should find that the queue is empty. The high priority
\r
403 task actually removed the data rather than just peeking it. */
\r
404 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
406 /* We expected to receive the data. */
\r
407 xErrorDetected = pdTRUE;
\r
410 /* Unsuspend the highest and high priority tasks so we can go back
\r
411 and repeat the whole thing. The medium priority task should not be
\r
412 suspended as it was not able to peek the data in this last case. */
\r
413 vTaskResume( xHighPriorityTask );
\r
414 vTaskResume( xHighestPriorityTask );
\r
416 /* Lets just delay a while as this is an intensive test as we don't
\r
417 want to starve other tests of processing time. */
\r
418 vTaskDelay( qpeekSHORT_DELAY );
\r
421 /*-----------------------------------------------------------*/
\r
423 /* This is called to check that all the created tasks are still running. */
\r
424 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
426 static unsigned portLONG ulLastLoopCounter = 0;
\r
428 /* If the demo task is still running then we expect the loopcounter to
\r
429 have incremented since this function was last called. */
\r
430 if( ulLastLoopCounter == ulLoopCounter )
\r
432 xErrorDetected = pdTRUE;
\r
435 ulLastLoopCounter = ulLoopCounter;
\r
437 /* Errors detected in the task itself will have latched xErrorDetected
\r
440 return !xErrorDetected;
\r