]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/GenQTest.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Demo / Common / Minimal / GenQTest.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /*\r
30  * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -\r
31  * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and\r
32  * mutex behaviour.\r
33  *\r
34  * See the comments above the prvSendFrontAndBackTest() and\r
35  * prvLowPriorityMutexTask() prototypes below for more information.\r
36  */\r
37 \r
38 /* Standard includes. */\r
39 #include <stdlib.h>\r
40 \r
41 /* Scheduler include files. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 #include "queue.h"\r
45 #include "semphr.h"\r
46 \r
47 /* Demo program include files. */\r
48 #include "GenQTest.h"\r
49 \r
50 #define genqQUEUE_LENGTH                ( 5 )\r
51 #define intsemNO_BLOCK                  ( 0 )\r
52 #define genqSHORT_BLOCK                 ( pdMS_TO_TICKS( 2 ) )\r
53 \r
54 #define genqMUTEX_LOW_PRIORITY          ( tskIDLE_PRIORITY )\r
55 #define genqMUTEX_TEST_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
56 #define genqMUTEX_MEDIUM_PRIORITY       ( tskIDLE_PRIORITY + 2 )\r
57 #define genqMUTEX_HIGH_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
58 \r
59 /*-----------------------------------------------------------*/\r
60 \r
61 /*\r
62  * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()\r
63  * macros by using both to fill a queue, then reading from the queue to\r
64  * check the resultant queue order is as expected.  Queue data is also\r
65  * peeked.\r
66  */\r
67 static void prvSendFrontAndBackTest( void *pvParameters );\r
68 \r
69 /*\r
70  * The following three tasks are used to demonstrate the mutex behaviour.\r
71  * Each task is given a different priority to demonstrate the priority\r
72  * inheritance mechanism.\r
73  *\r
74  * The low priority task obtains a mutex.  After this a high priority task\r
75  * attempts to obtain the same mutex, causing its priority to be inherited\r
76  * by the low priority task.  The task with the inherited high priority then\r
77  * resumes a medium priority task to ensure it is not blocked by the medium\r
78  * priority task while it holds the inherited high priority.  Once the mutex\r
79  * is returned the task with the inherited priority returns to its original\r
80  * low priority, and is therefore immediately preempted by first the high\r
81  * priority task and then the medium priority task before it can continue.\r
82  */\r
83 static void prvLowPriorityMutexTask( void *pvParameters );\r
84 static void prvMediumPriorityMutexTask( void *pvParameters );\r
85 static void prvHighPriorityMutexTask( void *pvParameters );\r
86 \r
87 /*\r
88  * Tests the behaviour when a low priority task inherits the priority of a\r
89  * higher priority task when taking two mutexes, and returns the mutexes in\r
90  * first the same order as the two mutexes were obtained, and second the\r
91  * opposite order as the two mutexes were obtained.\r
92  */\r
93 static void prvTakeTwoMutexesReturnInSameOrder( SemaphoreHandle_t xMutex, SemaphoreHandle_t xLocalMutex );\r
94 static void prvTakeTwoMutexesReturnInDifferentOrder( SemaphoreHandle_t xMutex, SemaphoreHandle_t xLocalMutex );\r
95 \r
96 #if( INCLUDE_xTaskAbortDelay == 1 )\r
97 \r
98         #if( configUSE_PREEMPTION == 0 )\r
99                 #error The additional tests included when INCLUDE_xTaskAbortDelay is 1 expect preemption to be used.\r
100         #endif\r
101 \r
102         /* Tests the behaviour when a low priority task inherits the priority of a\r
103         high priority task only for the high priority task to timeout before\r
104         obtaining the mutex. */\r
105         static void prvHighPriorityTimeout( SemaphoreHandle_t xMutex );\r
106 #endif\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
111 detected in any of the tasks. */\r
112 static volatile BaseType_t xErrorDetected = pdFALSE;\r
113 \r
114 /* Counters that are incremented on each cycle of a test.  This is used to\r
115 detect a stalled task - a test that is no longer running. */\r
116 static volatile uint32_t ulLoopCounter = 0;\r
117 static volatile uint32_t ulLoopCounter2 = 0;\r
118 \r
119 /* The variable that is guarded by the mutex in the mutex demo tasks. */\r
120 static volatile uint32_t ulGuardedVariable = 0;\r
121 \r
122 /* Handles used in the mutex test to suspend and resume the high and medium\r
123 priority mutex test tasks. */\r
124 static TaskHandle_t xHighPriorityMutexTask, xMediumPriorityMutexTask;\r
125 \r
126 /* If INCLUDE_xTaskAbortDelay is 1 additional tests are performed, requiring an\r
127 additional task. */\r
128 #if( INCLUDE_xTaskAbortDelay == 1 )\r
129         static TaskHandle_t xSecondMediumPriorityMutexTask;\r
130 #endif\r
131 \r
132 /* Lets the high priority semaphore task know that its wait for the semaphore\r
133 was aborted, in which case not being able to obtain the semaphore is not to be\r
134 considered an error. */\r
135 static volatile BaseType_t xBlockWasAborted = pdFALSE;\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 void vStartGenericQueueTasks( UBaseType_t uxPriority )\r
140 {\r
141 QueueHandle_t xQueue;\r
142 SemaphoreHandle_t xMutex;\r
143 \r
144         /* Create the queue that we are going to use for the\r
145         prvSendFrontAndBackTest demo. */\r
146         xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( uint32_t ) );\r
147 \r
148         if( xQueue != NULL )\r
149         {\r
150                 /* vQueueAddToRegistry() adds the queue to the queue registry, if one\r
151                 is in use.  The queue registry is provided as a means for kernel aware\r
152                 debuggers to locate queues and has no purpose if a kernel aware debugger\r
153                 is not being used.  The call to vQueueAddToRegistry() will be removed\r
154                 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
155                 defined to be less than 1. */\r
156                 vQueueAddToRegistry( xQueue, "Gen_Queue_Test" );\r
157 \r
158                 /* Create the demo task and pass it the queue just created.  We are\r
159                 passing the queue handle by value so it does not matter that it is\r
160                 declared on the stack here. */\r
161                 xTaskCreate( prvSendFrontAndBackTest, "GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );\r
162         }\r
163 \r
164         /* Create the mutex used by the prvMutexTest task. */\r
165         xMutex = xSemaphoreCreateMutex();\r
166 \r
167         if( xMutex != NULL )\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\r
172                 debugger is not being used.  The call to vQueueAddToRegistry() will be\r
173                 removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not\r
174                 defined or is 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\r
178                 are passing the mutex handle by value so it does not matter that it is\r
179                 declared 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                 /* If INCLUDE_xTaskAbortDelay is set then additional tests are performed,\r
185                 requiring two instances of prvHighPriorityMutexTask(). */\r
186                 #if( INCLUDE_xTaskAbortDelay == 1 )\r
187                 {\r
188                         xTaskCreate( prvHighPriorityMutexTask, "MuHigh2", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_MEDIUM_PRIORITY, &xSecondMediumPriorityMutexTask );\r
189                 }\r
190                 #endif /* INCLUDE_xTaskAbortDelay */\r
191         }\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvSendFrontAndBackTest( void *pvParameters )\r
196 {\r
197 uint32_t ulData, ulData2, ulLoopCounterSnapshot;\r
198 QueueHandle_t xQueue;\r
199 \r
200         #ifdef USE_STDIO\r
201         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
202 \r
203                 const char * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";\r
204 \r
205                 /* Queue a message for printing to say the task has started. */\r
206                 vPrintDisplayMessage( &pcTaskStartMsg );\r
207         #endif\r
208 \r
209         xQueue = ( QueueHandle_t ) pvParameters;\r
210 \r
211         for( ;; )\r
212         {\r
213                 /* The queue is empty, so sending an item to the back of the queue\r
214                 should have the same efect as sending it to the front of the queue.\r
215 \r
216                 First send to the front and check everything is as expected. */\r
217                 ulLoopCounterSnapshot = ulLoopCounter;\r
218                 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounterSnapshot, intsemNO_BLOCK );\r
219 \r
220                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
221                 {\r
222                         xErrorDetected = pdTRUE;\r
223                 }\r
224 \r
225                 if( xQueueReceive( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != pdPASS )\r
226                 {\r
227                         xErrorDetected = pdTRUE;\r
228                 }\r
229 \r
230                 /* The data we sent to the queue should equal the data we just received\r
231                 from the queue. */\r
232                 if( ulLoopCounter != ulData )\r
233                 {\r
234                         xErrorDetected = pdTRUE;\r
235                 }\r
236 \r
237                 /* Then do the same, sending the data to the back, checking everything\r
238                 is as expected. */\r
239                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
240                 {\r
241                         xErrorDetected = pdTRUE;\r
242                 }\r
243 \r
244                 ulLoopCounterSnapshot = ulLoopCounter;\r
245                 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounterSnapshot, intsemNO_BLOCK );\r
246 \r
247                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
248                 {\r
249                         xErrorDetected = pdTRUE;\r
250                 }\r
251 \r
252                 if( xQueueReceive( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != pdPASS )\r
253                 {\r
254                         xErrorDetected = pdTRUE;\r
255                 }\r
256 \r
257                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
258                 {\r
259                         xErrorDetected = pdTRUE;\r
260                 }\r
261 \r
262                 /* The data sent to the queue should equal the data just received from\r
263                 the queue. */\r
264                 if( ulLoopCounter != ulData )\r
265                 {\r
266                         xErrorDetected = pdTRUE;\r
267                 }\r
268 \r
269                 #if configUSE_PREEMPTION == 0\r
270                         taskYIELD();\r
271                 #endif\r
272 \r
273 \r
274 \r
275                 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */\r
276                 for( ulData = 2; ulData < 5; ulData++ )\r
277                 {\r
278                         xQueueSendToBack( xQueue, ( void * ) &ulData, intsemNO_BLOCK );\r
279                 }\r
280 \r
281                 /* Now the order in the queue should be 2, 3, 4, with 2 being the first\r
282                 thing to be read out.  Now add 1 then 0 to the front of the queue. */\r
283                 if( uxQueueMessagesWaiting( xQueue ) != 3 )\r
284                 {\r
285                         xErrorDetected = pdTRUE;\r
286                 }\r
287                 ulData = 1;\r
288                 xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK );\r
289                 ulData = 0;\r
290                 xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK );\r
291 \r
292                 /* Now the queue should be full, and when we read the data out we\r
293                 should receive 0, 1, 2, 3, 4. */\r
294                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
295                 {\r
296                         xErrorDetected = pdTRUE;\r
297                 }\r
298 \r
299                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
300                 {\r
301                         xErrorDetected = pdTRUE;\r
302                 }\r
303 \r
304                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
305                 {\r
306                         xErrorDetected = pdTRUE;\r
307                 }\r
308 \r
309                 #if configUSE_PREEMPTION == 0\r
310                         taskYIELD();\r
311                 #endif\r
312 \r
313                 /* Check the data we read out is in the expected order. */\r
314                 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )\r
315                 {\r
316                         /* Try peeking the data first. */\r
317                         if( xQueuePeek( xQueue, &ulData2, intsemNO_BLOCK ) != pdPASS )\r
318                         {\r
319                                 xErrorDetected = pdTRUE;\r
320                         }\r
321 \r
322                         if( ulData != ulData2 )\r
323                         {\r
324                                 xErrorDetected = pdTRUE;\r
325                         }\r
326 \r
327 \r
328                         /* Now try receiving the data for real.  The value should be the\r
329                         same.  Clobber the value first so we know we really received it. */\r
330                         ulData2 = ~ulData2;\r
331                         if( xQueueReceive( xQueue, &ulData2, intsemNO_BLOCK ) != pdPASS )\r
332                         {\r
333                                 xErrorDetected = pdTRUE;\r
334                         }\r
335 \r
336                         if( ulData != ulData2 )\r
337                         {\r
338                                 xErrorDetected = pdTRUE;\r
339                         }\r
340                 }\r
341 \r
342                 /* The queue should now be empty again. */\r
343                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
344                 {\r
345                         xErrorDetected = pdTRUE;\r
346                 }\r
347 \r
348                 #if configUSE_PREEMPTION == 0\r
349                         taskYIELD();\r
350                 #endif\r
351 \r
352 \r
353                 /* Our queue is empty once more, add 10, 11 to the back. */\r
354                 ulData = 10;\r
355                 if( xQueueSend( xQueue, &ulData, intsemNO_BLOCK ) != pdPASS )\r
356                 {\r
357                         xErrorDetected = pdTRUE;\r
358                 }\r
359                 ulData = 11;\r
360                 if( xQueueSend( xQueue, &ulData, intsemNO_BLOCK ) != pdPASS )\r
361                 {\r
362                         xErrorDetected = pdTRUE;\r
363                 }\r
364 \r
365                 if( uxQueueMessagesWaiting( xQueue ) != 2 )\r
366                 {\r
367                         xErrorDetected = pdTRUE;\r
368                 }\r
369 \r
370                 /* Now we should have 10, 11 in the queue.  Add 7, 8, 9 to the\r
371                 front. */\r
372                 for( ulData = 9; ulData >= 7; ulData-- )\r
373                 {\r
374                         if( xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != pdPASS )\r
375                         {\r
376                                 xErrorDetected = pdTRUE;\r
377                         }\r
378                 }\r
379 \r
380                 /* Now check that the queue is full, and that receiving data provides\r
381                 the expected sequence of 7, 8, 9, 10, 11. */\r
382                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
383                 {\r
384                         xErrorDetected = pdTRUE;\r
385                 }\r
386 \r
387                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
388                 {\r
389                         xErrorDetected = pdTRUE;\r
390                 }\r
391 \r
392                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
393                 {\r
394                         xErrorDetected = pdTRUE;\r
395                 }\r
396 \r
397                 #if configUSE_PREEMPTION == 0\r
398                         taskYIELD();\r
399                 #endif\r
400 \r
401                 /* Check the data we read out is in the expected order. */\r
402                 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )\r
403                 {\r
404                         if( xQueueReceive( xQueue, &ulData2, intsemNO_BLOCK ) != pdPASS )\r
405                         {\r
406                                 xErrorDetected = pdTRUE;\r
407                         }\r
408 \r
409                         if( ulData != ulData2 )\r
410                         {\r
411                                 xErrorDetected = pdTRUE;\r
412                         }\r
413                 }\r
414 \r
415                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
416                 {\r
417                         xErrorDetected = pdTRUE;\r
418                 }\r
419 \r
420                 /* Increment the loop counter to indicate these tasks are still\r
421                 executing. */\r
422                 ulLoopCounter++;\r
423         }\r
424 }\r
425 /*-----------------------------------------------------------*/\r
426 \r
427 #if( INCLUDE_xTaskAbortDelay == 1 )\r
428 \r
429         static void prvHighPriorityTimeout( SemaphoreHandle_t xMutex )\r
430         {\r
431         static UBaseType_t uxLoopCount = 0;\r
432 \r
433                 /* The tests in this function are very similar, the slight variations\r
434                 are for code coverage purposes. */\r
435 \r
436                 /* Take the mutex.  It should be available now. */\r
437                 if( xSemaphoreTake( xMutex, intsemNO_BLOCK ) != pdPASS )\r
438                 {\r
439                         xErrorDetected = pdTRUE;\r
440                 }\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                 /* This task should now have inherited the priority of the high priority\r
454                 task as by now the high priority task will have attempted to obtain the\r
455                 mutex. */\r
456                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
457                 {\r
458                         xErrorDetected = pdTRUE;\r
459                 }\r
460 \r
461                 /* Unblock a second medium priority task.  It too will attempt to take\r
462                 the mutex and enter the Blocked state - it won't run yet though as this\r
463                 task has inherited a priority above it. */\r
464                 vTaskResume( xSecondMediumPriorityMutexTask );\r
465 \r
466                 /* This task should still have the priority of the high priority task as\r
467                 that had already been inherited as is the highest priority of the three\r
468                 tasks using the mutex. */\r
469                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
470                 {\r
471                         xErrorDetected = pdTRUE;\r
472                 }\r
473 \r
474                 /* On some loops, block for a short while to provide additional\r
475                 code coverage.  Blocking here will allow the medium priority task to\r
476                 execute and so also block on the mutex so when the high priority task\r
477                 causes this task to disinherit the high priority it is inherited down to\r
478                 the priority of the medium priority task.  When there is no delay the\r
479                 medium priority task will not run until after the disinheritance, so\r
480                 this task will disinherit back to its base priority, then only up to the\r
481                 medium priority after the medium priority has executed. */\r
482                 vTaskDelay( uxLoopCount & ( UBaseType_t ) 0x07 );\r
483 \r
484                 /* Now force the high priority task to unblock.  It will fail to obtain\r
485                 the mutex and go back to the suspended state - allowing this task to\r
486                 execute again.  xBlockWasAborted is set to pdTRUE so the higher priority\r
487                 task knows that its failure to obtain the semaphore is not an error. */\r
488                 xBlockWasAborted = pdTRUE;\r
489                 if( xTaskAbortDelay( xHighPriorityMutexTask ) != pdPASS )\r
490                 {\r
491                         xErrorDetected = pdTRUE;\r
492                 }\r
493 \r
494                 /* This task has inherited the priority of xHighPriorityMutexTask so\r
495                 could still be running even though xHighPriorityMutexTask is no longer\r
496                 blocked.  Delay for a short while to ensure xHighPriorityMutexTask gets\r
497                 a chance to run - indicated by this task changing priority.  It should\r
498                 disinherit the high priority task, but then inherit the priority of the\r
499                 medium priority task that is waiting for the same mutex. */\r
500                 while( uxTaskPriorityGet( NULL ) != genqMUTEX_MEDIUM_PRIORITY )\r
501                 {\r
502                         /* If this task gets stuck here then the check variables will stop\r
503                         incrementing and the check task will detect the error. */\r
504                         vTaskDelay( genqSHORT_BLOCK );\r
505                 }\r
506 \r
507                 /* Now force the medium priority task to unblock.  xBlockWasAborted is\r
508                 set to pdTRUE so the medium priority task knows that its failure to\r
509                 obtain the semaphore is not an error. */\r
510                 xBlockWasAborted = pdTRUE;\r
511                 if( xTaskAbortDelay( xSecondMediumPriorityMutexTask ) != pdPASS )\r
512                 {\r
513                         xErrorDetected = pdTRUE;\r
514                 }\r
515 \r
516                 /* This time no other tasks are waiting for the mutex, so this task\r
517                 should return to its base priority.  This might not happen straight\r
518                 away as it is running at the same priority as the task it just\r
519                 unblocked. */\r
520                 while( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
521                 {\r
522                         /* If this task gets stuck here then the check variables will stop\r
523                         incrementing and the check task will detect the error. */\r
524                         vTaskDelay( genqSHORT_BLOCK );\r
525                 }\r
526 \r
527                 /* Give the semaphore back ready for the next test. */\r
528                 xSemaphoreGive( xMutex );\r
529 \r
530                 configASSERT( xErrorDetected == pdFALSE );\r
531 \r
532 \r
533 \r
534                 /* Now do the same again, but this time unsuspend the tasks in the\r
535                 opposite order.  This takes a different path though the code because\r
536                 when the high priority task has its block aborted there is already\r
537                 another task in the list of tasks waiting for the mutex, and the\r
538                 low priority task drops down to that priority, rather than dropping\r
539                 down to its base priority before inheriting the priority of the medium\r
540                 priority task. */\r
541                 if( xSemaphoreTake( xMutex, intsemNO_BLOCK ) != pdPASS )\r
542                 {\r
543                         xErrorDetected = pdTRUE;\r
544                 }\r
545 \r
546                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
547                 {\r
548                         xErrorDetected = pdTRUE;\r
549                 }\r
550 \r
551                 /* This time unsuspend the medium priority task first.  This will\r
552                 attempt to take the mutex, and block when it finds it cannot obtain it. */\r
553                 vTaskResume( xSecondMediumPriorityMutexTask );\r
554 \r
555                 /* This time this task should now have inherited the priority of the\r
556                 medium task. */\r
557                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_MEDIUM_PRIORITY )\r
558                 {\r
559                         xErrorDetected = pdTRUE;\r
560                 }\r
561 \r
562                 /* This time the high priority task in unsuspended second. */\r
563                 vTaskResume( xHighPriorityMutexTask );\r
564 \r
565                 /* The high priority task should already have run, causing this task to\r
566                 inherit a priority for the second time. */\r
567                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
568                 {\r
569                         xErrorDetected = pdTRUE;\r
570                 }\r
571 \r
572                 /* This time, when the high priority task has its delay aborted and it\r
573                 fails to obtain the mutex this task will immediately have its priority\r
574                 lowered down to that of the highest priority task waiting on the mutex,\r
575                 which is the medium priority task. */\r
576                 xBlockWasAborted = pdTRUE;\r
577                 if( xTaskAbortDelay( xHighPriorityMutexTask ) != pdPASS )\r
578                 {\r
579                         xErrorDetected = pdTRUE;\r
580                 }\r
581 \r
582                 while( uxTaskPriorityGet( NULL ) != genqMUTEX_MEDIUM_PRIORITY )\r
583                 {\r
584                         /* If this task gets stuck here then the check variables will stop\r
585                         incrementing and the check task will detect the error. */\r
586                         vTaskDelay( genqSHORT_BLOCK );\r
587                 }\r
588 \r
589                 /* And finally, when the medium priority task also have its delay\r
590                 aborted there are no other tasks waiting for the mutex so this task\r
591                 returns to its base priority. */\r
592                 xBlockWasAborted = pdTRUE;\r
593                 if( xTaskAbortDelay( xSecondMediumPriorityMutexTask ) != pdPASS )\r
594                 {\r
595                         xErrorDetected = pdTRUE;\r
596                 }\r
597 \r
598                 while( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
599                 {\r
600                         /* If this task gets stuck here then the check variables will stop\r
601                         incrementing and the check task will detect the error. */\r
602                         vTaskDelay( genqSHORT_BLOCK );\r
603                 }\r
604 \r
605                 /* Give the semaphore back ready for the next test. */\r
606                 xSemaphoreGive( xMutex );\r
607 \r
608                 configASSERT( xErrorDetected == pdFALSE );\r
609 \r
610                 /* uxLoopCount is used to add a variable delay, and in-so-doing provide\r
611                 additional code coverage. */\r
612                 uxLoopCount++;\r
613         }\r
614 \r
615 #endif /* INCLUDE_xTaskAbortDelay == 1 */\r
616 /*-----------------------------------------------------------*/\r
617 \r
618 static void prvTakeTwoMutexesReturnInDifferentOrder( SemaphoreHandle_t xMutex, SemaphoreHandle_t xLocalMutex )\r
619 {\r
620         /* Take the mutex.  It should be available now. */\r
621         if( xSemaphoreTake( xMutex, intsemNO_BLOCK ) != pdPASS )\r
622         {\r
623                 xErrorDetected = pdTRUE;\r
624         }\r
625 \r
626         /* Set the guarded variable to a known start value. */\r
627         ulGuardedVariable = 0;\r
628 \r
629         /* This task's priority should be as per that assigned when the task was\r
630         created. */\r
631         if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
632         {\r
633                 xErrorDetected = pdTRUE;\r
634         }\r
635 \r
636         /* Now unsuspend the high priority task.  This will attempt to take the\r
637         mutex, and block when it finds it cannot obtain it. */\r
638         vTaskResume( xHighPriorityMutexTask );\r
639 \r
640         #if configUSE_PREEMPTION == 0\r
641                 taskYIELD();\r
642         #endif\r
643 \r
644         /* Ensure the task is reporting its priority as blocked and not\r
645         suspended (as it would have done in versions up to V7.5.3). */\r
646         #if( INCLUDE_eTaskGetState == 1 )\r
647         {\r
648                 configASSERT( eTaskGetState( xHighPriorityMutexTask ) == eBlocked );\r
649         }\r
650         #endif /* INCLUDE_eTaskGetState */\r
651 \r
652         /* This task should now have inherited the priority of the high priority\r
653         task as by now the high priority task will have attempted to obtain the\r
654         mutex. */\r
655         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
656         {\r
657                 xErrorDetected = pdTRUE;\r
658         }\r
659 \r
660         /* Attempt to set the priority of this task to the test priority -\r
661         between the idle priority and the medium/high test priorities, but the\r
662         actual priority should remain at the high priority. */\r
663         vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );\r
664         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
665         {\r
666                 xErrorDetected = pdTRUE;\r
667         }\r
668 \r
669         /* Now unsuspend the medium priority task.  This should not run as the\r
670         inherited priority of this task is above that of the medium priority\r
671         task. */\r
672         vTaskResume( xMediumPriorityMutexTask );\r
673 \r
674         /* If the medium priority task did run then it will have incremented the\r
675         guarded variable. */\r
676         if( ulGuardedVariable != 0 )\r
677         {\r
678                 xErrorDetected = pdTRUE;\r
679         }\r
680 \r
681         /* Take the local mutex too, so two mutexes are now held. */\r
682         if( xSemaphoreTake( xLocalMutex, intsemNO_BLOCK ) != pdPASS )\r
683         {\r
684                 xErrorDetected = pdTRUE;\r
685         }\r
686 \r
687         /* When the semaphore is given back the priority of this task should not\r
688         yet be disinherited because the local mutex is still held.  This is a\r
689         simplification to allow FreeRTOS to be integrated with middleware that\r
690         attempts to hold multiple mutexes without bloating the code with complex\r
691         algorithms.  It is possible that the high priority mutex task will\r
692         execute as it shares a priority with this task. */\r
693         if( xSemaphoreGive( xMutex ) != pdPASS )\r
694         {\r
695                 xErrorDetected = pdTRUE;\r
696         }\r
697 \r
698         #if configUSE_PREEMPTION == 0\r
699                 taskYIELD();\r
700         #endif\r
701 \r
702         /* The guarded variable is only incremented by the medium priority task,\r
703         which still should not have executed as this task should remain at the\r
704         higher priority, ensure this is the case. */\r
705         if( ulGuardedVariable != 0 )\r
706         {\r
707                 xErrorDetected = pdTRUE;\r
708         }\r
709 \r
710         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
711         {\r
712                 xErrorDetected = pdTRUE;\r
713         }\r
714 \r
715         /* Now also give back the local mutex, taking the held count back to 0.\r
716         This time the priority of this task should be disinherited back to the\r
717         priority to which it was set while the mutex was held.  This means\r
718         the medium priority task should execute and increment the guarded\r
719         variable.   When this task next runs both the high and medium priority\r
720         tasks will have been suspended again. */\r
721         if( xSemaphoreGive( xLocalMutex ) != pdPASS )\r
722         {\r
723                 xErrorDetected = pdTRUE;\r
724         }\r
725 \r
726         #if configUSE_PREEMPTION == 0\r
727                 taskYIELD();\r
728         #endif\r
729 \r
730         /* Check the guarded variable did indeed increment... */\r
731         if( ulGuardedVariable != 1 )\r
732         {\r
733                 xErrorDetected = pdTRUE;\r
734         }\r
735 \r
736         /* ... and that the priority of this task has been disinherited to\r
737         genqMUTEX_TEST_PRIORITY. */\r
738         if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )\r
739         {\r
740                 xErrorDetected = pdTRUE;\r
741         }\r
742 \r
743         /* Set the priority of this task back to its original value, ready for\r
744         the next loop around this test. */\r
745         vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );\r
746 }\r
747 /*-----------------------------------------------------------*/\r
748 \r
749 static void prvTakeTwoMutexesReturnInSameOrder( SemaphoreHandle_t xMutex, SemaphoreHandle_t xLocalMutex )\r
750 {\r
751         /* Take the mutex.  It should be available now. */\r
752         if( xSemaphoreTake( xMutex, intsemNO_BLOCK ) != pdPASS )\r
753         {\r
754                 xErrorDetected = pdTRUE;\r
755         }\r
756 \r
757         /* Set the guarded variable to a known start value. */\r
758         ulGuardedVariable = 0;\r
759 \r
760         /* This task's priority should be as per that assigned when the task was\r
761         created. */\r
762         if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
763         {\r
764                 xErrorDetected = pdTRUE;\r
765         }\r
766 \r
767         /* Now unsuspend the high priority task.  This will attempt to take the\r
768         mutex, and block when it finds it cannot obtain it. */\r
769         vTaskResume( xHighPriorityMutexTask );\r
770 \r
771         #if configUSE_PREEMPTION == 0\r
772                 taskYIELD();\r
773         #endif\r
774 \r
775         /* Ensure the task is reporting its priority as blocked and not\r
776         suspended (as it would have done in versions up to V7.5.3). */\r
777         #if( INCLUDE_eTaskGetState == 1 )\r
778         {\r
779                 configASSERT( eTaskGetState( xHighPriorityMutexTask ) == eBlocked );\r
780         }\r
781         #endif /* INCLUDE_eTaskGetState */\r
782 \r
783         /* This task should now have inherited the priority of the high priority\r
784         task as by now the high priority task will have attempted to obtain the\r
785         mutex. */\r
786         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
787         {\r
788                 xErrorDetected = pdTRUE;\r
789         }\r
790 \r
791         /* Now unsuspend the medium priority task.  This should not run as the\r
792         inherited priority of this task is above that of the medium priority\r
793         task. */\r
794         vTaskResume( xMediumPriorityMutexTask );\r
795 \r
796         /* If the medium priority task did run then it will have incremented the\r
797         guarded variable. */\r
798         if( ulGuardedVariable != 0 )\r
799         {\r
800                 xErrorDetected = pdTRUE;\r
801         }\r
802 \r
803         /* Take the local mutex too, so two mutexes are now held. */\r
804         if( xSemaphoreTake( xLocalMutex, intsemNO_BLOCK ) != pdPASS )\r
805         {\r
806                 xErrorDetected = pdTRUE;\r
807         }\r
808 \r
809         /* When the local semaphore is given back the priority of this task should\r
810         not yet be disinherited because the shared mutex is still held.  This is a\r
811         simplification to allow FreeRTOS to be integrated with middleware that\r
812         attempts to hold multiple mutexes without bloating the code with complex\r
813         algorithms.  It is possible that the high priority mutex task will\r
814         execute as it shares a priority with this task. */\r
815         if( xSemaphoreGive( xLocalMutex ) != pdPASS )\r
816         {\r
817                 xErrorDetected = pdTRUE;\r
818         }\r
819 \r
820         #if configUSE_PREEMPTION == 0\r
821                 taskYIELD();\r
822         #endif\r
823 \r
824         /* The guarded variable is only incremented by the medium priority task,\r
825         which still should not have executed as this task should remain at the\r
826         higher priority, ensure this is the case. */\r
827         if( ulGuardedVariable != 0 )\r
828         {\r
829                 xErrorDetected = pdTRUE;\r
830         }\r
831 \r
832         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
833         {\r
834                 xErrorDetected = pdTRUE;\r
835         }\r
836 \r
837         /* Now also give back the shared mutex, taking the held count back to 0.\r
838         This time the priority of this task should be disinherited back to the\r
839         priority at which it was created.  This means the medium priority task\r
840         should execute and increment the guarded variable.  When this task next runs\r
841         both the high and medium priority tasks will have been suspended again. */\r
842         if( xSemaphoreGive( xMutex ) != pdPASS )\r
843         {\r
844                 xErrorDetected = pdTRUE;\r
845         }\r
846 \r
847         #if configUSE_PREEMPTION == 0\r
848                 taskYIELD();\r
849         #endif\r
850 \r
851         /* Check the guarded variable did indeed increment... */\r
852         if( ulGuardedVariable != 1 )\r
853         {\r
854                 xErrorDetected = pdTRUE;\r
855         }\r
856 \r
857         /* ... and that the priority of this task has been disinherited to\r
858         genqMUTEX_LOW_PRIORITY. */\r
859         if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
860         {\r
861                 xErrorDetected = pdTRUE;\r
862         }\r
863 }\r
864 /*-----------------------------------------------------------*/\r
865 \r
866 static void prvLowPriorityMutexTask( void *pvParameters )\r
867 {\r
868 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters, xLocalMutex;\r
869 \r
870         #ifdef USE_STDIO\r
871         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
872 \r
873                 const char * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";\r
874 \r
875                 /* Queue a message for printing to say the task has started. */\r
876                 vPrintDisplayMessage( &pcTaskStartMsg );\r
877         #endif\r
878 \r
879         /* The local mutex is used to check the 'mutexs held' count. */\r
880         xLocalMutex = xSemaphoreCreateMutex();\r
881         configASSERT( xLocalMutex );\r
882 \r
883         for( ;; )\r
884         {\r
885                 /* The first tests exercise the priority inheritance when two mutexes\r
886                 are taken then returned in a different order to which they were\r
887                 taken. */\r
888                 prvTakeTwoMutexesReturnInDifferentOrder( xMutex, xLocalMutex );\r
889 \r
890                 /* Just to show this task is still running. */\r
891                 ulLoopCounter2++;\r
892 \r
893                 #if configUSE_PREEMPTION == 0\r
894                         taskYIELD();\r
895                 #endif\r
896 \r
897                 /* The second tests exercise the priority inheritance when two mutexes\r
898                 are taken then returned in the same order in which they were taken. */\r
899                 prvTakeTwoMutexesReturnInSameOrder( xMutex, xLocalMutex );\r
900 \r
901                 /* Just to show this task is still running. */\r
902                 ulLoopCounter2++;\r
903 \r
904                 #if configUSE_PREEMPTION == 0\r
905                         taskYIELD();\r
906                 #endif\r
907 \r
908                 #if( INCLUDE_xTaskAbortDelay == 1 )\r
909                 {\r
910                         /* Tests the behaviour when a low priority task inherits the\r
911                         priority of a high priority task only for the high priority task to\r
912                         timeout before obtaining the mutex. */\r
913                         prvHighPriorityTimeout( xMutex );\r
914                 }\r
915                 #endif\r
916         }\r
917 }\r
918 /*-----------------------------------------------------------*/\r
919 \r
920 static void prvMediumPriorityMutexTask( void *pvParameters )\r
921 {\r
922         ( void ) pvParameters;\r
923 \r
924         for( ;; )\r
925         {\r
926                 /* The medium priority task starts by suspending itself.  The low\r
927                 priority task will unsuspend this task when required. */\r
928                 vTaskSuspend( NULL );\r
929 \r
930                 /* When this task unsuspends all it does is increment the guarded\r
931                 variable, this is so the low priority task knows that it has\r
932                 executed. */\r
933                 ulGuardedVariable++;\r
934         }\r
935 }\r
936 /*-----------------------------------------------------------*/\r
937 \r
938 static void prvHighPriorityMutexTask( void *pvParameters )\r
939 {\r
940 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;\r
941 \r
942         for( ;; )\r
943         {\r
944                 /* The high priority task starts by suspending itself.  The low\r
945                 priority task will unsuspend this task when required. */\r
946                 vTaskSuspend( NULL );\r
947 \r
948                 /* When this task unsuspends all it does is attempt to obtain the\r
949                 mutex.  It should find the mutex is not available so a block time is\r
950                 specified. */\r
951                 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )\r
952                 {\r
953                         /* This task would expect to obtain the mutex unless its wait for\r
954                         the mutex was aborted. */\r
955                         if( xBlockWasAborted == pdFALSE )\r
956                         {\r
957                                 xErrorDetected = pdTRUE;\r
958                         }\r
959                         else\r
960                         {\r
961                                 xBlockWasAborted = pdFALSE;\r
962                         }\r
963                 }\r
964                 else\r
965                 {\r
966                         /* When the mutex is eventually obtained it is just given back before\r
967                         returning to suspend ready for the next cycle. */\r
968                         if( xSemaphoreGive( xMutex ) != pdPASS )\r
969                         {\r
970                                 xErrorDetected = pdTRUE;\r
971                         }\r
972                 }\r
973         }\r
974 }\r
975 /*-----------------------------------------------------------*/\r
976 \r
977 \r
978 /* This is called to check that all the created tasks are still running. */\r
979 BaseType_t xAreGenericQueueTasksStillRunning( void )\r
980 {\r
981 static uint32_t ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;\r
982 \r
983         /* If the demo task is still running then we expect the loop counters to\r
984         have incremented since this function was last called. */\r
985         if( ulLastLoopCounter == ulLoopCounter )\r
986         {\r
987                 xErrorDetected = pdTRUE;\r
988         }\r
989 \r
990         if( ulLastLoopCounter2 == ulLoopCounter2 )\r
991         {\r
992                 xErrorDetected = pdTRUE;\r
993         }\r
994 \r
995         ulLastLoopCounter = ulLoopCounter;\r
996         ulLastLoopCounter2 = ulLoopCounter2;\r
997 \r
998         /* Errors detected in the task itself will have latched xErrorDetected\r
999         to true. */\r
1000 \r
1001         return ( BaseType_t ) !xErrorDetected;\r
1002 }\r
1003 \r
1004 \r