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