2 FreeRTOS.org V4.8.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! Why not get us to quote to get FreeRTOS.org *
\r
30 * running on your hardware - or even write all or part of your application*
\r
31 * for you? See http://www.OpenRTOS.com for details. *
\r
33 ***************************************************************************
\r
34 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
51 * Tests the behaviour when data is peeked from a queue when there are
\r
52 * multiple tasks blocked on the queue.
\r
58 /* Scheduler include files. */
\r
59 #include "FreeRTOS.h"
\r
64 /* Demo program include files. */
\r
67 #define qpeekQUEUE_LENGTH ( 5 )
\r
68 #define qpeekNO_BLOCK ( 0 )
\r
69 #define qpeekSHORT_DELAY ( 10 )
\r
71 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
72 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
73 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
74 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
76 /*-----------------------------------------------------------*/
\r
79 * The following three tasks are used to demonstrate the peeking behaviour.
\r
80 * Each task is given a different priority to demonstrate the order in which
\r
81 * tasks are woken as data is peeked from a queue.
\r
83 static void prvLowPriorityPeekTask( void *pvParameters );
\r
84 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
85 static void prvHighPriorityPeekTask( void *pvParameters );
\r
86 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
88 /*-----------------------------------------------------------*/
\r
90 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
91 detected in any of the tasks. */
\r
92 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
94 /* Counter that is incremented on each cycle of a test. This is used to
\r
95 detect a stalled task - a test that is no longer running. */
\r
96 static volatile unsigned portLONG ulLoopCounter = 0;
\r
98 /* Handles to the test tasks. */
\r
99 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
100 /*-----------------------------------------------------------*/
\r
102 void vStartQueuePeekTasks( void )
\r
104 xQueueHandle xQueue;
\r
106 /* Create the queue that we are going to use for the test/demo. */
\r
107 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
109 /* Create the demo tasks and pass it the queue just created. We are
\r
110 passing the queue handle by value so it does not matter that it is declared
\r
111 on the stack here. */
\r
112 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
113 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
114 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
115 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
117 /*-----------------------------------------------------------*/
\r
119 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
121 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
122 unsigned portLONG ulValue;
\r
126 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
128 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
130 /* Queue a message for printing to say the task has started. */
\r
131 vPrintDisplayMessage( &pcTaskStartMsg );
\r
137 /* Try peeking from the queue. The queue should be empty so we will
\r
138 block, allowing the high priority task to execute. */
\r
139 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
141 /* We expected to have received something by the time we unblock. */
\r
142 xErrorDetected = pdTRUE;
\r
145 /* When we reach here the high and medium priority tasks should still
\r
146 be blocked on the queue. We unblocked because the low priority task
\r
147 wrote a value to the queue, which we should have peeked. Peeking the
\r
148 data (rather than receiving it) will leave the data on the queue, so
\r
149 the high priority task should then have also been unblocked, but not
\r
151 if( ulValue != 0x11223344 )
\r
153 /* We did not receive the expected value. */
\r
154 xErrorDetected = pdTRUE;
\r
157 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
159 /* The message should have been left on the queue. */
\r
160 xErrorDetected = pdTRUE;
\r
163 /* Now we are going to actually receive the data, so when the high
\r
164 priority task runs it will find the queue empty and return to the
\r
167 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
169 /* We expected to receive the value. */
\r
170 xErrorDetected = pdTRUE;
\r
173 if( ulValue != 0x11223344 )
\r
175 /* We did not receive the expected value - which should have been
\r
176 the same value as was peeked. */
\r
177 xErrorDetected = pdTRUE;
\r
180 /* Now we will block again as the queue is once more empty. The low
\r
181 priority task can then execute again. */
\r
182 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
184 /* We expected to have received something by the time we unblock. */
\r
185 xErrorDetected = pdTRUE;
\r
188 /* When we get here the low priority task should have again written to the
\r
190 if( ulValue != 0x01234567 )
\r
192 /* We did not receive the expected value. */
\r
193 xErrorDetected = pdTRUE;
\r
196 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
198 /* The message should have been left on the queue. */
\r
199 xErrorDetected = pdTRUE;
\r
202 /* We only peeked the data, so suspending ourselves now should enable
\r
203 the high priority task to also peek the data. The high priority task
\r
204 will have been unblocked when we peeked the data as we left the data
\r
206 vTaskSuspend( NULL );
\r
210 /* This time we are going to do the same as the above test, but the
\r
211 high priority task is going to receive the data, rather than peek it.
\r
212 This means that the medium priority task should never peek the value. */
\r
213 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
215 xErrorDetected = pdTRUE;
\r
218 if( ulValue != 0xaabbaabb )
\r
220 xErrorDetected = pdTRUE;
\r
223 vTaskSuspend( NULL );
\r
226 /*-----------------------------------------------------------*/
\r
228 static void prvHighPriorityPeekTask( void *pvParameters )
\r
230 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
231 unsigned portLONG ulValue;
\r
235 /* Try peeking from the queue. The queue should be empty so we will
\r
236 block, allowing the medium priority task to execute. Both the high
\r
237 and highest priority tasks will then be blocked on the queue. */
\r
238 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
240 /* We expected to have received something by the time we unblock. */
\r
241 xErrorDetected = pdTRUE;
\r
244 /* When we get here the highest priority task should have peeked the data
\r
245 (unblocking this task) then suspended (allowing this task to also peek
\r
247 if( ulValue != 0x01234567 )
\r
249 /* We did not receive the expected value. */
\r
250 xErrorDetected = pdTRUE;
\r
253 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
255 /* The message should have been left on the queue. */
\r
256 xErrorDetected = pdTRUE;
\r
259 /* We only peeked the data, so suspending ourselves now should enable
\r
260 the medium priority task to also peek the data. The medium priority task
\r
261 will have been unblocked when we peeked the data as we left the data
\r
263 vTaskSuspend( NULL );
\r
266 /* This time we are going actually receive the value, so the medium
\r
267 priority task will never peek the data - we removed it from the queue. */
\r
268 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
270 xErrorDetected = pdTRUE;
\r
273 if( ulValue != 0xaabbaabb )
\r
275 xErrorDetected = pdTRUE;
\r
278 vTaskSuspend( NULL );
\r
281 /*-----------------------------------------------------------*/
\r
283 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
285 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
286 unsigned portLONG ulValue;
\r
290 /* Try peeking from the queue. The queue should be empty so we will
\r
291 block, allowing the low priority task to execute. The highest, high
\r
292 and medium priority tasks will then all be blocked on the queue. */
\r
293 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
295 /* We expected to have received something by the time we unblock. */
\r
296 xErrorDetected = pdTRUE;
\r
299 /* When we get here the high priority task should have peeked the data
\r
300 (unblocking this task) then suspended (allowing this task to also peek
\r
302 if( ulValue != 0x01234567 )
\r
304 /* We did not receive the expected value. */
\r
305 xErrorDetected = pdTRUE;
\r
308 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
310 /* The message should have been left on the queue. */
\r
311 xErrorDetected = pdTRUE;
\r
314 /* Just so we know the test is still running. */
\r
317 /* Now we can suspend ourselves so the low priority task can execute
\r
319 vTaskSuspend( NULL );
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvLowPriorityPeekTask( void *pvParameters )
\r
326 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
327 unsigned portLONG ulValue;
\r
331 /* Write some data to the queue. This should unblock the highest
\r
332 priority task that is waiting to peek data from the queue. */
\r
333 ulValue = 0x11223344;
\r
334 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
336 /* We were expecting the queue to be empty so we should not of
\r
337 had a problem writing to the queue. */
\r
338 xErrorDetected = pdTRUE;
\r
341 /* By the time we get here the data should have been removed from
\r
343 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
345 xErrorDetected = pdTRUE;
\r
348 /* Write another value to the queue, again waking the highest priority
\r
349 task that is blocked on the queue. */
\r
350 ulValue = 0x01234567;
\r
351 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
353 /* We were expecting the queue to be empty so we should not of
\r
354 had a problem writing to the queue. */
\r
355 xErrorDetected = pdTRUE;
\r
358 /* All the other tasks should now have successfully peeked the data.
\r
359 The data is still in the queue so we should be able to receive it. */
\r
361 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
363 /* We expected to receive the data. */
\r
364 xErrorDetected = pdTRUE;
\r
367 if( ulValue != 0x01234567 )
\r
369 /* We did not receive the expected value. */
\r
372 /* Lets just delay a while as this is an intensive test as we don't
\r
373 want to starve other tests of processing time. */
\r
374 vTaskDelay( qpeekSHORT_DELAY );
\r
376 /* Unsuspend the other tasks so we can repeat the test - this time
\r
377 however not all the other tasks will peek the data as the high
\r
378 priority task is actually going to remove it from the queue. Send
\r
379 to front is used just to be different. As the queue is empty it
\r
380 makes no difference to the result. */
\r
381 vTaskResume( xMediumPriorityTask );
\r
382 vTaskResume( xHighPriorityTask );
\r
383 vTaskResume( xHighestPriorityTask );
\r
385 ulValue = 0xaabbaabb;
\r
386 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
388 /* We were expecting the queue to be empty so we should not of
\r
389 had a problem writing to the queue. */
\r
390 xErrorDetected = pdTRUE;
\r
393 /* This time we should find that the queue is empty. The high priority
\r
394 task actually removed the data rather than just peeking it. */
\r
395 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
397 /* We expected to receive the data. */
\r
398 xErrorDetected = pdTRUE;
\r
401 /* Unsuspend the highest and high priority tasks so we can go back
\r
402 and repeat the whole thing. The medium priority task should not be
\r
403 suspended as it was not able to peek the data in this last case. */
\r
404 vTaskResume( xHighPriorityTask );
\r
405 vTaskResume( xHighestPriorityTask );
\r
407 /* Lets just delay a while as this is an intensive test as we don't
\r
408 want to starve other tests of processing time. */
\r
409 vTaskDelay( qpeekSHORT_DELAY );
\r
412 /*-----------------------------------------------------------*/
\r
414 /* This is called to check that all the created tasks are still running. */
\r
415 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
417 static unsigned portLONG ulLastLoopCounter = 0;
\r
419 /* If the demo task is still running then we expect the loopcounter to
\r
420 have incremented since this function was last called. */
\r
421 if( ulLastLoopCounter == ulLoopCounter )
\r
423 xErrorDetected = pdTRUE;
\r
426 ulLastLoopCounter = ulLoopCounter;
\r
428 /* Errors detected in the task itself will have latched xErrorDetected
\r
431 return !xErrorDetected;
\r