2 FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 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
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
38 The tasks defined on this page demonstrate the use of recursive mutexes.
\r
40 All mutexes are created using a call to xSemaphoreCreateMutex(). For
\r
41 recursive mutex functionality the created mutex should then be manipulated
\r
42 using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
\r
43 functions. Recursive mutexes must NOT be passed as a parameter to the
\r
44 standard mutex API functions xSemaphoreTake() and xSemaphoreGive().
\r
46 This demo creates three tasks all of which access the same recursive mutex:
\r
48 prvRecursiveMutexControllingTask() has the highest priority so executes
\r
49 first and grabs the mutex. It then performs some recursive accesses -
\r
50 between each of which it sleeps for a short period to let the lower
\r
51 priority tasks execute. When it has completed its demo functionality
\r
52 it gives the mutex back before suspending itself.
\r
54 prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
\r
55 a blocking 'take'. The blocking task has a lower priority than the
\r
56 controlling task so by the time it executes the mutex has already been
\r
57 taken by the controlling task, causing the blocking task to block. It
\r
58 does not unblock until the controlling task has given the mutex back,
\r
59 and it does not actually run until the controlling task has suspended
\r
60 itself (due to the relative priorities). When it eventually does obtain
\r
61 the mutex all it does is give the mutex back prior to also suspending
\r
62 itself. At this point both the controlling task and the blocking task are
\r
65 prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
\r
66 a tight loop attempting to obtain the mutex with a non-blocking call. As
\r
67 the lowest priority task it will not successfully obtain the mutex until
\r
68 both the controlling and blocking tasks are suspended. Once it eventually
\r
69 does obtain the mutex it first unsuspends both the controlling task and
\r
70 blocking task prior to giving the mutex back - resulting in the polling
\r
71 task temporarily inheriting the controlling tasks priority.
\r
74 /* Scheduler include files. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Demo app include files. */
\r
80 #include "recmutex.h"
\r
82 /* Priorities assigned to the three tasks. */
\r
83 #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
84 #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
85 #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
87 /* The recursive call depth. */
\r
88 #define recmuMAX_COUNT ( 10 )
\r
91 #define recmuSHORT_DELAY ( 5 / portTICK_RATE_MS )
\r
92 #define recmuNO_DELAY ( ( portTickType ) 0 )
\r
93 #define recmuONE_TICK_DELAY ( ( portTickType ) 1 )
\r
95 /* The three tasks as described at the top of this file. */
\r
96 static void prvRecursiveMutexControllingTask( void *pvParameters );
\r
97 static void prvRecursiveMutexBlockingTask( void *pvParameters );
\r
98 static void prvRecursiveMutexPollingTask( void *pvParameters );
\r
100 /* The mutex used by the demo. */
\r
101 static xSemaphoreHandle xMutex;
\r
103 /* Variables used to detect and latch errors. */
\r
104 static portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
\r
105 static unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0;
\r
107 /* Handles of the two higher priority tasks, required so they can be resumed
\r
109 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
\r
111 /*-----------------------------------------------------------*/
\r
113 void vStartRecursiveMutexTasks( void )
\r
115 /* Just creates the mutex and the three tasks. */
\r
117 xMutex = xSemaphoreCreateMutex();
\r
119 if( xMutex != NULL )
\r
121 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
\r
122 xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
\r
123 xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
\r
126 /*-----------------------------------------------------------*/
\r
128 static void prvRecursiveMutexControllingTask( void *pvParameters )
\r
130 unsigned portBASE_TYPE ux;
\r
134 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
\r
136 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
138 xErrorOccurred = pdTRUE;
\r
141 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
143 /* We should now be able to take the mutex as many times as
\r
144 we like. A one tick delay is used so the polling task will
\r
145 inherit our priority on all but the first cycle of this task.
\r
146 If we did not block attempting to receive the mutex then no
\r
147 priority inheritance would occur. */
\r
148 if( xSemaphoreTakeRecursive( xMutex, recmuONE_TICK_DELAY ) != pdPASS )
\r
150 xErrorOccurred = pdTRUE;
\r
153 /* Ensure the other task attempting to access the mutex (and the
\r
154 other demo tasks) are able to execute. */
\r
155 vTaskDelay( recmuSHORT_DELAY );
\r
158 /* For each time we took the mutex, give it back. */
\r
159 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
161 /* Ensure the other task attempting to access the mutex (and the
\r
162 other demo tasks) are able to execute. */
\r
163 vTaskDelay( recmuSHORT_DELAY );
\r
165 /* We should now be able to give the mutex as many times as we
\r
167 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
169 xErrorOccurred = pdTRUE;
\r
173 /* Having given it back the same number of times as it was taken, we
\r
174 should no longer be the mutex owner, so the next give should fail. */
\r
175 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
177 xErrorOccurred = pdTRUE;
\r
180 /* Keep count of the number of cycles this task has performed so a
\r
181 stall can be detected. */
\r
182 uxControllingCycles++;
\r
184 /* Suspend ourselves to the blocking task can execute. */
\r
185 xControllingIsSuspended = pdTRUE;
\r
186 vTaskSuspend( NULL );
\r
187 xControllingIsSuspended = pdFALSE;
\r
190 /*-----------------------------------------------------------*/
\r
192 static void prvRecursiveMutexBlockingTask( void *pvParameters )
\r
196 /* Attempt to obtain the mutex. We should block until the
\r
197 controlling task has given up the mutex, and not actually execute
\r
198 past this call until the controlling task is suspended. */
\r
199 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
\r
201 if( xControllingIsSuspended != pdTRUE )
\r
203 /* Did not expect to execute until the controlling task was
\r
205 xErrorOccurred = pdTRUE;
\r
209 /* Give the mutex back before suspending ourselves to allow
\r
210 the polling task to obtain the mutex. */
\r
211 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
213 xErrorOccurred = pdTRUE;
\r
216 xBlockingIsSuspended = pdTRUE;
\r
217 vTaskSuspend( NULL );
\r
218 xBlockingIsSuspended = pdFALSE;
\r
223 /* We should not leave the xSemaphoreTakeRecursive() function
\r
224 until the mutex was obtained. */
\r
225 xErrorOccurred = pdTRUE;
\r
228 /* The controlling and blocking tasks should be in lock step. */
\r
229 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
\r
231 xErrorOccurred = pdTRUE;
\r
234 /* Keep count of the number of cycles this task has performed so a
\r
235 stall can be detected. */
\r
236 uxBlockingCycles++;
\r
239 /*-----------------------------------------------------------*/
\r
241 static void prvRecursiveMutexPollingTask( void *pvParameters )
\r
245 /* Keep attempting to obtain the mutex. We should only obtain it when
\r
246 the blocking task has suspended itself. */
\r
247 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
\r
249 /* Is the blocking task suspended? */
\r
250 if( xBlockingIsSuspended != pdTRUE )
\r
252 xErrorOccurred = pdTRUE;
\r
256 /* Keep count of the number of cycles this task has performed so
\r
257 a stall can be detected. */
\r
260 /* We can resume the other tasks here even though they have a
\r
261 higher priority than the polling task. When they execute they
\r
262 will attempt to obtain the mutex but fail because the polling
\r
263 task is still the mutex holder. The polling task (this task)
\r
264 will then inherit the higher priority. */
\r
265 vTaskResume( xBlockingTaskHandle );
\r
266 vTaskResume( xControllingTaskHandle );
\r
268 /* Release the mutex, disinheriting the higher priority again. */
\r
269 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
271 xErrorOccurred = pdTRUE;
\r
276 #if configUSE_PREEMPTION == 0
\r
283 /*-----------------------------------------------------------*/
\r
285 /* This is called to check that all the created tasks are still running. */
\r
286 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
\r
288 portBASE_TYPE xReturn;
\r
289 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
\r
291 /* Is the controlling task still cycling? */
\r
292 if( uxLastControllingCycles == uxControllingCycles )
\r
294 xErrorOccurred = pdTRUE;
\r
298 uxLastControllingCycles = uxControllingCycles;
\r
301 /* Is the blocking task still cycling? */
\r
302 if( uxLastBlockingCycles == uxBlockingCycles )
\r
304 xErrorOccurred = pdTRUE;
\r
308 uxLastBlockingCycles = uxBlockingCycles;
\r
311 /* Is the polling task still cycling? */
\r
312 if( uxLastPollingCycles == uxPollingCycles )
\r
314 xErrorOccurred = pdTRUE;
\r
318 uxLastPollingCycles = uxPollingCycles;
\r
321 if( xErrorOccurred == pdTRUE )
\r