2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 The tasks defined on this page demonstrate the use of recursive mutexes.
\r
55 For recursive mutex functionality the created mutex should be created using
\r
56 xSemaphoreCreateRecursiveMutex(), then be manipulated
\r
57 using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
\r
60 This demo creates three tasks all of which access the same recursive mutex:
\r
62 prvRecursiveMutexControllingTask() has the highest priority so executes
\r
63 first and grabs the mutex. It then performs some recursive accesses -
\r
64 between each of which it sleeps for a short period to let the lower
\r
65 priority tasks execute. When it has completed its demo functionality
\r
66 it gives the mutex back before suspending itself.
\r
68 prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
\r
69 a blocking 'take'. The blocking task has a lower priority than the
\r
70 controlling task so by the time it executes the mutex has already been
\r
71 taken by the controlling task, causing the blocking task to block. It
\r
72 does not unblock until the controlling task has given the mutex back,
\r
73 and it does not actually run until the controlling task has suspended
\r
74 itself (due to the relative priorities). When it eventually does obtain
\r
75 the mutex all it does is give the mutex back prior to also suspending
\r
76 itself. At this point both the controlling task and the blocking task are
\r
79 prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
\r
80 a tight loop attempting to obtain the mutex with a non-blocking call. As
\r
81 the lowest priority task it will not successfully obtain the mutex until
\r
82 both the controlling and blocking tasks are suspended. Once it eventually
\r
83 does obtain the mutex it first unsuspends both the controlling task and
\r
84 blocking task prior to giving the mutex back - resulting in the polling
\r
85 task temporarily inheriting the controlling tasks priority.
\r
88 /* Scheduler include files. */
\r
89 #include "FreeRTOS.h"
\r
93 /* Demo app include files. */
\r
94 #include "recmutex.h"
\r
96 /* Priorities assigned to the three tasks. */
\r
97 #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
98 #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
99 #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
101 /* The recursive call depth. */
\r
102 #define recmuMAX_COUNT ( 10 )
\r
105 #define recmuSHORT_DELAY ( 20 / portTICK_RATE_MS )
\r
106 #define recmuNO_DELAY ( ( portTickType ) 0 )
\r
107 #define recmuTWO_TICK_DELAY ( ( portTickType ) 2 )
\r
109 /* The three tasks as described at the top of this file. */
\r
110 static void prvRecursiveMutexControllingTask( void *pvParameters );
\r
111 static void prvRecursiveMutexBlockingTask( void *pvParameters );
\r
112 static void prvRecursiveMutexPollingTask( void *pvParameters );
\r
114 /* The mutex used by the demo. */
\r
115 static xSemaphoreHandle xMutex;
\r
117 /* Variables used to detect and latch errors. */
\r
118 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
\r
119 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0;
\r
121 /* Handles of the two higher priority tasks, required so they can be resumed
\r
123 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
\r
125 /*-----------------------------------------------------------*/
\r
127 void vStartRecursiveMutexTasks( void )
\r
129 /* Just creates the mutex and the three tasks. */
\r
131 xMutex = xSemaphoreCreateRecursiveMutex();
\r
133 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
134 in use. The registry is provided as a means for kernel aware
\r
135 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
136 is not being used. The call to vQueueAddToRegistry() will be removed
\r
137 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
138 defined to be less than 1. */
\r
139 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );
\r
142 if( xMutex != NULL )
\r
144 xTaskCreate( prvRecursiveMutexControllingTask, ( signed portCHAR * ) "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
\r
145 xTaskCreate( prvRecursiveMutexBlockingTask, ( signed portCHAR * ) "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
\r
146 xTaskCreate( prvRecursiveMutexPollingTask, ( signed portCHAR * ) "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
\r
149 /*-----------------------------------------------------------*/
\r
151 static void prvRecursiveMutexControllingTask( void *pvParameters )
\r
153 unsigned portBASE_TYPE ux;
\r
155 /* Just to remove compiler warning. */
\r
156 ( void ) pvParameters;
\r
160 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
\r
162 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
164 xErrorOccurred = pdTRUE;
\r
167 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
169 /* We should now be able to take the mutex as many times as
\r
170 we like. A one tick delay is used so the polling task will
\r
171 inherit our priority on all but the first cycle of this task.
\r
172 If we did not block attempting to receive the mutex then no
\r
173 priority inheritance would occur. */
\r
174 if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )
\r
176 xErrorOccurred = pdTRUE;
\r
179 /* Ensure the other task attempting to access the mutex (and the
\r
180 other demo tasks) are able to execute. */
\r
181 vTaskDelay( recmuSHORT_DELAY );
\r
184 /* For each time we took the mutex, give it back. */
\r
185 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
187 /* Ensure the other task attempting to access the mutex (and the
\r
188 other demo tasks) are able to execute. */
\r
189 vTaskDelay( recmuSHORT_DELAY );
\r
191 /* We should now be able to give the mutex as many times as we
\r
193 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
195 xErrorOccurred = pdTRUE;
\r
199 /* Having given it back the same number of times as it was taken, we
\r
200 should no longer be the mutex owner, so the next give sh ould fail. */
\r
201 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
203 xErrorOccurred = pdTRUE;
\r
206 /* Keep count of the number of cycles this task has performed so a
\r
207 stall can be detected. */
\r
208 uxControllingCycles++;
\r
210 /* Suspend ourselves to the blocking task can execute. */
\r
211 xControllingIsSuspended = pdTRUE;
\r
212 vTaskSuspend( NULL );
\r
213 xControllingIsSuspended = pdFALSE;
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvRecursiveMutexBlockingTask( void *pvParameters )
\r
220 /* Just to remove compiler warning. */
\r
221 ( void ) pvParameters;
\r
225 /* Attempt to obtain the mutex. We should block until the
\r
226 controlling task has given up the mutex, and not actually execute
\r
227 past this call until the controlling task is suspended. */
\r
228 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
\r
230 if( xControllingIsSuspended != pdTRUE )
\r
232 /* Did not expect to execute until the controlling task was
\r
234 xErrorOccurred = pdTRUE;
\r
238 /* Give the mutex back before suspending ourselves to allow
\r
239 the polling task to obtain the mutex. */
\r
240 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
242 xErrorOccurred = pdTRUE;
\r
245 xBlockingIsSuspended = pdTRUE;
\r
246 vTaskSuspend( NULL );
\r
247 xBlockingIsSuspended = pdFALSE;
\r
252 /* We should not leave the xSemaphoreTakeRecursive() function
\r
253 until the mutex was obtained. */
\r
254 xErrorOccurred = pdTRUE;
\r
257 /* The controlling and blocking tasks should be in lock step. */
\r
258 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
\r
260 xErrorOccurred = pdTRUE;
\r
263 /* Keep count of the number of cycles this task has performed so a
\r
264 stall can be detected. */
\r
265 uxBlockingCycles++;
\r
268 /*-----------------------------------------------------------*/
\r
270 static void prvRecursiveMutexPollingTask( void *pvParameters )
\r
272 /* Just to remove compiler warning. */
\r
273 ( void ) pvParameters;
\r
277 /* Keep attempting to obtain the mutex. We should only obtain it when
\r
278 the blocking task has suspended itself. */
\r
279 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
\r
281 /* Is the blocking task suspended? */
\r
282 if( xBlockingIsSuspended != pdTRUE )
\r
284 xErrorOccurred = pdTRUE;
\r
288 /* Keep count of the number of cycles this task has performed so
\r
289 a stall can be detected. */
\r
292 /* We can resume the other tasks here even though they have a
\r
293 higher priority than the polling task. When they execute they
\r
294 will attempt to obtain the mutex but fail because the polling
\r
295 task is still the mutex holder. The polling task (this task)
\r
296 will then inherit the higher priority. */
\r
297 vTaskResume( xBlockingTaskHandle );
\r
298 vTaskResume( xControllingTaskHandle );
\r
300 /* Release the mutex, disinheriting the higher priority again. */
\r
301 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
303 xErrorOccurred = pdTRUE;
\r
308 #if configUSE_PREEMPTION == 0
\r
315 /*-----------------------------------------------------------*/
\r
317 /* This is called to check that all the created tasks are still running. */
\r
318 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
\r
320 portBASE_TYPE xReturn;
\r
321 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
\r
323 /* Is the controlling task still cycling? */
\r
324 if( uxLastControllingCycles == uxControllingCycles )
\r
326 xErrorOccurred = pdTRUE;
\r
330 uxLastControllingCycles = uxControllingCycles;
\r
333 /* Is the blocking task still cycling? */
\r
334 if( uxLastBlockingCycles == uxBlockingCycles )
\r
336 xErrorOccurred = pdTRUE;
\r
340 uxLastBlockingCycles = uxBlockingCycles;
\r
343 /* Is the polling task still cycling? */
\r
344 if( uxLastPollingCycles == uxPollingCycles )
\r
346 xErrorOccurred = pdTRUE;
\r
350 uxLastPollingCycles = uxPollingCycles;
\r
353 if( xErrorOccurred == pdTRUE )
\r