]> begriffs open source - freertos/blob - Demo/Common/Minimal/recmutex.c
Prepare for V5.3.0 release.
[freertos] / Demo / Common / Minimal / recmutex.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \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
14 \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
18         more details.\r
19 \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
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\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
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53         The tasks defined on this page demonstrate the use of recursive mutexes.\r
54 \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
58         functions.\r
59 \r
60         This demo creates three tasks all of which access the same recursive mutex:\r
61 \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
67 \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
77         suspended.\r
78 \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
86 */\r
87 \r
88 /* Scheduler include files. */\r
89 #include "FreeRTOS.h"\r
90 #include "task.h"\r
91 #include "semphr.h"\r
92 \r
93 /* Demo app include files. */\r
94 #include "recmutex.h"\r
95 \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
100 \r
101 /* The recursive call depth. */\r
102 #define recmuMAX_COUNT                                  ( 10 )\r
103 \r
104 /* Misc. */\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
108 \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
113 \r
114 /* The mutex used by the demo. */\r
115 static xSemaphoreHandle xMutex;\r
116 \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
120 \r
121 /* Handles of the two higher priority tasks, required so they can be resumed \r
122 (unsuspended). */\r
123 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vStartRecursiveMutexTasks( void )\r
128 {\r
129         /* Just creates the mutex and the three tasks. */\r
130 \r
131         xMutex = xSemaphoreCreateRecursiveMutex();\r
132 \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
140 \r
141 \r
142         if( xMutex != NULL )\r
143         {\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
147         }\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
152 {\r
153 unsigned portBASE_TYPE ux;\r
154 \r
155         /* Just to remove compiler warning. */\r
156         ( void ) pvParameters;\r
157 \r
158         for( ;; )\r
159         {\r
160                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
161                 it. */\r
162                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
163                 {\r
164                         xErrorOccurred = pdTRUE;\r
165                 }\r
166 \r
167                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
168                 {\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
175                         {\r
176                                 xErrorOccurred = pdTRUE;\r
177                         }\r
178 \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
182                 }\r
183 \r
184                 /* For each time we took the mutex, give it back. */\r
185                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
186                 {\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
190 \r
191                         /* We should now be able to give the mutex as many times as we\r
192                         took it. */\r
193                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
194                         {\r
195                                 xErrorOccurred = pdTRUE;\r
196                         }\r
197                 }\r
198 \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
202                 {\r
203                         xErrorOccurred = pdTRUE;\r
204                 }\r
205 \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
209 \r
210                 /* Suspend ourselves to the blocking task can execute. */\r
211                 xControllingIsSuspended = pdTRUE;\r
212                 vTaskSuspend( NULL );\r
213                 xControllingIsSuspended = pdFALSE;\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
219 {\r
220         /* Just to remove compiler warning. */\r
221         ( void ) pvParameters;\r
222 \r
223         for( ;; )\r
224         {\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
229                 {\r
230                         if( xControllingIsSuspended != pdTRUE )\r
231                         {\r
232                                 /* Did not expect to execute until the controlling task was\r
233                                 suspended. */\r
234                                 xErrorOccurred = pdTRUE;\r
235                         }\r
236                         else\r
237                         {\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
241                                 {\r
242                                         xErrorOccurred = pdTRUE;\r
243                                 }\r
244 \r
245                                 xBlockingIsSuspended = pdTRUE;\r
246                                 vTaskSuspend( NULL );\r
247                                 xBlockingIsSuspended = pdFALSE;\r
248                         }\r
249                 }\r
250                 else\r
251                 {\r
252                         /* We should not leave the xSemaphoreTakeRecursive() function\r
253                         until the mutex was obtained. */\r
254                         xErrorOccurred = pdTRUE;\r
255                 }\r
256 \r
257                 /* The controlling and blocking tasks should be in lock step. */\r
258                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\r
259                 {\r
260                         xErrorOccurred = pdTRUE;\r
261                 }\r
262 \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
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
271 {\r
272         /* Just to remove compiler warning. */\r
273         ( void ) pvParameters;\r
274 \r
275         for( ;; )\r
276         {\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
280                 {\r
281                         /* Is the blocking task suspended? */\r
282                         if( xBlockingIsSuspended != pdTRUE )\r
283                         {\r
284                                 xErrorOccurred = pdTRUE;\r
285                         }\r
286                         else\r
287                         {\r
288                                 /* Keep count of the number of cycles this task has performed so \r
289                                 a stall can be detected. */\r
290                                 uxPollingCycles++;\r
291 \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
299                         \r
300                                 /* Release the mutex, disinheriting the higher priority again. */\r
301                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
302                                 {\r
303                                         xErrorOccurred = pdTRUE;\r
304                                 }\r
305                         }\r
306                 }\r
307 \r
308                 #if configUSE_PREEMPTION == 0\r
309                 {\r
310                         taskYIELD();\r
311                 }\r
312                 #endif\r
313         }\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 /* This is called to check that all the created tasks are still running. */\r
318 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )\r
319 {\r
320 portBASE_TYPE xReturn;\r
321 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
322 \r
323         /* Is the controlling task still cycling? */\r
324         if( uxLastControllingCycles == uxControllingCycles )\r
325         {\r
326                 xErrorOccurred = pdTRUE;\r
327         }\r
328         else\r
329         {\r
330                 uxLastControllingCycles = uxControllingCycles;\r
331         }\r
332 \r
333         /* Is the blocking task still cycling? */\r
334         if( uxLastBlockingCycles == uxBlockingCycles )\r
335         {\r
336                 xErrorOccurred = pdTRUE;\r
337         }\r
338         else\r
339         {\r
340                 uxLastBlockingCycles = uxBlockingCycles;\r
341         }\r
342 \r
343         /* Is the polling task still cycling? */\r
344         if( uxLastPollingCycles == uxPollingCycles )\r
345         {\r
346                 xErrorOccurred = pdTRUE;\r
347         }\r
348         else\r
349         {\r
350                 uxLastPollingCycles = uxPollingCycles;\r
351         }\r
352 \r
353         if( xErrorOccurred == pdTRUE )\r
354         {\r
355                 xReturn = pdFAIL;\r
356         }\r
357         else\r
358         {\r
359                 xReturn = pdTRUE;\r
360         }\r
361 \r
362         return xReturn;\r
363 }\r
364 \r
365 \r
366 \r
367 \r