FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Minimal / recmutex.c
blob39013915fde499c9dab1e7360206df2eeb91c332
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 The tasks defined on this page demonstrate the use of recursive mutexes.
57 For recursive mutex functionality the created mutex should be created using
58 xSemaphoreCreateRecursiveMutex(), then be manipulated
59 using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
60 functions.
62 This demo creates three tasks all of which access the same recursive mutex:
64 prvRecursiveMutexControllingTask() has the highest priority so executes
65 first and grabs the mutex. It then performs some recursive accesses -
66 between each of which it sleeps for a short period to let the lower
67 priority tasks execute. When it has completed its demo functionality
68 it gives the mutex back before suspending itself.
70 prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
71 a blocking 'take'. The blocking task has a lower priority than the
72 controlling task so by the time it executes the mutex has already been
73 taken by the controlling task, causing the blocking task to block. It
74 does not unblock until the controlling task has given the mutex back,
75 and it does not actually run until the controlling task has suspended
76 itself (due to the relative priorities). When it eventually does obtain
77 the mutex all it does is give the mutex back prior to also suspending
78 itself. At this point both the controlling task and the blocking task are
79 suspended.
81 prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
82 a tight loop attempting to obtain the mutex with a non-blocking call. As
83 the lowest priority task it will not successfully obtain the mutex until
84 both the controlling and blocking tasks are suspended. Once it eventually
85 does obtain the mutex it first unsuspends both the controlling task and
86 blocking task prior to giving the mutex back - resulting in the polling
87 task temporarily inheriting the controlling tasks priority.
90 /* Scheduler include files. */
91 #include "FreeRTOS.h"
92 #include "task.h"
93 #include "semphr.h"
95 /* Demo app include files. */
96 #include "recmutex.h"
98 /* Priorities assigned to the three tasks. */
99 #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
100 #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
101 #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
103 /* The recursive call depth. */
104 #define recmuMAX_COUNT ( 10 )
106 /* Misc. */
107 #define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS )
108 #define recmuNO_DELAY ( ( portTickType ) 0 )
109 #define recmuTWO_TICK_DELAY ( ( portTickType ) 2 )
111 /* The three tasks as described at the top of this file. */
112 static void prvRecursiveMutexControllingTask( void *pvParameters );
113 static void prvRecursiveMutexBlockingTask( void *pvParameters );
114 static void prvRecursiveMutexPollingTask( void *pvParameters );
116 /* The mutex used by the demo. */
117 static xSemaphoreHandle xMutex;
119 /* Variables used to detect and latch errors. */
120 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
121 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0;
123 /* Handles of the two higher priority tasks, required so they can be resumed
124 (unsuspended). */
125 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
127 /*-----------------------------------------------------------*/
129 void vStartRecursiveMutexTasks( void )
131 /* Just creates the mutex and the three tasks. */
133 xMutex = xSemaphoreCreateRecursiveMutex();
135 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
136 in use. The registry is provided as a means for kernel aware
137 debuggers to locate mutex and has no purpose if a kernel aware debugger
138 is not being used. The call to vQueueAddToRegistry() will be removed
139 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
140 defined to be less than 1. */
141 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );
144 if( xMutex != NULL )
146 xTaskCreate( prvRecursiveMutexControllingTask, ( signed portCHAR * ) "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
147 xTaskCreate( prvRecursiveMutexBlockingTask, ( signed portCHAR * ) "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
148 xTaskCreate( prvRecursiveMutexPollingTask, ( signed portCHAR * ) "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
151 /*-----------------------------------------------------------*/
153 static void prvRecursiveMutexControllingTask( void *pvParameters )
155 unsigned portBASE_TYPE ux;
157 /* Just to remove compiler warning. */
158 ( void ) pvParameters;
160 for( ;; )
162 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
163 it. */
164 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
166 xErrorOccurred = pdTRUE;
169 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
171 /* We should now be able to take the mutex as many times as
172 we like. A one tick delay is used so the polling task will
173 inherit our priority on all but the first cycle of this task.
174 If we did not block attempting to receive the mutex then no
175 priority inheritance would occur. */
176 if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )
178 xErrorOccurred = pdTRUE;
181 /* Ensure the other task attempting to access the mutex (and the
182 other demo tasks) are able to execute. */
183 vTaskDelay( recmuSHORT_DELAY );
186 /* For each time we took the mutex, give it back. */
187 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
189 /* Ensure the other task attempting to access the mutex (and the
190 other demo tasks) are able to execute. */
191 vTaskDelay( recmuSHORT_DELAY );
193 /* We should now be able to give the mutex as many times as we
194 took it. */
195 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
197 xErrorOccurred = pdTRUE;
201 /* Having given it back the same number of times as it was taken, we
202 should no longer be the mutex owner, so the next give sh ould fail. */
203 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
205 xErrorOccurred = pdTRUE;
208 /* Keep count of the number of cycles this task has performed so a
209 stall can be detected. */
210 uxControllingCycles++;
212 /* Suspend ourselves to the blocking task can execute. */
213 xControllingIsSuspended = pdTRUE;
214 vTaskSuspend( NULL );
215 xControllingIsSuspended = pdFALSE;
218 /*-----------------------------------------------------------*/
220 static void prvRecursiveMutexBlockingTask( void *pvParameters )
222 /* Just to remove compiler warning. */
223 ( void ) pvParameters;
225 for( ;; )
227 /* Attempt to obtain the mutex. We should block until the
228 controlling task has given up the mutex, and not actually execute
229 past this call until the controlling task is suspended. */
230 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
232 if( xControllingIsSuspended != pdTRUE )
234 /* Did not expect to execute until the controlling task was
235 suspended. */
236 xErrorOccurred = pdTRUE;
238 else
240 /* Give the mutex back before suspending ourselves to allow
241 the polling task to obtain the mutex. */
242 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
244 xErrorOccurred = pdTRUE;
247 xBlockingIsSuspended = pdTRUE;
248 vTaskSuspend( NULL );
249 xBlockingIsSuspended = pdFALSE;
252 else
254 /* We should not leave the xSemaphoreTakeRecursive() function
255 until the mutex was obtained. */
256 xErrorOccurred = pdTRUE;
259 /* The controlling and blocking tasks should be in lock step. */
260 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
262 xErrorOccurred = pdTRUE;
265 /* Keep count of the number of cycles this task has performed so a
266 stall can be detected. */
267 uxBlockingCycles++;
270 /*-----------------------------------------------------------*/
272 static void prvRecursiveMutexPollingTask( void *pvParameters )
274 /* Just to remove compiler warning. */
275 ( void ) pvParameters;
277 for( ;; )
279 /* Keep attempting to obtain the mutex. We should only obtain it when
280 the blocking task has suspended itself. */
281 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
283 /* Is the blocking task suspended? */
284 if( xBlockingIsSuspended != pdTRUE )
286 xErrorOccurred = pdTRUE;
288 else
290 /* Keep count of the number of cycles this task has performed so
291 a stall can be detected. */
292 uxPollingCycles++;
294 /* We can resume the other tasks here even though they have a
295 higher priority than the polling task. When they execute they
296 will attempt to obtain the mutex but fail because the polling
297 task is still the mutex holder. The polling task (this task)
298 will then inherit the higher priority. */
299 vTaskResume( xBlockingTaskHandle );
300 vTaskResume( xControllingTaskHandle );
302 /* Release the mutex, disinheriting the higher priority again. */
303 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
305 xErrorOccurred = pdTRUE;
310 #if configUSE_PREEMPTION == 0
312 taskYIELD();
314 #endif
317 /*-----------------------------------------------------------*/
319 /* This is called to check that all the created tasks are still running. */
320 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
322 portBASE_TYPE xReturn;
323 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
325 /* Is the controlling task still cycling? */
326 if( uxLastControllingCycles == uxControllingCycles )
328 xErrorOccurred = pdTRUE;
330 else
332 uxLastControllingCycles = uxControllingCycles;
335 /* Is the blocking task still cycling? */
336 if( uxLastBlockingCycles == uxBlockingCycles )
338 xErrorOccurred = pdTRUE;
340 else
342 uxLastBlockingCycles = uxBlockingCycles;
345 /* Is the polling task still cycling? */
346 if( uxLastPollingCycles == uxPollingCycles )
348 xErrorOccurred = pdTRUE;
350 else
352 uxLastPollingCycles = uxPollingCycles;
355 if( xErrorOccurred == pdTRUE )
357 xReturn = pdFAIL;
359 else
361 xReturn = pdTRUE;
364 return xReturn;