2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
39 * Tests the behaviour when data is peeked from a queue when there are
\r
40 * multiple tasks blocked on the queue.
\r
46 /* Scheduler include files. */
\r
47 #include "FreeRTOS.h"
\r
52 /* Demo program include files. */
\r
55 #define qpeekQUEUE_LENGTH ( 5 )
\r
56 #define qpeekNO_BLOCK ( 0 )
\r
57 #define qpeekSHORT_DELAY ( 10 )
\r
59 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
60 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
61 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
62 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
64 /*-----------------------------------------------------------*/
\r
67 * The following three tasks are used to demonstrate the peeking behaviour.
\r
68 * Each task is given a different priority to demonstrate the order in which
\r
69 * tasks are woken as data is peeked from a queue.
\r
71 static void prvLowPriorityPeekTask( void *pvParameters );
\r
72 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
73 static void prvHighPriorityPeekTask( void *pvParameters );
\r
74 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
76 /*-----------------------------------------------------------*/
\r
78 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
79 detected in any of the tasks. */
\r
80 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
82 /* Counter that is incremented on each cycle of a test. This is used to
\r
83 detect a stalled task - a test that is no longer running. */
\r
84 static volatile unsigned portLONG ulLoopCounter = 0;
\r
86 /* Handles to the test tasks. */
\r
87 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
88 /*-----------------------------------------------------------*/
\r
90 void vStartQueuePeekTasks( void )
\r
92 xQueueHandle xQueue;
\r
94 /* Create the queue that we are going to use for the test/demo. */
\r
95 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
97 /* Create the demo tasks and pass it the queue just created. We are
\r
98 passing the queue handle by value so it does not matter that it is declared
\r
99 on the stack here. */
\r
100 xTaskCreate( prvLowPriorityPeekTask, "PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
101 xTaskCreate( prvMediumPriorityPeekTask, "PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
102 xTaskCreate( prvHighPriorityPeekTask, "PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
103 xTaskCreate( prvHighestPriorityPeekTask, "PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
105 /*-----------------------------------------------------------*/
\r
107 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
109 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
110 unsigned portLONG ulValue;
\r
114 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
116 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
118 /* Queue a message for printing to say the task has started. */
\r
119 vPrintDisplayMessage( &pcTaskStartMsg );
\r
125 /* Try peeking from the queue. The queue should be empty so we will
\r
126 block, allowing the high priority task to execute. */
\r
127 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
129 /* We expected to have received something by the time we unblock. */
\r
130 xErrorDetected = pdTRUE;
\r
133 /* When we reach here the high and medium priority tasks should still
\r
134 be blocked on the queue. We unblocked because the low priority task
\r
135 wrote a value to the queue, which we should have peeked. Peeking the
\r
136 data (rather than receiving it) will leave the data on the queue, so
\r
137 the high priority task should then have also been unblocked, but not
\r
139 if( ulValue != 0x11223344 )
\r
141 /* We did not receive the expected value. */
\r
142 xErrorDetected = pdTRUE;
\r
145 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
147 /* The message should have been left on the queue. */
\r
148 xErrorDetected = pdTRUE;
\r
151 /* Now we are going to actually receive the data, so when the high
\r
152 priority task runs it will find the queue empty and return to the
\r
155 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
157 /* We expected to receive the value. */
\r
158 xErrorDetected = pdTRUE;
\r
161 if( ulValue != 0x11223344 )
\r
163 /* We did not receive the expected value - which should have been
\r
164 the same value as was peeked. */
\r
165 xErrorDetected = pdTRUE;
\r
168 /* Now we will block again as the queue is once more empty. The low
\r
169 priority task can then execute again. */
\r
170 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
172 /* We expected to have received something by the time we unblock. */
\r
173 xErrorDetected = pdTRUE;
\r
176 /* When we get here the low priority task should have again written to the
\r
178 if( ulValue != 0x01234567 )
\r
180 /* We did not receive the expected value. */
\r
181 xErrorDetected = pdTRUE;
\r
184 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
186 /* The message should have been left on the queue. */
\r
187 xErrorDetected = pdTRUE;
\r
190 /* We only peeked the data, so suspending ourselves now should enable
\r
191 the high priority task to also peek the data. The high priority task
\r
192 will have been unblocked when we peeked the data as we left the data
\r
194 vTaskSuspend( NULL );
\r
198 /* This time we are going to do the same as the above test, but the
\r
199 high priority task is going to receive the data, rather than peek it.
\r
200 This means that the medium priority task should never peek the value. */
\r
201 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
203 xErrorDetected = pdTRUE;
\r
206 if( ulValue != 0xaabbaabb )
\r
208 xErrorDetected = pdTRUE;
\r
211 vTaskSuspend( NULL );
\r
214 /*-----------------------------------------------------------*/
\r
216 static void prvHighPriorityPeekTask( void *pvParameters )
\r
218 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
219 unsigned portLONG ulValue;
\r
223 /* Try peeking from the queue. The queue should be empty so we will
\r
224 block, allowing the medium priority task to execute. Both the high
\r
225 and highest priority tasks will then be blocked on the queue. */
\r
226 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
228 /* We expected to have received something by the time we unblock. */
\r
229 xErrorDetected = pdTRUE;
\r
232 /* When we get here the highest priority task should have peeked the data
\r
233 (unblocking this task) then suspended (allowing this task to also peek
\r
235 if( ulValue != 0x01234567 )
\r
237 /* We did not receive the expected value. */
\r
238 xErrorDetected = pdTRUE;
\r
241 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
243 /* The message should have been left on the queue. */
\r
244 xErrorDetected = pdTRUE;
\r
247 /* We only peeked the data, so suspending ourselves now should enable
\r
248 the medium priority task to also peek the data. The medium priority task
\r
249 will have been unblocked when we peeked the data as we left the data
\r
251 vTaskSuspend( NULL );
\r
254 /* This time we are going actually receive the value, so the medium
\r
255 priority task will never peek the data - we removed it from the queue. */
\r
256 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
258 xErrorDetected = pdTRUE;
\r
261 if( ulValue != 0xaabbaabb )
\r
263 xErrorDetected = pdTRUE;
\r
266 vTaskSuspend( NULL );
\r
269 /*-----------------------------------------------------------*/
\r
271 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
273 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
274 unsigned portLONG ulValue;
\r
278 /* Try peeking from the queue. The queue should be empty so we will
\r
279 block, allowing the low priority task to execute. The highest, high
\r
280 and medium priority tasks will then all be blocked on the queue. */
\r
281 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
283 /* We expected to have received something by the time we unblock. */
\r
284 xErrorDetected = pdTRUE;
\r
287 /* When we get here the high priority task should have peeked the data
\r
288 (unblocking this task) then suspended (allowing this task to also peek
\r
290 if( ulValue != 0x01234567 )
\r
292 /* We did not receive the expected value. */
\r
293 xErrorDetected = pdTRUE;
\r
296 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
298 /* The message should have been left on the queue. */
\r
299 xErrorDetected = pdTRUE;
\r
302 /* Just so we know the test is still running. */
\r
305 /* Now we can suspend ourselves so the low priority task can execute
\r
307 vTaskSuspend( NULL );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvLowPriorityPeekTask( void *pvParameters )
\r
314 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
315 unsigned portLONG ulValue;
\r
319 /* Write some data to the queue. This should unblock the highest
\r
320 priority task that is waiting to peek data from the queue. */
\r
321 ulValue = 0x11223344;
\r
322 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
324 /* We were expecting the queue to be empty so we should not of
\r
325 had a problem writing to the queue. */
\r
326 xErrorDetected = pdTRUE;
\r
329 /* By the time we get here the data should have been removed from
\r
331 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
333 xErrorDetected = pdTRUE;
\r
336 /* Write another value to the queue, again waking the highest priority
\r
337 task that is blocked on the queue. */
\r
338 ulValue = 0x01234567;
\r
339 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
341 /* We were expecting the queue to be empty so we should not of
\r
342 had a problem writing to the queue. */
\r
343 xErrorDetected = pdTRUE;
\r
346 /* All the other tasks should now have successfully peeked the data.
\r
347 The data is still in the queue so we should be able to receive it. */
\r
349 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
351 /* We expected to receive the data. */
\r
352 xErrorDetected = pdTRUE;
\r
355 if( ulValue != 0x01234567 )
\r
357 /* We did not receive the expected value. */
\r
360 /* Lets just delay a while as this is an intensive test as we don't
\r
361 want to starve other tests of processing time. */
\r
362 vTaskDelay( qpeekSHORT_DELAY );
\r
364 /* Unsuspend the other tasks so we can repeat the test - this time
\r
365 however not all the other tasks will peek the data as the high
\r
366 priority task is actually going to remove it from the queue. Send
\r
367 to front is used just to be different. As the queue is empty it
\r
368 makes no difference to the result. */
\r
369 vTaskResume( xMediumPriorityTask );
\r
370 vTaskResume( xHighPriorityTask );
\r
371 vTaskResume( xHighestPriorityTask );
\r
373 ulValue = 0xaabbaabb;
\r
374 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
376 /* We were expecting the queue to be empty so we should not of
\r
377 had a problem writing to the queue. */
\r
378 xErrorDetected = pdTRUE;
\r
381 /* This time we should find that the queue is empty. The high priority
\r
382 task actually removed the data rather than just peeking it. */
\r
383 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
385 /* We expected to receive the data. */
\r
386 xErrorDetected = pdTRUE;
\r
389 /* Unsuspend the highest and high priority tasks so we can go back
\r
390 and repeat the whole thing. The medium priority task should not be
\r
391 suspended as it was not able to peek the data in this last case. */
\r
392 vTaskResume( xHighPriorityTask );
\r
393 vTaskResume( xHighestPriorityTask );
\r
395 /* Lets just delay a while as this is an intensive test as we don't
\r
396 want to starve other tests of processing time. */
\r
397 vTaskDelay( qpeekSHORT_DELAY );
\r
400 /*-----------------------------------------------------------*/
\r
402 /* This is called to check that all the created tasks are still running. */
\r
403 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
405 static unsigned portLONG ulLastLoopCounter = 0;
\r
407 /* If the demo task is still running then we expect the loopcounter to
\r
408 have incremented since this function was last called. */
\r
409 if( ulLastLoopCounter == ulLoopCounter )
\r
411 xErrorDetected = pdTRUE;
\r
414 ulLastLoopCounter = ulLoopCounter;
\r
416 /* Errors detected in the task itself will have latched xErrorDetected
\r
419 return !xErrorDetected;
\r