]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/GenQTest.c
Common demo tasks:
[freertos] / FreeRTOS / Demo / Common / Minimal / GenQTest.c
1 /*\r
2     FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /*\r
68  * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -\r
69  * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and\r
70  * mutex behaviour.\r
71  *\r
72  * See the comments above the prvSendFrontAndBackTest() and\r
73  * prvLowPriorityMutexTask() prototypes below for more information.\r
74  */\r
75 \r
76 \r
77 #include <stdlib.h>\r
78 \r
79 /* Scheduler include files. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 #include "queue.h"\r
83 #include "semphr.h"\r
84 \r
85 /* Demo program include files. */\r
86 #include "GenQTest.h"\r
87 \r
88 #define genqQUEUE_LENGTH                ( 5 )\r
89 #define genqNO_BLOCK                    ( 0 )\r
90 \r
91 #define genqMUTEX_LOW_PRIORITY          ( tskIDLE_PRIORITY )\r
92 #define genqMUTEX_TEST_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
93 #define genqMUTEX_MEDIUM_PRIORITY       ( tskIDLE_PRIORITY + 2 )\r
94 #define genqMUTEX_HIGH_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /*\r
99  * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()\r
100  * macros by using both to fill a queue, then reading from the queue to\r
101  * check the resultant queue order is as expected.  Queue data is also\r
102  * peeked.\r
103  */\r
104 static void prvSendFrontAndBackTest( void *pvParameters );\r
105 \r
106 /*\r
107  * The following three tasks are used to demonstrate the mutex behaviour.\r
108  * Each task is given a different priority to demonstrate the priority\r
109  * inheritance mechanism.\r
110  *\r
111  * The low priority task obtains a mutex.  After this a high priority task\r
112  * attempts to obtain the same mutex, causing its priority to be inherited\r
113  * by the low priority task.  The task with the inherited high priority then\r
114  * resumes a medium priority task to ensure it is not blocked by the medium\r
115  * priority task while it holds the inherited high priority.  Once the mutex\r
116  * is returned the task with the inherited priority returns to its original\r
117  * low priority, and is therefore immediately preempted by first the high\r
118  * priority task and then the medium prioroity task before it can continue.\r
119  */\r
120 static void prvLowPriorityMutexTask( void *pvParameters );\r
121 static void prvMediumPriorityMutexTask( void *pvParameters );\r
122 static void prvHighPriorityMutexTask( void *pvParameters );\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
127 detected in any of the tasks. */\r
128 static volatile BaseType_t xErrorDetected = pdFALSE;\r
129 \r
130 /* Counters that are incremented on each cycle of a test.  This is used to\r
131 detect a stalled task - a test that is no longer running. */\r
132 static volatile uint32_t ulLoopCounter = 0;\r
133 static volatile uint32_t ulLoopCounter2 = 0;\r
134 \r
135 /* The variable that is guarded by the mutex in the mutex demo tasks. */\r
136 static volatile uint32_t ulGuardedVariable = 0;\r
137 \r
138 /* Handles used in the mutext test to suspend and resume the high and medium\r
139 priority mutex test tasks. */\r
140 static TaskHandle_t xHighPriorityMutexTask, xMediumPriorityMutexTask;\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 void vStartGenericQueueTasks( UBaseType_t uxPriority )\r
145 {\r
146 QueueHandle_t xQueue;\r
147 SemaphoreHandle_t xMutex;\r
148 \r
149         /* Create the queue that we are going to use for the\r
150         prvSendFrontAndBackTest demo. */\r
151         xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( uint32_t ) );\r
152 \r
153         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
154         in use.  The queue registry is provided as a means for kernel aware\r
155         debuggers to locate queues and has no purpose if a kernel aware debugger\r
156         is not being used.  The call to vQueueAddToRegistry() will be removed\r
157         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
158         defined to be less than 1. */\r
159         vQueueAddToRegistry( xQueue, "Gen_Queue_Test" );\r
160 \r
161         /* Create the demo task and pass it the queue just created.  We are\r
162         passing the queue handle by value so it does not matter that it is\r
163         declared on the stack here. */\r
164         xTaskCreate( prvSendFrontAndBackTest, "GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );\r
165 \r
166         /* Create the mutex used by the prvMutexTest task. */\r
167         xMutex = xSemaphoreCreateMutex();\r
168 \r
169         /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
170         in use.  The registry is provided as a means for kernel aware\r
171         debuggers to locate mutexes and has no purpose if a kernel aware debugger\r
172         is not being used.  The call to vQueueAddToRegistry() will be removed\r
173         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
174         defined to be less than 1. */\r
175         vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Gen_Queue_Mutex" );\r
176 \r
177         /* Create the mutex demo tasks and pass it the mutex just created.  We are\r
178         passing the mutex handle by value so it does not matter that it is declared\r
179         on the stack here. */\r
180         xTaskCreate( prvLowPriorityMutexTask, "MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );\r
181         xTaskCreate( prvMediumPriorityMutexTask, "MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );\r
182         xTaskCreate( prvHighPriorityMutexTask, "MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static void prvSendFrontAndBackTest( void *pvParameters )\r
187 {\r
188 uint32_t ulData, ulData2;\r
189 QueueHandle_t xQueue;\r
190 \r
191         #ifdef USE_STDIO\r
192         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
193 \r
194                 const char * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";\r
195 \r
196                 /* Queue a message for printing to say the task has started. */\r
197                 vPrintDisplayMessage( &pcTaskStartMsg );\r
198         #endif\r
199 \r
200         xQueue = ( QueueHandle_t ) pvParameters;\r
201 \r
202         for( ;; )\r
203         {\r
204                 /* The queue is empty, so sending an item to the back of the queue\r
205                 should have the same efect as sending it to the front of the queue.\r
206 \r
207                 First send to the front and check everything is as expected. */\r
208                 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );\r
209 \r
210                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
211                 {\r
212                         xErrorDetected = pdTRUE;\r
213                 }\r
214 \r
215                 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )\r
216                 {\r
217                         xErrorDetected = pdTRUE;\r
218                 }\r
219 \r
220                 /* The data we sent to the queue should equal the data we just received\r
221                 from the queue. */\r
222                 if( ulLoopCounter != ulData )\r
223                 {\r
224                         xErrorDetected = pdTRUE;\r
225                 }\r
226 \r
227                 /* Then do the same, sending the data to the back, checking everything\r
228                 is as expected. */\r
229                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
230                 {\r
231                         xErrorDetected = pdTRUE;\r
232                 }\r
233 \r
234                 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );\r
235 \r
236                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
237                 {\r
238                         xErrorDetected = pdTRUE;\r
239                 }\r
240 \r
241                 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )\r
242                 {\r
243                         xErrorDetected = pdTRUE;\r
244                 }\r
245 \r
246                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
247                 {\r
248                         xErrorDetected = pdTRUE;\r
249                 }\r
250 \r
251                 /* The data we sent to the queue should equal the data we just received\r
252                 from the queue. */\r
253                 if( ulLoopCounter != ulData )\r
254                 {\r
255                         xErrorDetected = pdTRUE;\r
256                 }\r
257 \r
258                 #if configUSE_PREEMPTION == 0\r
259                         taskYIELD();\r
260                 #endif\r
261 \r
262 \r
263 \r
264                 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */\r
265                 for( ulData = 2; ulData < 5; ulData++ )\r
266                 {\r
267                         xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );\r
268                 }\r
269 \r
270                 /* Now the order in the queue should be 2, 3, 4, with 2 being the first\r
271                 thing to be read out.  Now add 1 then 0 to the front of the queue. */\r
272                 if( uxQueueMessagesWaiting( xQueue ) != 3 )\r
273                 {\r
274                         xErrorDetected = pdTRUE;\r
275                 }\r
276                 ulData = 1;\r
277                 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );\r
278                 ulData = 0;\r
279                 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );\r
280 \r
281                 /* Now the queue should be full, and when we read the data out we\r
282                 should receive 0, 1, 2, 3, 4. */\r
283                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
284                 {\r
285                         xErrorDetected = pdTRUE;\r
286                 }\r
287 \r
288                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
289                 {\r
290                         xErrorDetected = pdTRUE;\r
291                 }\r
292 \r
293                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
294                 {\r
295                         xErrorDetected = pdTRUE;\r
296                 }\r
297 \r
298                 #if configUSE_PREEMPTION == 0\r
299                         taskYIELD();\r
300                 #endif\r
301 \r
302                 /* Check the data we read out is in the expected order. */\r
303                 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )\r
304                 {\r
305                         /* Try peeking the data first. */\r
306                         if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )\r
307                         {\r
308                                 xErrorDetected = pdTRUE;\r
309                         }\r
310 \r
311                         if( ulData != ulData2 )\r
312                         {\r
313                                 xErrorDetected = pdTRUE;\r
314                         }\r
315 \r
316 \r
317                         /* Now try receiving the data for real.  The value should be the\r
318                         same.  Clobber the value first so we know we really received it. */\r
319                         ulData2 = ~ulData2;\r
320                         if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )\r
321                         {\r
322                                 xErrorDetected = pdTRUE;\r
323                         }\r
324 \r
325                         if( ulData != ulData2 )\r
326                         {\r
327                                 xErrorDetected = pdTRUE;\r
328                         }\r
329                 }\r
330 \r
331                 /* The queue should now be empty again. */\r
332                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
333                 {\r
334                         xErrorDetected = pdTRUE;\r
335                 }\r
336 \r
337                 #if configUSE_PREEMPTION == 0\r
338                         taskYIELD();\r
339                 #endif\r
340 \r
341 \r
342                 /* Our queue is empty once more, add 10, 11 to the back. */\r
343                 ulData = 10;\r
344                 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )\r
345                 {\r
346                         xErrorDetected = pdTRUE;\r
347                 }\r
348                 ulData = 11;\r
349                 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )\r
350                 {\r
351                         xErrorDetected = pdTRUE;\r
352                 }\r
353 \r
354                 if( uxQueueMessagesWaiting( xQueue ) != 2 )\r
355                 {\r
356                         xErrorDetected = pdTRUE;\r
357                 }\r
358 \r
359                 /* Now we should have 10, 11 in the queue.  Add 7, 8, 9 to the\r
360                 front. */\r
361                 for( ulData = 9; ulData >= 7; ulData-- )\r
362                 {\r
363                         if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )\r
364                         {\r
365                                 xErrorDetected = pdTRUE;\r
366                         }\r
367                 }\r
368 \r
369                 /* Now check that the queue is full, and that receiving data provides\r
370                 the expected sequence of 7, 8, 9, 10, 11. */\r
371                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
372                 {\r
373                         xErrorDetected = pdTRUE;\r
374                 }\r
375 \r
376                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
377                 {\r
378                         xErrorDetected = pdTRUE;\r
379                 }\r
380 \r
381                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
382                 {\r
383                         xErrorDetected = pdTRUE;\r
384                 }\r
385 \r
386                 #if configUSE_PREEMPTION == 0\r
387                         taskYIELD();\r
388                 #endif\r
389 \r
390                 /* Check the data we read out is in the expected order. */\r
391                 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )\r
392                 {\r
393                         if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )\r
394                         {\r
395                                 xErrorDetected = pdTRUE;\r
396                         }\r
397 \r
398                         if( ulData != ulData2 )\r
399                         {\r
400                                 xErrorDetected = pdTRUE;\r
401                         }\r
402                 }\r
403 \r
404                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
405                 {\r
406                         xErrorDetected = pdTRUE;\r
407                 }\r
408 \r
409                 ulLoopCounter++;\r
410         }\r
411 }\r
412 /*-----------------------------------------------------------*/\r
413 \r
414 static void prvLowPriorityMutexTask( void *pvParameters )\r
415 {\r
416 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters, xLocalMutex;\r
417 \r
418         #ifdef USE_STDIO\r
419         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
420 \r
421                 const char * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";\r
422 \r
423                 /* Queue a message for printing to say the task has started. */\r
424                 vPrintDisplayMessage( &pcTaskStartMsg );\r
425         #endif\r
426 \r
427         /* The local mutex is used to check the 'mutexs held' count. */\r
428         xLocalMutex = xSemaphoreCreateMutex();\r
429         configASSERT( xLocalMutex );\r
430 \r
431         for( ;; )\r
432         {\r
433                 /* Take the mutex.  It should be available now. */\r
434                 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )\r
435                 {\r
436                         xErrorDetected = pdTRUE;\r
437                 }\r
438 \r
439                 /* Set the guarded variable to a known start value. */\r
440                 ulGuardedVariable = 0;\r
441 \r
442                 /* This task's priority should be as per that assigned when the task was\r
443                 created. */\r
444                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
445                 {\r
446                         xErrorDetected = pdTRUE;\r
447                 }\r
448 \r
449                 /* Now unsuspend the high priority task.  This will attempt to take the\r
450                 mutex, and block when it finds it cannot obtain it. */\r
451                 vTaskResume( xHighPriorityMutexTask );\r
452 \r
453                 #if configUSE_PREEMPTION == 0\r
454                         taskYIELD();\r
455                 #endif\r
456 \r
457                 /* Ensure the task is reporting its priority as blocked and not\r
458                 suspended (as it would have done in versions up to V7.5.3). */\r
459                 #if( INCLUDE_eTaskGetState == 1 )\r
460                 {\r
461                         configASSERT( eTaskGetState( xHighPriorityMutexTask ) == eBlocked );\r
462                 }\r
463                 #endif /* INCLUDE_eTaskGetState */\r
464 \r
465                 /* The priority of the high priority task should now have been inherited\r
466                 as by now it will have attempted to get the mutex. */\r
467                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
468                 {\r
469                         xErrorDetected = pdTRUE;\r
470                 }\r
471 \r
472                 /* Attempt to set the priority of this task to the test priority -\r
473                 between the     idle priority and the medium/high test priorities, but the\r
474                 actual priority should remain at the high priority. */\r
475                 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );\r
476                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
477                 {\r
478                         xErrorDetected = pdTRUE;\r
479                 }\r
480 \r
481                 /* Now unsuspend the medium priority task.  This should not run as the\r
482                 inherited priority of this task is above that of the medium priority\r
483                 task. */\r
484                 vTaskResume( xMediumPriorityMutexTask );\r
485 \r
486                 /* If the medium priority task did run then it will have incremented the \r
487                 guarded variable. */\r
488                 if( ulGuardedVariable != 0 )\r
489                 {\r
490                         xErrorDetected = pdTRUE;\r
491                 }\r
492 \r
493                 /* Take the local mutex too, so two mutexes are now held. */\r
494                 if( xSemaphoreTake( xLocalMutex, genqNO_BLOCK ) != pdPASS )\r
495                 {\r
496                         xErrorDetected = pdTRUE;\r
497                 }\r
498 \r
499                 /* When the semaphore is given back the priority of this task should not\r
500                 yet be disinherited because the local mutex is still held.  This is a\r
501                 simplification to allow FreeRTOS to be integrated with middleware that\r
502                 attempts to hold multiple mutexes without bloating the code with complex\r
503                 algorithms.  It is possible that the high priority mutex task will\r
504                 execute as it shares a priority with this task. */\r
505                 if( xSemaphoreGive( xMutex ) != pdPASS )\r
506                 {\r
507                         xErrorDetected = pdTRUE;\r
508                 }\r
509 \r
510                 #if configUSE_PREEMPTION == 0\r
511                         taskYIELD();\r
512                 #endif\r
513 \r
514                 /* The guarded variable is only incremented by the medium priority task,\r
515                 which still should not have executed as this task should remain at the\r
516                 higher priority, ensure this is the case. */\r
517                 if( ulGuardedVariable != 0 )\r
518                 {\r
519                         xErrorDetected = pdTRUE;\r
520                 }\r
521 \r
522                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
523                 {\r
524                         xErrorDetected = pdTRUE;\r
525                 }\r
526 \r
527                 /* Now also give back the local mutex, taking the held count back to 0.\r
528                 This time the priority of this task should be disinherited back to the\r
529                 priority to which it was set while the mutex was held.  This means\r
530                 the medium priority task should execute and increment the guarded \r
531                 variable.   When this task next runs both the high and medium priority \r
532                 tasks will have been suspended again. */\r
533                 if( xSemaphoreGive( xLocalMutex ) != pdPASS )\r
534                 {\r
535                         xErrorDetected = pdTRUE;\r
536                 }\r
537 \r
538                 #if configUSE_PREEMPTION == 0\r
539                         taskYIELD();\r
540                 #endif\r
541 \r
542                 /* Check the guarded variable did indeed increment... */\r
543                 if( ulGuardedVariable != 1 )\r
544                 {\r
545                         xErrorDetected = pdTRUE;\r
546                 }\r
547 \r
548                 /* ... and that the priority of this task has been disinherited to\r
549                 genqMUTEX_TEST_PRIORITY. */\r
550                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )\r
551                 {\r
552                         xErrorDetected = pdTRUE;\r
553                 }\r
554 \r
555                 /* Set the priority of this task back to its original value, ready for\r
556                 the next loop around this test. */\r
557                 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );\r
558 \r
559                 /* Just to show this task is still running. */\r
560                 ulLoopCounter2++;\r
561 \r
562                 #if configUSE_PREEMPTION == 0\r
563                         taskYIELD();\r
564                 #endif\r
565         }\r
566 }\r
567 /*-----------------------------------------------------------*/\r
568 \r
569 static void prvMediumPriorityMutexTask( void *pvParameters )\r
570 {\r
571         ( void ) pvParameters;\r
572 \r
573         for( ;; )\r
574         {\r
575                 /* The medium priority task starts by suspending itself.  The low\r
576                 priority task will unsuspend this task when required. */\r
577                 vTaskSuspend( NULL );\r
578 \r
579                 /* When this task unsuspends all it does is increment the guarded\r
580                 variable, this is so the low priority task knows that it has\r
581                 executed. */\r
582                 ulGuardedVariable++;\r
583         }\r
584 }\r
585 /*-----------------------------------------------------------*/\r
586 \r
587 static void prvHighPriorityMutexTask( void *pvParameters )\r
588 {\r
589 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;\r
590 \r
591         for( ;; )\r
592         {\r
593                 /* The high priority task starts by suspending itself.  The low\r
594                 priority task will unsuspend this task when required. */\r
595                 vTaskSuspend( NULL );\r
596 \r
597                 /* When this task unsuspends all it does is attempt to obtain\r
598                 the mutex.  It should find the mutex is not available so a\r
599                 block time is specified. */\r
600                 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )\r
601                 {\r
602                         xErrorDetected = pdTRUE;\r
603                 }\r
604 \r
605                 /* When the mutex is eventually obtained it is just given back before\r
606                 returning to suspend ready for the next cycle. */\r
607                 if( xSemaphoreGive( xMutex ) != pdPASS )\r
608                 {\r
609                         xErrorDetected = pdTRUE;\r
610                 }\r
611         }\r
612 }\r
613 /*-----------------------------------------------------------*/\r
614 \r
615 /* This is called to check that all the created tasks are still running. */\r
616 BaseType_t xAreGenericQueueTasksStillRunning( void )\r
617 {\r
618 static uint32_t ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;\r
619 \r
620         /* If the demo task is still running then we expect the loopcounters to\r
621         have incremented since this function was last called. */\r
622         if( ulLastLoopCounter == ulLoopCounter )\r
623         {\r
624                 xErrorDetected = pdTRUE;\r
625         }\r
626 \r
627         if( ulLastLoopCounter2 == ulLoopCounter2 )\r
628         {\r
629                 xErrorDetected = pdTRUE;\r
630         }\r
631 \r
632         ulLastLoopCounter = ulLoopCounter;\r
633         ulLastLoopCounter2 = ulLoopCounter2;\r
634 \r
635         /* Errors detected in the task itself will have latched xErrorDetected\r
636         to true. */\r
637 \r
638         return ( BaseType_t ) !xErrorDetected;\r
639 }\r
640 \r
641 \r