2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 The tasks defined on this page demonstrate the use of recursive mutexes.
\r
73 For recursive mutex functionality the created mutex should be created using
\r
74 xSemaphoreCreateRecursiveMutex(), then be manipulated
\r
75 using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
\r
78 This demo creates three tasks all of which access the same recursive mutex:
\r
80 prvRecursiveMutexControllingTask() has the highest priority so executes
\r
81 first and grabs the mutex. It then performs some recursive accesses -
\r
82 between each of which it sleeps for a short period to let the lower
\r
83 priority tasks execute. When it has completed its demo functionality
\r
84 it gives the mutex back before suspending itself.
\r
86 prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
\r
87 a blocking 'take'. The blocking task has a lower priority than the
\r
88 controlling task so by the time it executes the mutex has already been
\r
89 taken by the controlling task, causing the blocking task to block. It
\r
90 does not unblock until the controlling task has given the mutex back,
\r
91 and it does not actually run until the controlling task has suspended
\r
92 itself (due to the relative priorities). When it eventually does obtain
\r
93 the mutex all it does is give the mutex back prior to also suspending
\r
94 itself. At this point both the controlling task and the blocking task are
\r
97 prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
\r
98 a tight loop attempting to obtain the mutex with a non-blocking call. As
\r
99 the lowest priority task it will not successfully obtain the mutex until
\r
100 both the controlling and blocking tasks are suspended. Once it eventually
\r
101 does obtain the mutex it first unsuspends both the controlling task and
\r
102 blocking task prior to giving the mutex back - resulting in the polling
\r
103 task temporarily inheriting the controlling tasks priority.
\r
106 /* Scheduler include files. */
\r
107 #include "FreeRTOS.h"
\r
109 #include "semphr.h"
\r
111 /* Demo app include files. */
\r
112 #include "recmutex.h"
\r
114 /* Priorities assigned to the three tasks. recmuCONTROLLING_TASK_PRIORITY can
\r
115 be overridden by a definition in FreeRTOSConfig.h. */
\r
116 #ifndef recmuCONTROLLING_TASK_PRIORITY
\r
117 #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
120 #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
122 /* The recursive call depth. */
\r
123 #define recmuMAX_COUNT ( 10 )
\r
126 #define recmuSHORT_DELAY ( pdMS_TO_TICKS( 20 ) )
\r
127 #define recmuNO_DELAY ( ( TickType_t ) 0 )
\r
128 #define recmu15ms_DELAY ( pdMS_TO_TICKS( 15 ) )
\r
130 /* The three tasks as described at the top of this file. */
\r
131 static void prvRecursiveMutexControllingTask( void *pvParameters );
\r
132 static void prvRecursiveMutexBlockingTask( void *pvParameters );
\r
133 static void prvRecursiveMutexPollingTask( void *pvParameters );
\r
135 /* The mutex used by the demo. */
\r
136 static SemaphoreHandle_t xMutex;
\r
138 /* Variables used to detect and latch errors. */
\r
139 static volatile BaseType_t xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
\r
140 static volatile UBaseType_t uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;
\r
142 /* Handles of the two higher priority tasks, required so they can be resumed
\r
144 static TaskHandle_t xControllingTaskHandle, xBlockingTaskHandle;
\r
146 /*-----------------------------------------------------------*/
\r
148 void vStartRecursiveMutexTasks( void )
\r
150 /* Just creates the mutex and the three tasks. */
\r
152 xMutex = xSemaphoreCreateRecursiveMutex();
\r
154 if( xMutex != NULL )
\r
156 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
157 in use. The registry is provided as a means for kernel aware
\r
158 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
159 is not being used. The call to vQueueAddToRegistry() will be removed
\r
160 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
161 defined to be less than 1. */
\r
162 vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Recursive_Mutex" );
\r
164 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
\r
165 xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
\r
166 xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
\r
169 /*-----------------------------------------------------------*/
\r
171 static void prvRecursiveMutexControllingTask( void *pvParameters )
\r
175 /* Just to remove compiler warning. */
\r
176 ( void ) pvParameters;
\r
180 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
\r
181 it. The first time through, the mutex will not have been used yet,
\r
182 subsequent times through, at this point the mutex will be held by the
\r
184 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
186 xErrorOccurred = pdTRUE;
\r
189 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
191 /* We should now be able to take the mutex as many times as
\r
194 The first time through the mutex will be immediately available, on
\r
195 subsequent times through the mutex will be held by the polling task
\r
196 at this point and this Take will cause the polling task to inherit
\r
197 the priority of this task. In this case the block time must be
\r
198 long enough to ensure the polling task will execute again before the
\r
199 block time expires. If the block time does expire then the error
\r
200 flag will be set here. */
\r
201 if( xSemaphoreTakeRecursive( xMutex, recmu15ms_DELAY ) != pdPASS )
\r
203 xErrorOccurred = pdTRUE;
\r
206 /* Ensure the other task attempting to access the mutex (and the
\r
207 other demo tasks) are able to execute to ensure they either block
\r
208 (where a block time is specified) or return an error (where no
\r
209 block time is specified) as the mutex is held by this task. */
\r
210 vTaskDelay( recmuSHORT_DELAY );
\r
213 /* For each time we took the mutex, give it back. */
\r
214 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
216 /* Ensure the other task attempting to access the mutex (and the
\r
217 other demo tasks) are able to execute. */
\r
218 vTaskDelay( recmuSHORT_DELAY );
\r
220 /* We should now be able to give the mutex as many times as we
\r
221 took it. When the mutex is available again the Blocking task
\r
222 should be unblocked but not run because it has a lower priority
\r
223 than this task. The polling task should also not run at this point
\r
224 as it too has a lower priority than this task. */
\r
225 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
227 xErrorOccurred = pdTRUE;
\r
230 #if( configUSE_PREEMPTION == 0 )
\r
235 /* Having given it back the same number of times as it was taken, we
\r
236 should no longer be the mutex owner, so the next give should fail. */
\r
237 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
239 xErrorOccurred = pdTRUE;
\r
242 /* Keep count of the number of cycles this task has performed so a
\r
243 stall can be detected. */
\r
244 uxControllingCycles++;
\r
246 /* Suspend ourselves so the blocking task can execute. */
\r
247 xControllingIsSuspended = pdTRUE;
\r
248 vTaskSuspend( NULL );
\r
249 xControllingIsSuspended = pdFALSE;
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvRecursiveMutexBlockingTask( void *pvParameters )
\r
256 /* Just to remove compiler warning. */
\r
257 ( void ) pvParameters;
\r
261 /* This task will run while the controlling task is blocked, and the
\r
262 controlling task will block only once it has the mutex - therefore
\r
263 this call should block until the controlling task has given up the
\r
264 mutex, and not actually execute past this call until the controlling
\r
265 task is suspended. portMAX_DELAY - 1 is used instead of portMAX_DELAY
\r
266 to ensure the task's state is reported as Blocked and not Suspended in
\r
267 a later call to configASSERT() (within the polling task). */
\r
268 if( xSemaphoreTakeRecursive( xMutex, ( portMAX_DELAY - 1 ) ) == pdPASS )
\r
270 if( xControllingIsSuspended != pdTRUE )
\r
272 /* Did not expect to execute until the controlling task was
\r
274 xErrorOccurred = pdTRUE;
\r
278 /* Give the mutex back before suspending ourselves to allow
\r
279 the polling task to obtain the mutex. */
\r
280 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
282 xErrorOccurred = pdTRUE;
\r
285 xBlockingIsSuspended = pdTRUE;
\r
286 vTaskSuspend( NULL );
\r
287 xBlockingIsSuspended = pdFALSE;
\r
292 /* We should not leave the xSemaphoreTakeRecursive() function
\r
293 until the mutex was obtained. */
\r
294 xErrorOccurred = pdTRUE;
\r
297 /* The controlling and blocking tasks should be in lock step. */
\r
298 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
\r
300 xErrorOccurred = pdTRUE;
\r
303 /* Keep count of the number of cycles this task has performed so a
\r
304 stall can be detected. */
\r
305 uxBlockingCycles++;
\r
308 /*-----------------------------------------------------------*/
\r
310 static void prvRecursiveMutexPollingTask( void *pvParameters )
\r
312 /* Just to remove compiler warning. */
\r
313 ( void ) pvParameters;
\r
317 /* Keep attempting to obtain the mutex. We should only obtain it when
\r
318 the blocking task has suspended itself, which in turn should only
\r
319 happen when the controlling task is also suspended. */
\r
320 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
\r
322 #if( INCLUDE_eTaskGetState == 1 )
\r
324 configASSERT( eTaskGetState( xControllingTaskHandle ) == eSuspended );
\r
325 configASSERT( eTaskGetState( xBlockingTaskHandle ) == eSuspended );
\r
327 #endif /* INCLUDE_eTaskGetState */
\r
329 /* Is the blocking task suspended? */
\r
330 if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )
\r
332 xErrorOccurred = pdTRUE;
\r
336 /* Keep count of the number of cycles this task has performed
\r
337 so a stall can be detected. */
\r
340 /* We can resume the other tasks here even though they have a
\r
341 higher priority than the polling task. When they execute they
\r
342 will attempt to obtain the mutex but fail because the polling
\r
343 task is still the mutex holder. The polling task (this task)
\r
344 will then inherit the higher priority. The Blocking task will
\r
345 block indefinitely when it attempts to obtain the mutex, the
\r
346 Controlling task will only block for a fixed period and an
\r
347 error will be latched if the polling task has not returned the
\r
348 mutex by the time this fixed period has expired. */
\r
349 vTaskResume( xBlockingTaskHandle );
\r
350 #if( configUSE_PREEMPTION == 0 )
\r
354 vTaskResume( xControllingTaskHandle );
\r
355 #if( configUSE_PREEMPTION == 0 )
\r
359 /* The other two tasks should now have executed and no longer
\r
361 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )
\r
363 xErrorOccurred = pdTRUE;
\r
366 #if( INCLUDE_uxTaskPriorityGet == 1 )
\r
368 /* Check priority inherited. */
\r
369 configASSERT( uxTaskPriorityGet( NULL ) == recmuCONTROLLING_TASK_PRIORITY );
\r
371 #endif /* INCLUDE_uxTaskPriorityGet */
\r
373 #if( INCLUDE_eTaskGetState == 1 )
\r
375 configASSERT( eTaskGetState( xControllingTaskHandle ) == eBlocked );
\r
376 configASSERT( eTaskGetState( xBlockingTaskHandle ) == eBlocked );
\r
378 #endif /* INCLUDE_eTaskGetState */
\r
380 /* Release the mutex, disinheriting the higher priority again. */
\r
381 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
383 xErrorOccurred = pdTRUE;
\r
386 #if( INCLUDE_uxTaskPriorityGet == 1 )
\r
388 /* Check priority disinherited. */
\r
389 configASSERT( uxTaskPriorityGet( NULL ) == recmuPOLLING_TASK_PRIORITY );
\r
391 #endif /* INCLUDE_uxTaskPriorityGet */
\r
395 #if configUSE_PREEMPTION == 0
\r
402 /*-----------------------------------------------------------*/
\r
404 /* This is called to check that all the created tasks are still running. */
\r
405 BaseType_t xAreRecursiveMutexTasksStillRunning( void )
\r
407 BaseType_t xReturn;
\r
408 static UBaseType_t uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
\r
410 /* Is the controlling task still cycling? */
\r
411 if( uxLastControllingCycles == uxControllingCycles )
\r
413 xErrorOccurred = pdTRUE;
\r
417 uxLastControllingCycles = uxControllingCycles;
\r
420 /* Is the blocking task still cycling? */
\r
421 if( uxLastBlockingCycles == uxBlockingCycles )
\r
423 xErrorOccurred = pdTRUE;
\r
427 uxLastBlockingCycles = uxBlockingCycles;
\r
430 /* Is the polling task still cycling? */
\r
431 if( uxLastPollingCycles == uxPollingCycles )
\r
433 xErrorOccurred = pdTRUE;
\r
437 uxLastPollingCycles = uxPollingCycles;
\r
440 if( xErrorOccurred == pdTRUE )
\r