2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 The tasks defined on this page demonstrate the use of recursive mutexes.
\r
68 For recursive mutex functionality the created mutex should be created using
\r
69 xSemaphoreCreateRecursiveMutex(), then be manipulated
\r
70 using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
\r
73 This demo creates three tasks all of which access the same recursive mutex:
\r
75 prvRecursiveMutexControllingTask() has the highest priority so executes
\r
76 first and grabs the mutex. It then performs some recursive accesses -
\r
77 between each of which it sleeps for a short period to let the lower
\r
78 priority tasks execute. When it has completed its demo functionality
\r
79 it gives the mutex back before suspending itself.
\r
81 prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
\r
82 a blocking 'take'. The blocking task has a lower priority than the
\r
83 controlling task so by the time it executes the mutex has already been
\r
84 taken by the controlling task, causing the blocking task to block. It
\r
85 does not unblock until the controlling task has given the mutex back,
\r
86 and it does not actually run until the controlling task has suspended
\r
87 itself (due to the relative priorities). When it eventually does obtain
\r
88 the mutex all it does is give the mutex back prior to also suspending
\r
89 itself. At this point both the controlling task and the blocking task are
\r
92 prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
\r
93 a tight loop attempting to obtain the mutex with a non-blocking call. As
\r
94 the lowest priority task it will not successfully obtain the mutex until
\r
95 both the controlling and blocking tasks are suspended. Once it eventually
\r
96 does obtain the mutex it first unsuspends both the controlling task and
\r
97 blocking task prior to giving the mutex back - resulting in the polling
\r
98 task temporarily inheriting the controlling tasks priority.
\r
101 /* Scheduler include files. */
\r
102 #include "FreeRTOS.h"
\r
104 #include "semphr.h"
\r
106 /* Demo app include files. */
\r
107 #include "recmutex.h"
\r
109 /* Priorities assigned to the three tasks. */
\r
110 #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
111 #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
112 #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
114 /* The recursive call depth. */
\r
115 #define recmuMAX_COUNT ( 10 )
\r
118 #define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS )
\r
119 #define recmuNO_DELAY ( ( portTickType ) 0 )
\r
120 #define recmuTWO_TICK_DELAY ( ( portTickType ) 2 )
\r
122 /* The three tasks as described at the top of this file. */
\r
123 static void prvRecursiveMutexControllingTask( void *pvParameters );
\r
124 static void prvRecursiveMutexBlockingTask( void *pvParameters );
\r
125 static void prvRecursiveMutexPollingTask( void *pvParameters );
\r
127 /* The mutex used by the demo. */
\r
128 static xSemaphoreHandle xMutex;
\r
130 /* Variables used to detect and latch errors. */
\r
131 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
\r
132 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;
\r
134 /* Handles of the two higher priority tasks, required so they can be resumed
\r
136 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
\r
138 /*-----------------------------------------------------------*/
\r
140 void vStartRecursiveMutexTasks( void )
\r
142 /* Just creates the mutex and the three tasks. */
\r
144 xMutex = xSemaphoreCreateRecursiveMutex();
\r
146 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
147 in use. The registry is provided as a means for kernel aware
\r
148 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
149 is not being used. The call to vQueueAddToRegistry() will be removed
\r
150 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
151 defined to be less than 1. */
\r
152 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );
\r
155 if( xMutex != NULL )
\r
157 xTaskCreate( prvRecursiveMutexControllingTask, ( signed portCHAR * ) "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
\r
158 xTaskCreate( prvRecursiveMutexBlockingTask, ( signed portCHAR * ) "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
\r
159 xTaskCreate( prvRecursiveMutexPollingTask, ( signed portCHAR * ) "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
\r
162 /*-----------------------------------------------------------*/
\r
164 static void prvRecursiveMutexControllingTask( void *pvParameters )
\r
166 unsigned portBASE_TYPE ux;
\r
168 /* Just to remove compiler warning. */
\r
169 ( void ) pvParameters;
\r
173 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
\r
174 it. The first time through, the mutex will not have been used yet,
\r
175 subsequent times through, at this point the mutex will be held by the
\r
177 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
179 xErrorOccurred = pdTRUE;
\r
182 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
184 /* We should now be able to take the mutex as many times as
\r
187 The first time through the mutex will be immediately available, on
\r
188 subsequent times through the mutex will be held by the polling task
\r
189 at this point and this Take will cause the polling task to inherit
\r
190 the priority of this task. In this case the block time must be
\r
191 long enough to ensure the polling task will execute again before the
\r
192 block time expires. If the block time does expire then the error
\r
193 flag will be set here. */
\r
194 if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )
\r
196 xErrorOccurred = pdTRUE;
\r
199 /* Ensure the other task attempting to access the mutex (and the
\r
200 other demo tasks) are able to execute to ensure they either block
\r
201 (where a block time is specified) or return an error (where no
\r
202 block time is specified) as the mutex is held by this task. */
\r
203 vTaskDelay( recmuSHORT_DELAY );
\r
206 /* For each time we took the mutex, give it back. */
\r
207 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
209 /* Ensure the other task attempting to access the mutex (and the
\r
210 other demo tasks) are able to execute. */
\r
211 vTaskDelay( recmuSHORT_DELAY );
\r
213 /* We should now be able to give the mutex as many times as we
\r
214 took it. When the mutex is available again the Blocking task
\r
215 should be unblocked but not run because it has a lower priority
\r
216 than this task. The polling task should also not run at this point
\r
217 as it too has a lower priority than this task. */
\r
218 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
220 xErrorOccurred = pdTRUE;
\r
224 /* Having given it back the same number of times as it was taken, we
\r
225 should no longer be the mutex owner, so the next give sh ould fail. */
\r
226 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
228 xErrorOccurred = pdTRUE;
\r
231 /* Keep count of the number of cycles this task has performed so a
\r
232 stall can be detected. */
\r
233 uxControllingCycles++;
\r
235 /* Suspend ourselves to the blocking task can execute. */
\r
236 xControllingIsSuspended = pdTRUE;
\r
237 vTaskSuspend( NULL );
\r
238 xControllingIsSuspended = pdFALSE;
\r
241 /*-----------------------------------------------------------*/
\r
243 static void prvRecursiveMutexBlockingTask( void *pvParameters )
\r
245 /* Just to remove compiler warning. */
\r
246 ( void ) pvParameters;
\r
250 /* This task will run while the controlling task is blocked, and the
\r
251 controlling task will block only once it has the mutex - therefore
\r
252 this call should block until the controlling task has given up the
\r
253 mutex, and not actually execute past this call until the controlling
\r
254 task is suspended. */
\r
255 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
\r
257 if( xControllingIsSuspended != pdTRUE )
\r
259 /* Did not expect to execute until the controlling task was
\r
261 xErrorOccurred = pdTRUE;
\r
265 /* Give the mutex back before suspending ourselves to allow
\r
266 the polling task to obtain the mutex. */
\r
267 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
269 xErrorOccurred = pdTRUE;
\r
272 xBlockingIsSuspended = pdTRUE;
\r
273 vTaskSuspend( NULL );
\r
274 xBlockingIsSuspended = pdFALSE;
\r
279 /* We should not leave the xSemaphoreTakeRecursive() function
\r
280 until the mutex was obtained. */
\r
281 xErrorOccurred = pdTRUE;
\r
284 /* The controlling and blocking tasks should be in lock step. */
\r
285 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
\r
287 xErrorOccurred = pdTRUE;
\r
290 /* Keep count of the number of cycles this task has performed so a
\r
291 stall can be detected. */
\r
292 uxBlockingCycles++;
\r
295 /*-----------------------------------------------------------*/
\r
297 static void prvRecursiveMutexPollingTask( void *pvParameters )
\r
299 /* Just to remove compiler warning. */
\r
300 ( void ) pvParameters;
\r
304 /* Keep attempting to obtain the mutex. We should only obtain it when
\r
305 the blocking task has suspended itself, which in turn should only
\r
306 happen when the controlling task is also suspended. */
\r
307 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
\r
309 /* Is the blocking task suspended? */
\r
310 if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )
\r
312 xErrorOccurred = pdTRUE;
\r
316 /* Keep count of the number of cycles this task has performed
\r
317 so a stall can be detected. */
\r
320 /* We can resume the other tasks here even though they have a
\r
321 higher priority than the polling task. When they execute they
\r
322 will attempt to obtain the mutex but fail because the polling
\r
323 task is still the mutex holder. The polling task (this task)
\r
324 will then inherit the higher priority. The Blocking task will
\r
325 block indefinitely when it attempts to obtain the mutex, the
\r
326 Controlling task will only block for a fixed period and an
\r
327 error will be latched if the polling task has not returned the
\r
328 mutex by the time this fixed period has expired. */
\r
329 vTaskResume( xBlockingTaskHandle );
\r
330 vTaskResume( xControllingTaskHandle );
\r
332 /* The other two tasks should now have executed and no longer
\r
334 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )
\r
336 xErrorOccurred = pdTRUE;
\r
339 /* Release the mutex, disinheriting the higher priority again. */
\r
340 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
342 xErrorOccurred = pdTRUE;
\r
347 #if configUSE_PREEMPTION == 0
\r
354 /*-----------------------------------------------------------*/
\r
356 /* This is called to check that all the created tasks are still running. */
\r
357 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
\r
359 portBASE_TYPE xReturn;
\r
360 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
\r
362 /* Is the controlling task still cycling? */
\r
363 if( uxLastControllingCycles == uxControllingCycles )
\r
365 xErrorOccurred = pdTRUE;
\r
369 uxLastControllingCycles = uxControllingCycles;
\r
372 /* Is the blocking task still cycling? */
\r
373 if( uxLastBlockingCycles == uxBlockingCycles )
\r
375 xErrorOccurred = pdTRUE;
\r
379 uxLastBlockingCycles = uxBlockingCycles;
\r
382 /* Is the polling task still cycling? */
\r
383 if( uxLastPollingCycles == uxPollingCycles )
\r
385 xErrorOccurred = pdTRUE;
\r
389 uxLastPollingCycles = uxPollingCycles;
\r
392 if( xErrorOccurred == pdTRUE )
\r