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