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 For recursive mutex functionality the created mutex should be created using
\r
41 xSemaphoreCreateRecursiveMutex(), then be manipulated
\r
42 using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
\r
45 This demo creates three tasks all of which access the same recursive mutex:
\r
47 prvRecursiveMutexControllingTask() has the highest priority so executes
\r
48 first and grabs the mutex. It then performs some recursive accesses -
\r
49 between each of which it sleeps for a short period to let the lower
\r
50 priority tasks execute. When it has completed its demo functionality
\r
51 it gives the mutex back before suspending itself.
\r
53 prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
\r
54 a blocking 'take'. The blocking task has a lower priority than the
\r
55 controlling task so by the time it executes the mutex has already been
\r
56 taken by the controlling task, causing the blocking task to block. It
\r
57 does not unblock until the controlling task has given the mutex back,
\r
58 and it does not actually run until the controlling task has suspended
\r
59 itself (due to the relative priorities). When it eventually does obtain
\r
60 the mutex all it does is give the mutex back prior to also suspending
\r
61 itself. At this point both the controlling task and the blocking task are
\r
64 prvRecursiveMutexPollingTask() runs at the idle priority. It spins round
\r
65 a tight loop attempting to obtain the mutex with a non-blocking call. As
\r
66 the lowest priority task it will not successfully obtain the mutex until
\r
67 both the controlling and blocking tasks are suspended. Once it eventually
\r
68 does obtain the mutex it first unsuspends both the controlling task and
\r
69 blocking task prior to giving the mutex back - resulting in the polling
\r
70 task temporarily inheriting the controlling tasks priority.
\r
73 /* Scheduler include files. */
\r
74 #include "FreeRTOS.h"
\r
78 /* Demo app include files. */
\r
79 #include "recmutex.h"
\r
81 /* Priorities assigned to the three tasks. */
\r
82 #define recmuCONTROLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
83 #define recmuBLOCKING_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
84 #define recmuPOLLING_TASK_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
86 /* The recursive call depth. */
\r
87 #define recmuMAX_COUNT ( 10 )
\r
90 #define recmuSHORT_DELAY ( 5 / portTICK_RATE_MS )
\r
91 #define recmuNO_DELAY ( ( portTickType ) 0 )
\r
92 #define recmuONE_TICK_DELAY ( ( portTickType ) 1 )
\r
94 /* The three tasks as described at the top of this file. */
\r
95 static void prvRecursiveMutexControllingTask( void *pvParameters );
\r
96 static void prvRecursiveMutexBlockingTask( void *pvParameters );
\r
97 static void prvRecursiveMutexPollingTask( void *pvParameters );
\r
99 /* The mutex used by the demo. */
\r
100 static xSemaphoreHandle xMutex;
\r
102 /* Variables used to detect and latch errors. */
\r
103 static portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
\r
104 static unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0;
\r
106 /* Handles of the two higher priority tasks, required so they can be resumed
\r
108 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
\r
110 /*-----------------------------------------------------------*/
\r
112 void vStartRecursiveMutexTasks( void )
\r
114 /* Just creates the mutex and the three tasks. */
\r
116 xMutex = xSemaphoreCreateMutex();
\r
118 if( xMutex != NULL )
\r
120 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
\r
121 xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
\r
122 xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
\r
125 /*-----------------------------------------------------------*/
\r
127 static void prvRecursiveMutexControllingTask( void *pvParameters )
\r
129 unsigned portBASE_TYPE ux;
\r
133 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
\r
135 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
137 xErrorOccurred = pdTRUE;
\r
140 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
142 /* We should now be able to take the mutex as many times as
\r
143 we like. A one tick delay is used so the polling task will
\r
144 inherit our priority on all but the first cycle of this task.
\r
145 If we did not block attempting to receive the mutex then no
\r
146 priority inheritance would occur. */
\r
147 if( xSemaphoreTakeRecursive( xMutex, recmuONE_TICK_DELAY ) != pdPASS )
\r
149 xErrorOccurred = pdTRUE;
\r
152 /* Ensure the other task attempting to access the mutex (and the
\r
153 other demo tasks) are able to execute. */
\r
154 vTaskDelay( recmuSHORT_DELAY );
\r
157 /* For each time we took the mutex, give it back. */
\r
158 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
\r
160 /* Ensure the other task attempting to access the mutex (and the
\r
161 other demo tasks) are able to execute. */
\r
162 vTaskDelay( recmuSHORT_DELAY );
\r
164 /* We should now be able to give the mutex as many times as we
\r
166 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
168 xErrorOccurred = pdTRUE;
\r
172 /* Having given it back the same number of times as it was taken, we
\r
173 should no longer be the mutex owner, so the next give should fail. */
\r
174 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
\r
176 xErrorOccurred = pdTRUE;
\r
179 /* Keep count of the number of cycles this task has performed so a
\r
180 stall can be detected. */
\r
181 uxControllingCycles++;
\r
183 /* Suspend ourselves to the blocking task can execute. */
\r
184 xControllingIsSuspended = pdTRUE;
\r
185 vTaskSuspend( NULL );
\r
186 xControllingIsSuspended = pdFALSE;
\r
189 /*-----------------------------------------------------------*/
\r
191 static void prvRecursiveMutexBlockingTask( void *pvParameters )
\r
195 /* Attempt to obtain the mutex. We should block until the
\r
196 controlling task has given up the mutex, and not actually execute
\r
197 past this call until the controlling task is suspended. */
\r
198 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
\r
200 if( xControllingIsSuspended != pdTRUE )
\r
202 /* Did not expect to execute until the controlling task was
\r
204 xErrorOccurred = pdTRUE;
\r
208 /* Give the mutex back before suspending ourselves to allow
\r
209 the polling task to obtain the mutex. */
\r
210 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
212 xErrorOccurred = pdTRUE;
\r
215 xBlockingIsSuspended = pdTRUE;
\r
216 vTaskSuspend( NULL );
\r
217 xBlockingIsSuspended = pdFALSE;
\r
222 /* We should not leave the xSemaphoreTakeRecursive() function
\r
223 until the mutex was obtained. */
\r
224 xErrorOccurred = pdTRUE;
\r
227 /* The controlling and blocking tasks should be in lock step. */
\r
228 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
\r
230 xErrorOccurred = pdTRUE;
\r
233 /* Keep count of the number of cycles this task has performed so a
\r
234 stall can be detected. */
\r
235 uxBlockingCycles++;
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvRecursiveMutexPollingTask( void *pvParameters )
\r
244 /* Keep attempting to obtain the mutex. We should only obtain it when
\r
245 the blocking task has suspended itself. */
\r
246 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
\r
248 /* Is the blocking task suspended? */
\r
249 if( xBlockingIsSuspended != pdTRUE )
\r
251 xErrorOccurred = pdTRUE;
\r
255 /* Keep count of the number of cycles this task has performed so
\r
256 a stall can be detected. */
\r
259 /* We can resume the other tasks here even though they have a
\r
260 higher priority than the polling task. When they execute they
\r
261 will attempt to obtain the mutex but fail because the polling
\r
262 task is still the mutex holder. The polling task (this task)
\r
263 will then inherit the higher priority. */
\r
264 vTaskResume( xBlockingTaskHandle );
\r
265 vTaskResume( xControllingTaskHandle );
\r
267 /* Release the mutex, disinheriting the higher priority again. */
\r
268 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
\r
270 xErrorOccurred = pdTRUE;
\r
275 #if configUSE_PREEMPTION == 0
\r
282 /*-----------------------------------------------------------*/
\r
284 /* This is called to check that all the created tasks are still running. */
\r
285 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
\r
287 portBASE_TYPE xReturn;
\r
288 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
\r
290 /* Is the controlling task still cycling? */
\r
291 if( uxLastControllingCycles == uxControllingCycles )
\r
293 xErrorOccurred = pdTRUE;
\r
297 uxLastControllingCycles = uxControllingCycles;
\r
300 /* Is the blocking task still cycling? */
\r
301 if( uxLastBlockingCycles == uxBlockingCycles )
\r
303 xErrorOccurred = pdTRUE;
\r
307 uxLastBlockingCycles = uxBlockingCycles;
\r
310 /* Is the polling task still cycling? */
\r
311 if( uxLastPollingCycles == uxPollingCycles )
\r
313 xErrorOccurred = pdTRUE;
\r
317 uxLastPollingCycles = uxPollingCycles;
\r
320 if( xErrorOccurred == pdTRUE )
\r