2 FreeRTOS.org V4.5.0 - 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 for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
38 * Tests the behaviour when data is peeked from a queue when there are
\r
39 * multiple tasks blocked on the queue.
\r
45 /* Scheduler include files. */
\r
46 #include "FreeRTOS.h"
\r
51 /* Demo program include files. */
\r
54 #define qpeekQUEUE_LENGTH ( 5 )
\r
55 #define qpeekNO_BLOCK ( 0 )
\r
56 #define qpeekSHORT_DELAY ( 10 )
\r
58 #define qpeekLOW_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
59 #define qpeekMEDIUM_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
60 #define qpeekHIGH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
61 #define qpeekHIGHEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
63 /*-----------------------------------------------------------*/
\r
66 * The following three tasks are used to demonstrate the peeking behaviour.
\r
67 * Each task is given a different priority to demonstrate the order in which
\r
68 * tasks are woken as data is peeked from a queue.
\r
70 static void prvLowPriorityPeekTask( void *pvParameters );
\r
71 static void prvMediumPriorityPeekTask( void *pvParameters );
\r
72 static void prvHighPriorityPeekTask( void *pvParameters );
\r
73 static void prvHighestPriorityPeekTask( void *pvParameters );
\r
75 /*-----------------------------------------------------------*/
\r
77 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
78 detected in any of the tasks. */
\r
79 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
81 /* Counter that is incremented on each cycle of a test. This is used to
\r
82 detect a stalled task - a test that is no longer running. */
\r
83 static volatile unsigned portLONG ulLoopCounter = 0;
\r
85 /* Handles to the test tasks. */
\r
86 xTaskHandle xMediumPriorityTask, xHighPriorityTask, xHighestPriorityTask;
\r
87 /*-----------------------------------------------------------*/
\r
89 void vStartQueuePeekTasks( void )
\r
91 xQueueHandle xQueue;
\r
93 /* Create the queue that we are going to use for the test/demo. */
\r
94 xQueue = xQueueCreate( qpeekQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
96 /* Create the demo tasks and pass it the queue just created. We are
\r
97 passing the queue handle by value so it does not matter that it is declared
\r
98 on the stack here. */
\r
99 xTaskCreate( prvLowPriorityPeekTask, "PeekL", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekLOW_PRIORITY, NULL );
\r
100 xTaskCreate( prvMediumPriorityPeekTask, "PeekM", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekMEDIUM_PRIORITY, &xMediumPriorityTask );
\r
101 xTaskCreate( prvHighPriorityPeekTask, "PeekH1", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGH_PRIORITY, &xHighPriorityTask );
\r
102 xTaskCreate( prvHighestPriorityPeekTask, "PeekH2", configMINIMAL_STACK_SIZE, ( void * ) xQueue, qpeekHIGHEST_PRIORITY, &xHighestPriorityTask );
\r
104 /*-----------------------------------------------------------*/
\r
106 static void prvHighestPriorityPeekTask( void *pvParameters )
\r
108 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
109 unsigned portLONG ulValue;
\r
113 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
115 const portCHAR * const pcTaskStartMsg = "Queue peek test started.\r\n";
\r
117 /* Queue a message for printing to say the task has started. */
\r
118 vPrintDisplayMessage( &pcTaskStartMsg );
\r
124 /* Try peeking from the queue. The queue should be empty so we will
\r
125 block, allowing the high priority task to execute. */
\r
126 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
128 /* We expected to have received something by the time we unblock. */
\r
129 xErrorDetected = pdTRUE;
\r
132 /* When we reach here the high and medium priority tasks should still
\r
133 be blocked on the queue. We unblocked because the low priority task
\r
134 wrote a value to the queue, which we should have peeked. Peeking the
\r
135 data (rather than receiving it) will leave the data on the queue, so
\r
136 the high priority task should then have also been unblocked, but not
\r
138 if( ulValue != 0x11223344 )
\r
140 /* We did not receive the expected value. */
\r
141 xErrorDetected = pdTRUE;
\r
144 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
146 /* The message should have been left on the queue. */
\r
147 xErrorDetected = pdTRUE;
\r
150 /* Now we are going to actually receive the data, so when the high
\r
151 priority task runs it will find the queue empty and return to the
\r
154 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
156 /* We expected to receive the value. */
\r
157 xErrorDetected = pdTRUE;
\r
160 if( ulValue != 0x11223344 )
\r
162 /* We did not receive the expected value - which should have been
\r
163 the same value as was peeked. */
\r
164 xErrorDetected = pdTRUE;
\r
167 /* Now we will block again as the queue is once more empty. The low
\r
168 priority task can then execute again. */
\r
169 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
171 /* We expected to have received something by the time we unblock. */
\r
172 xErrorDetected = pdTRUE;
\r
175 /* When we get here the low priority task should have again written to the
\r
177 if( ulValue != 0x01234567 )
\r
179 /* We did not receive the expected value. */
\r
180 xErrorDetected = pdTRUE;
\r
183 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
185 /* The message should have been left on the queue. */
\r
186 xErrorDetected = pdTRUE;
\r
189 /* We only peeked the data, so suspending ourselves now should enable
\r
190 the high priority task to also peek the data. The high priority task
\r
191 will have been unblocked when we peeked the data as we left the data
\r
193 vTaskSuspend( NULL );
\r
197 /* This time we are going to do the same as the above test, but the
\r
198 high priority task is going to receive the data, rather than peek it.
\r
199 This means that the medium priority task should never peek the value. */
\r
200 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
202 xErrorDetected = pdTRUE;
\r
205 if( ulValue != 0xaabbaabb )
\r
207 xErrorDetected = pdTRUE;
\r
210 vTaskSuspend( NULL );
\r
213 /*-----------------------------------------------------------*/
\r
215 static void prvHighPriorityPeekTask( void *pvParameters )
\r
217 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
218 unsigned portLONG ulValue;
\r
222 /* Try peeking from the queue. The queue should be empty so we will
\r
223 block, allowing the medium priority task to execute. Both the high
\r
224 and highest priority tasks will then be blocked on the queue. */
\r
225 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
227 /* We expected to have received something by the time we unblock. */
\r
228 xErrorDetected = pdTRUE;
\r
231 /* When we get here the highest priority task should have peeked the data
\r
232 (unblocking this task) then suspended (allowing this task to also peek
\r
234 if( ulValue != 0x01234567 )
\r
236 /* We did not receive the expected value. */
\r
237 xErrorDetected = pdTRUE;
\r
240 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
242 /* The message should have been left on the queue. */
\r
243 xErrorDetected = pdTRUE;
\r
246 /* We only peeked the data, so suspending ourselves now should enable
\r
247 the medium priority task to also peek the data. The medium priority task
\r
248 will have been unblocked when we peeked the data as we left the data
\r
250 vTaskSuspend( NULL );
\r
253 /* This time we are going actually receive the value, so the medium
\r
254 priority task will never peek the data - we removed it from the queue. */
\r
255 if( xQueueReceive( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
257 xErrorDetected = pdTRUE;
\r
260 if( ulValue != 0xaabbaabb )
\r
262 xErrorDetected = pdTRUE;
\r
265 vTaskSuspend( NULL );
\r
268 /*-----------------------------------------------------------*/
\r
270 static void prvMediumPriorityPeekTask( void *pvParameters )
\r
272 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
273 unsigned portLONG ulValue;
\r
277 /* Try peeking from the queue. The queue should be empty so we will
\r
278 block, allowing the low priority task to execute. The highest, high
\r
279 and medium priority tasks will then all be blocked on the queue. */
\r
280 if( xQueuePeek( xQueue, &ulValue, portMAX_DELAY ) != pdPASS )
\r
282 /* We expected to have received something by the time we unblock. */
\r
283 xErrorDetected = pdTRUE;
\r
286 /* When we get here the high priority task should have peeked the data
\r
287 (unblocking this task) then suspended (allowing this task to also peek
\r
289 if( ulValue != 0x01234567 )
\r
291 /* We did not receive the expected value. */
\r
292 xErrorDetected = pdTRUE;
\r
295 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
297 /* The message should have been left on the queue. */
\r
298 xErrorDetected = pdTRUE;
\r
301 /* Just so we know the test is still running. */
\r
304 /* Now we can suspend ourselves so the low priority task can execute
\r
306 vTaskSuspend( NULL );
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvLowPriorityPeekTask( void *pvParameters )
\r
313 xQueueHandle xQueue = ( xQueueHandle ) pvParameters;
\r
314 unsigned portLONG ulValue;
\r
318 /* Write some data to the queue. This should unblock the highest
\r
319 priority task that is waiting to peek data from the queue. */
\r
320 ulValue = 0x11223344;
\r
321 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
323 /* We were expecting the queue to be empty so we should not of
\r
324 had a problem writing to the queue. */
\r
325 xErrorDetected = pdTRUE;
\r
328 /* By the time we get here the data should have been removed from
\r
330 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
332 xErrorDetected = pdTRUE;
\r
335 /* Write another value to the queue, again waking the highest priority
\r
336 task that is blocked on the queue. */
\r
337 ulValue = 0x01234567;
\r
338 if( xQueueSendToBack( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
340 /* We were expecting the queue to be empty so we should not of
\r
341 had a problem writing to the queue. */
\r
342 xErrorDetected = pdTRUE;
\r
345 /* All the other tasks should now have successfully peeked the data.
\r
346 The data is still in the queue so we should be able to receive it. */
\r
348 if( xQueueReceive( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
350 /* We expected to receive the data. */
\r
351 xErrorDetected = pdTRUE;
\r
354 if( ulValue != 0x01234567 )
\r
356 /* We did not receive the expected value. */
\r
359 /* Lets just delay a while as this is an intensive test as we don't
\r
360 want to starve other tests of processing time. */
\r
361 vTaskDelay( qpeekSHORT_DELAY );
\r
363 /* Unsuspend the other tasks so we can repeat the test - this time
\r
364 however not all the other tasks will peek the data as the high
\r
365 priority task is actually going to remove it from the queue. Send
\r
366 to front is used just to be different. As the queue is empty it
\r
367 makes no difference to the result. */
\r
368 vTaskResume( xMediumPriorityTask );
\r
369 vTaskResume( xHighPriorityTask );
\r
370 vTaskResume( xHighestPriorityTask );
\r
372 ulValue = 0xaabbaabb;
\r
373 if( xQueueSendToFront( xQueue, &ulValue, qpeekNO_BLOCK ) != pdPASS )
\r
375 /* We were expecting the queue to be empty so we should not of
\r
376 had a problem writing to the queue. */
\r
377 xErrorDetected = pdTRUE;
\r
380 /* This time we should find that the queue is empty. The high priority
\r
381 task actually removed the data rather than just peeking it. */
\r
382 if( xQueuePeek( xQueue, &ulValue, qpeekNO_BLOCK ) != errQUEUE_EMPTY )
\r
384 /* We expected to receive the data. */
\r
385 xErrorDetected = pdTRUE;
\r
388 /* Unsuspend the highest and high priority tasks so we can go back
\r
389 and repeat the whole thing. The medium priority task should not be
\r
390 suspended as it was not able to peek the data in this last case. */
\r
391 vTaskResume( xHighPriorityTask );
\r
392 vTaskResume( xHighestPriorityTask );
\r
394 /* Lets just delay a while as this is an intensive test as we don't
\r
395 want to starve other tests of processing time. */
\r
396 vTaskDelay( qpeekSHORT_DELAY );
\r
399 /*-----------------------------------------------------------*/
\r
401 /* This is called to check that all the created tasks are still running. */
\r
402 portBASE_TYPE xAreQueuePeekTasksStillRunning( void )
\r
404 static unsigned portLONG ulLastLoopCounter = 0;
\r
406 /* If the demo task is still running then we expect the loopcounter to
\r
407 have incremented since this function was last called. */
\r
408 if( ulLastLoopCounter == ulLoopCounter )
\r
410 xErrorDetected = pdTRUE;
\r
413 ulLastLoopCounter = ulLoopCounter;
\r
415 /* Errors detected in the task itself will have latched xErrorDetected
\r
418 return !xErrorDetected;
\r