2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 The tasks defined on this page demonstrate the use of recursive mutexes.
\r
70 For recursive mutex functionality the created mutex should be created using
\r
71 xSemaphoreCreateRecursiveMutex(), then be manipulated
\r
72 using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
\r
75 This demo creates three tasks all of which access the same recursive mutex:
\r
77 prvRecursiveMutexControllingTask() has the highest priority so executes
\r
78 first and grabs the mutex. It then performs some recursive accesses -
\r
79 between each of which it sleeps for a short period to let the lower
\r
80 priority tasks execute. When it has completed its demo functionality
\r
81 it gives the mutex back before suspending itself.
\r
83 prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
\r
84 a blocking 'take'. The blocking task has a lower priority than the
\r
85 controlling task so by the time it executes the mutex has already been
\r
86 taken by the controlling task, causing the blocking task to block. It
\r
87 does not unblock until the controlling task has given the mutex back,
\r
88 and it does not actually run until the controlling task has suspended
\r
89 itself (due to the relative priorities). When it eventually does obtain
\r
90 the mutex all it does is give the mutex back prior to also suspending
\r
91 itself. At this point both the controlling task and the blocking task are
\r
94 prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
\r
95 a tight loop attempting to obtain the mutex with a non-blocking call. As
\r
96 the lowest priority task it will not successfully obtain the mutex until
\r
97 both the controlling and blocking tasks are suspended. Once it eventually
\r
98 does obtain the mutex it first unsuspends both the controlling task and
\r
99 blocking task prior to giving the mutex back - resulting in the polling
\r
100 task temporarily inheriting the controlling tasks priority.
\r
103 /* Scheduler include files. */
\r
104 #include "FreeRTOS.h"
\r
106 #include "semphr.h"
\r
108 /* Demo app include files. */
\r
109 #include "recmutex.h"
\r
111 /* Priorities assigned to the three tasks. */
\r
112 #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
113 #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
114 #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
116 /* In this version the tick period is very long, so the short delay cannot be
\r
117 for too many ticks, or the check task will execute and find that the recmutex
\r
118 tasks have not completed their functionality and then signal an error. The
\r
119 delay does however have to be long enough to allow the lower priority tasks
\r
120 a chance of executing - this is basically achieved by reducing the number
\r
121 of times the loop that takes/gives the recursive mutex executes. */
\r
122 #define recmuMAX_COUNT ( 2 )
\r
123 #define recmuSHORT_DELAY ( 20 )
\r
124 #define recmuNO_DELAY ( ( portTickType ) 0 )
\r
125 #define recmuFIVE_TICK_DELAY ( ( portTickType ) 5 )
\r
127 /* The three tasks as described at the top of this file. */
\r
128 static void prvRecursiveMutexControllingTask( void *pvParameters );
\r
129 static void prvRecursiveMutexBlockingTask( void *pvParameters );
\r
130 static void prvRecursiveMutexPollingTask( void *pvParameters );
\r
132 /* The mutex used by the demo. */
\r
133 static xSemaphoreHandle xMutex;
\r
135 /* Variables used to detect and latch errors. */
\r
136 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
\r
137 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;
\r
139 /* Handles of the two higher priority tasks, required so they can be resumed
\r
141 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
\r
143 /*-----------------------------------------------------------*/
\r
145 void vStartRecursiveMutexTasks( void )
\r
147 /* Just creates the mutex and the three tasks. */
\r
149 xMutex = xSemaphoreCreateRecursiveMutex();
\r
151 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
152 in use. The registry is provided as a means for kernel aware
\r
153 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
154 is not being used. The call to vQueueAddToRegistry() will be removed
\r
155 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
156 defined to be less than 1. */
\r
157 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );
\r
160 if( xMutex != NULL )
\r
162 xTaskCreate( prvRecursiveMutexControllingTask, ( signed portCHAR * ) "Rec1Ctrl", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
\r
163 xTaskCreate( prvRecursiveMutexBlockingTask, ( signed portCHAR * ) "Rec2Blck", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
\r
164 xTaskCreate( prvRecursiveMutexPollingTask, ( signed portCHAR * ) "Rec3Poll", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
\r
167 /*-----------------------------------------------------------*/
\r
169 static void prvRecursiveMutexControllingTask( void *pvParameters )
\r
171 unsigned portBASE_TYPE ux;
\r
173 /* Just to remove compiler warning. */
\r
174 ( void ) pvParameters;
\r
178 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
\r
179 it. The first time through, the mutex will not have been used yet,
\r
180 subsequent times through, at this point the mutex will be held by the
\r
182 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
184 xErrorOccurred = pdTRUE;
\r
187 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
189 /* We should now be able to take the mutex as many times as
\r
192 The first time through the mutex will be immediately available, on
\r
193 subsequent times through the mutex will be held by the polling task
\r
194 at this point and this Take will cause the polling task to inherit
\r
195 the priority of this task. In this case the block time must be
\r
196 long enough to ensure the polling task will execute again before the
\r
197 block time expires. If the block time does expire then the error
\r
198 flag will be set here. */
\r
199 if( xSemaphoreTakeRecursive( xMutex, recmuFIVE_TICK_DELAY ) != pdPASS )
\r
201 xErrorOccurred = pdTRUE;
\r
204 /* Ensure the other task attempting to access the mutex (and the
\r
205 other demo tasks) are able to execute to ensure they either block
\r
206 (where a block time is specified) or return an error (where no
\r
207 block time is specified) as the mutex is held by this task. */
\r
208 vTaskDelay( recmuSHORT_DELAY );
\r
211 /* For each time we took the mutex, give it back. */
\r
212 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
214 /* Ensure the other task attempting to access the mutex (and the
\r
215 other demo tasks) are able to execute. */
\r
216 vTaskDelay( recmuSHORT_DELAY );
\r
218 /* We should now be able to give the mutex as many times as we
\r
219 took it. When the mutex is available again the Blocking task
\r
220 should be unblocked but not run because it has a lower priority
\r
221 than this task. The polling task should also not run at this point
\r
222 as it too has a lower priority than this task. */
\r
223 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
225 xErrorOccurred = pdTRUE;
\r
229 /* Having given it back the same number of times as it was taken, we
\r
230 should no longer be the mutex owner, so the next give should fail. */
\r
231 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
233 xErrorOccurred = pdTRUE;
\r
236 /* Keep count of the number of cycles this task has performed so a
\r
237 stall can be detected. */
\r
238 uxControllingCycles++;
\r
240 /* Suspend ourselves so the blocking task can execute. */
\r
241 xControllingIsSuspended = pdTRUE;
\r
242 vTaskSuspend( NULL );
\r
243 xControllingIsSuspended = pdFALSE;
\r
246 /*-----------------------------------------------------------*/
\r
248 static void prvRecursiveMutexBlockingTask( void *pvParameters )
\r
250 /* Just to remove compiler warning. */
\r
251 ( void ) pvParameters;
\r
255 /* This task will run while the controlling task is blocked, and the
\r
256 controlling task will block only once it has the mutex - therefore
\r
257 this call should block until the controlling task has given up the
\r
258 mutex, and not actually execute past this call until the controlling
\r
259 task is suspended. */
\r
260 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
\r
262 if( xControllingIsSuspended != pdTRUE )
\r
264 /* Did not expect to execute until the controlling task was
\r
266 xErrorOccurred = pdTRUE;
\r
270 /* Give the mutex back before suspending ourselves to allow
\r
271 the polling task to obtain the mutex. */
\r
272 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
274 xErrorOccurred = pdTRUE;
\r
277 xBlockingIsSuspended = pdTRUE;
\r
278 vTaskSuspend( NULL );
\r
279 xBlockingIsSuspended = pdFALSE;
\r
284 /* We should not leave the xSemaphoreTakeRecursive() function
\r
285 until the mutex was obtained. */
\r
286 xErrorOccurred = pdTRUE;
\r
289 /* The controlling and blocking tasks should be in lock step. */
\r
290 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
\r
292 xErrorOccurred = pdTRUE;
\r
295 /* Keep count of the number of cycles this task has performed so a
\r
296 stall can be detected. */
\r
297 uxBlockingCycles++;
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvRecursiveMutexPollingTask( void *pvParameters )
\r
304 /* Just to remove compiler warning. */
\r
305 ( void ) pvParameters;
\r
309 /* Keep attempting to obtain the mutex. We should only obtain it when
\r
310 the blocking task has suspended itself, which in turn should only
\r
311 happen when the controlling task is also suspended. */
\r
312 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
\r
314 /* Is the blocking task suspended? */
\r
315 if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )
\r
317 xErrorOccurred = pdTRUE;
\r
321 /* Keep count of the number of cycles this task has performed
\r
322 so a stall can be detected. */
\r
325 /* We can resume the other tasks here even though they have a
\r
326 higher priority than the polling task. When they execute they
\r
327 will attempt to obtain the mutex but fail because the polling
\r
328 task is still the mutex holder. The polling task (this task)
\r
329 will then inherit the higher priority. The Blocking task will
\r
330 block indefinitely when it attempts to obtain the mutex, the
\r
331 Controlling task will only block for a fixed period and an
\r
332 error will be latched if the polling task has not returned the
\r
333 mutex by the time this fixed period has expired. */
\r
334 vTaskResume( xBlockingTaskHandle );
\r
335 vTaskResume( xControllingTaskHandle );
\r
337 /* The other two tasks should now have executed and no longer
\r
339 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )
\r
341 xErrorOccurred = pdTRUE;
\r
344 /* Release the mutex, disinheriting the higher priority again. */
\r
345 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
347 xErrorOccurred = pdTRUE;
\r
352 #if configUSE_PREEMPTION == 0
\r
359 /*-----------------------------------------------------------*/
\r
361 /* This is called to check that all the created tasks are still running. */
\r
362 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
\r
364 portBASE_TYPE xReturn;
\r
365 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
\r
367 /* Is the controlling task still cycling? */
\r
368 if( uxLastControllingCycles == uxControllingCycles )
\r
370 xErrorOccurred = pdTRUE;
\r
374 uxLastControllingCycles = uxControllingCycles;
\r
377 /* Is the blocking task still cycling? */
\r
378 if( uxLastBlockingCycles == uxBlockingCycles )
\r
380 xErrorOccurred = pdTRUE;
\r
384 uxLastBlockingCycles = uxBlockingCycles;
\r
387 /* Is the polling task still cycling? */
\r
388 if( uxLastPollingCycles == uxPollingCycles )
\r
390 xErrorOccurred = pdTRUE;
\r
394 uxLastPollingCycles = uxPollingCycles;
\r
397 if( xErrorOccurred == pdTRUE )
\r