2 FreeRTOS.org V4.7.1 - 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
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
45 * Tests the behaviour when data is peeked from a queue when there are
\r
46 * multiple tasks blocked on the queue.
\r
52 /* Scheduler include files. */
\r
53 #include "FreeRTOS.h"
\r
58 /* Demo program include files. */
\r
61 #define qpeekQUEUE_LENGTH ( 5 )
\r
62 #define qpeekNO_BLOCK ( 0 )
\r
63 #define qpeekSHORT_DELAY ( 10 )
\r
65 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
66 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
67 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
68 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
70 /*-----------------------------------------------------------*/
\r
73 * The following three tasks are used to demonstrate the peeking behaviour.
\r
74 * Each task is given a different priority to demonstrate the order in which
\r
75 * tasks are woken as data is peeked from a queue.
\r
77 static void prvLowPriorityPeekTask( void *pvParameters );
\r
78 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
79 static void prvHighPriorityPeekTask( void *pvParameters );
\r
80 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
82 /*-----------------------------------------------------------*/
\r
84 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
85 detected in any of the tasks. */
\r
86 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
88 /* Counter that is incremented on each cycle of a test. This is used to
\r
89 detect a stalled task - a test that is no longer running. */
\r
90 static volatile unsigned portLONG ulLoopCounter = 0;
\r
92 /* Handles to the test tasks. */
\r
93 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
94 /*-----------------------------------------------------------*/
\r
96 void vStartQueuePeekTasks( void )
\r
98 xQueueHandle xQueue;
\r
100 /* Create the queue that we are going to use for the test/demo. */
\r
101 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
103 /* Create the demo tasks and pass it the queue just created. We are
\r
104 passing the queue handle by value so it does not matter that it is declared
\r
105 on the stack here. */
\r
106 xTaskCreate( prvLowPriorityPeekTask, ( signed portCHAR * )"PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
107 xTaskCreate( prvMediumPriorityPeekTask, ( signed portCHAR * )"PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
108 xTaskCreate( prvHighPriorityPeekTask, ( signed portCHAR * )"PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
109 xTaskCreate( prvHighestPriorityPeekTask, ( signed portCHAR * )"PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
111 /*-----------------------------------------------------------*/
\r
113 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
115 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
116 unsigned portLONG ulValue;
\r
120 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
122 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
124 /* Queue a message for printing to say the task has started. */
\r
125 vPrintDisplayMessage( &pcTaskStartMsg );
\r
131 /* Try peeking from the queue. The queue should be empty so we will
\r
132 block, allowing the high priority task to execute. */
\r
133 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
135 /* We expected to have received something by the time we unblock. */
\r
136 xErrorDetected = pdTRUE;
\r
139 /* When we reach here the high and medium priority tasks should still
\r
140 be blocked on the queue. We unblocked because the low priority task
\r
141 wrote a value to the queue, which we should have peeked. Peeking the
\r
142 data (rather than receiving it) will leave the data on the queue, so
\r
143 the high priority task should then have also been unblocked, but not
\r
145 if( ulValue != 0x11223344 )
\r
147 /* We did not receive the expected value. */
\r
148 xErrorDetected = pdTRUE;
\r
151 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
153 /* The message should have been left on the queue. */
\r
154 xErrorDetected = pdTRUE;
\r
157 /* Now we are going to actually receive the data, so when the high
\r
158 priority task runs it will find the queue empty and return to the
\r
161 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
163 /* We expected to receive the value. */
\r
164 xErrorDetected = pdTRUE;
\r
167 if( ulValue != 0x11223344 )
\r
169 /* We did not receive the expected value - which should have been
\r
170 the same value as was peeked. */
\r
171 xErrorDetected = pdTRUE;
\r
174 /* Now we will block again as the queue is once more empty. The low
\r
175 priority task can then execute again. */
\r
176 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
178 /* We expected to have received something by the time we unblock. */
\r
179 xErrorDetected = pdTRUE;
\r
182 /* When we get here the low priority task should have again written to the
\r
184 if( ulValue != 0x01234567 )
\r
186 /* We did not receive the expected value. */
\r
187 xErrorDetected = pdTRUE;
\r
190 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
192 /* The message should have been left on the queue. */
\r
193 xErrorDetected = pdTRUE;
\r
196 /* We only peeked the data, so suspending ourselves now should enable
\r
197 the high priority task to also peek the data. The high priority task
\r
198 will have been unblocked when we peeked the data as we left the data
\r
200 vTaskSuspend( NULL );
\r
204 /* This time we are going to do the same as the above test, but the
\r
205 high priority task is going to receive the data, rather than peek it.
\r
206 This means that the medium priority task should never peek the value. */
\r
207 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
209 xErrorDetected = pdTRUE;
\r
212 if( ulValue != 0xaabbaabb )
\r
214 xErrorDetected = pdTRUE;
\r
217 vTaskSuspend( NULL );
\r
220 /*-----------------------------------------------------------*/
\r
222 static void prvHighPriorityPeekTask( void *pvParameters )
\r
224 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
225 unsigned portLONG ulValue;
\r
229 /* Try peeking from the queue. The queue should be empty so we will
\r
230 block, allowing the medium priority task to execute. Both the high
\r
231 and highest priority tasks will then be blocked on the queue. */
\r
232 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
234 /* We expected to have received something by the time we unblock. */
\r
235 xErrorDetected = pdTRUE;
\r
238 /* When we get here the highest priority task should have peeked the data
\r
239 (unblocking this task) then suspended (allowing this task to also peek
\r
241 if( ulValue != 0x01234567 )
\r
243 /* We did not receive the expected value. */
\r
244 xErrorDetected = pdTRUE;
\r
247 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
249 /* The message should have been left on the queue. */
\r
250 xErrorDetected = pdTRUE;
\r
253 /* We only peeked the data, so suspending ourselves now should enable
\r
254 the medium priority task to also peek the data. The medium priority task
\r
255 will have been unblocked when we peeked the data as we left the data
\r
257 vTaskSuspend( NULL );
\r
260 /* This time we are going actually receive the value, so the medium
\r
261 priority task will never peek the data - we removed it from the queue. */
\r
262 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
264 xErrorDetected = pdTRUE;
\r
267 if( ulValue != 0xaabbaabb )
\r
269 xErrorDetected = pdTRUE;
\r
272 vTaskSuspend( NULL );
\r
275 /*-----------------------------------------------------------*/
\r
277 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
279 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
280 unsigned portLONG ulValue;
\r
284 /* Try peeking from the queue. The queue should be empty so we will
\r
285 block, allowing the low priority task to execute. The highest, high
\r
286 and medium priority tasks will then all be blocked on the queue. */
\r
287 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
289 /* We expected to have received something by the time we unblock. */
\r
290 xErrorDetected = pdTRUE;
\r
293 /* When we get here the high priority task should have peeked the data
\r
294 (unblocking this task) then suspended (allowing this task to also peek
\r
296 if( ulValue != 0x01234567 )
\r
298 /* We did not receive the expected value. */
\r
299 xErrorDetected = pdTRUE;
\r
302 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
304 /* The message should have been left on the queue. */
\r
305 xErrorDetected = pdTRUE;
\r
308 /* Just so we know the test is still running. */
\r
311 /* Now we can suspend ourselves so the low priority task can execute
\r
313 vTaskSuspend( NULL );
\r
316 /*-----------------------------------------------------------*/
\r
318 static void prvLowPriorityPeekTask( void *pvParameters )
\r
320 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
321 unsigned portLONG ulValue;
\r
325 /* Write some data to the queue. This should unblock the highest
\r
326 priority task that is waiting to peek data from the queue. */
\r
327 ulValue = 0x11223344;
\r
328 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
330 /* We were expecting the queue to be empty so we should not of
\r
331 had a problem writing to the queue. */
\r
332 xErrorDetected = pdTRUE;
\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 /* All the other tasks should now have successfully peeked the data.
\r
353 The data is still in the queue so we should be able to receive it. */
\r
355 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
357 /* We expected to receive the data. */
\r
358 xErrorDetected = pdTRUE;
\r
361 if( ulValue != 0x01234567 )
\r
363 /* We did not receive the expected value. */
\r
366 /* Lets just delay a while as this is an intensive test as we don't
\r
367 want to starve other tests of processing time. */
\r
368 vTaskDelay( qpeekSHORT_DELAY );
\r
370 /* Unsuspend the other tasks so we can repeat the test - this time
\r
371 however not all the other tasks will peek the data as the high
\r
372 priority task is actually going to remove it from the queue. Send
\r
373 to front is used just to be different. As the queue is empty it
\r
374 makes no difference to the result. */
\r
375 vTaskResume( xMediumPriorityTask );
\r
376 vTaskResume( xHighPriorityTask );
\r
377 vTaskResume( xHighestPriorityTask );
\r
379 ulValue = 0xaabbaabb;
\r
380 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
382 /* We were expecting the queue to be empty so we should not of
\r
383 had a problem writing to the queue. */
\r
384 xErrorDetected = pdTRUE;
\r
387 /* This time we should find that the queue is empty. The high priority
\r
388 task actually removed the data rather than just peeking it. */
\r
389 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
391 /* We expected to receive the data. */
\r
392 xErrorDetected = pdTRUE;
\r
395 /* Unsuspend the highest and high priority tasks so we can go back
\r
396 and repeat the whole thing. The medium priority task should not be
\r
397 suspended as it was not able to peek the data in this last case. */
\r
398 vTaskResume( xHighPriorityTask );
\r
399 vTaskResume( xHighestPriorityTask );
\r
401 /* Lets just delay a while as this is an intensive test as we don't
\r
402 want to starve other tests of processing time. */
\r
403 vTaskDelay( qpeekSHORT_DELAY );
\r
406 /*-----------------------------------------------------------*/
\r
408 /* This is called to check that all the created tasks are still running. */
\r
409 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
411 static unsigned portLONG ulLastLoopCounter = 0;
\r
413 /* If the demo task is still running then we expect the loopcounter to
\r
414 have incremented since this function was last called. */
\r
415 if( ulLastLoopCounter == ulLoopCounter )
\r
417 xErrorDetected = pdTRUE;
\r
420 ulLastLoopCounter = ulLoopCounter;
\r
422 /* Errors detected in the task itself will have latched xErrorDetected
\r
425 return !xErrorDetected;
\r