]> begriffs open source - freertos/blob - Demo/Common/Minimal/recmutex.c
Documentation updates.
[freertos] / Demo / Common / Minimal / recmutex.c
1 /*\r
2         FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 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         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
30 \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
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 /*\r
38         The tasks defined on this page demonstrate the use of recursive mutexes.\r
39 \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
43         functions.\r
44 \r
45         This demo creates three tasks all of which access the same recursive mutex:\r
46 \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
52 \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
62         suspended.\r
63 \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
71 */\r
72 \r
73 /* Scheduler include files. */\r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "semphr.h"\r
77 \r
78 /* Demo app include files. */\r
79 #include "recmutex.h"\r
80 \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
85 \r
86 /* The recursive call depth. */\r
87 #define recmuMAX_COUNT                                  ( 10 )\r
88 \r
89 /* Misc. */\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
93 \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
98 \r
99 /* The mutex used by the demo. */\r
100 static xSemaphoreHandle xMutex;\r
101 \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
105 \r
106 /* Handles of the two higher priority tasks, required so they can be resumed \r
107 (unsuspended). */\r
108 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vStartRecursiveMutexTasks( void )\r
113 {\r
114         /* Just creates the mutex and the three tasks. */\r
115 \r
116         xMutex = xSemaphoreCreateMutex();\r
117 \r
118         if( xMutex != NULL )\r
119         {\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
123         }\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
128 {\r
129 unsigned portBASE_TYPE ux;\r
130 \r
131         for( ;; )\r
132         {\r
133                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
134                 it. */\r
135                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
136                 {\r
137                         xErrorOccurred = pdTRUE;\r
138                 }\r
139 \r
140                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
141                 {\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
148                         {\r
149                                 xErrorOccurred = pdTRUE;\r
150                         }\r
151 \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
155                 }\r
156 \r
157                 /* For each time we took the mutex, give it back. */\r
158                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
159                 {\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
163 \r
164                         /* We should now be able to give the mutex as many times as we\r
165                         took it. */\r
166                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
167                         {\r
168                                 xErrorOccurred = pdTRUE;\r
169                         }\r
170                 }\r
171 \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
175                 {\r
176                         xErrorOccurred = pdTRUE;\r
177                 }\r
178 \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
182 \r
183                 /* Suspend ourselves to the blocking task can execute. */\r
184                 xControllingIsSuspended = pdTRUE;\r
185                 vTaskSuspend( NULL );\r
186                 xControllingIsSuspended = pdFALSE;\r
187         }\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
192 {\r
193         for( ;; )\r
194         {\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
199                 {\r
200                         if( xControllingIsSuspended != pdTRUE )\r
201                         {\r
202                                 /* Did not expect to execute until the controlling task was\r
203                                 suspended. */\r
204                                 xErrorOccurred = pdTRUE;\r
205                         }\r
206                         else\r
207                         {\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
211                                 {\r
212                                         xErrorOccurred = pdTRUE;\r
213                                 }\r
214 \r
215                                 xBlockingIsSuspended = pdTRUE;\r
216                                 vTaskSuspend( NULL );\r
217                                 xBlockingIsSuspended = pdFALSE;\r
218                         }\r
219                 }\r
220                 else\r
221                 {\r
222                         /* We should not leave the xSemaphoreTakeRecursive() function\r
223                         until the mutex was obtained. */\r
224                         xErrorOccurred = pdTRUE;\r
225                 }\r
226 \r
227                 /* The controlling and blocking tasks should be in lock step. */\r
228                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\r
229                 {\r
230                         xErrorOccurred = pdTRUE;\r
231                 }\r
232 \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
236         }\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
241 {\r
242         for( ;; )\r
243         {\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
247                 {\r
248                         /* Is the blocking task suspended? */\r
249                         if( xBlockingIsSuspended != pdTRUE )\r
250                         {\r
251                                 xErrorOccurred = pdTRUE;\r
252                         }\r
253                         else\r
254                         {\r
255                                 /* Keep count of the number of cycles this task has performed so \r
256                                 a stall can be detected. */\r
257                                 uxPollingCycles++;\r
258 \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
266                         \r
267                                 /* Release the mutex, disinheriting the higher priority again. */\r
268                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
269                                 {\r
270                                         xErrorOccurred = pdTRUE;\r
271                                 }\r
272                         }\r
273                 }\r
274 \r
275                 #if configUSE_PREEMPTION == 0\r
276                 {\r
277                         taskYIELD();\r
278                 }\r
279                 #endif\r
280         }\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 /* This is called to check that all the created tasks are still running. */\r
285 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )\r
286 {\r
287 portBASE_TYPE xReturn;\r
288 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
289 \r
290         /* Is the controlling task still cycling? */\r
291         if( uxLastControllingCycles == uxControllingCycles )\r
292         {\r
293                 xErrorOccurred = pdTRUE;\r
294         }\r
295         else\r
296         {\r
297                 uxLastControllingCycles = uxControllingCycles;\r
298         }\r
299 \r
300         /* Is the blocking task still cycling? */\r
301         if( uxLastBlockingCycles == uxBlockingCycles )\r
302         {\r
303                 xErrorOccurred = pdTRUE;\r
304         }\r
305         else\r
306         {\r
307                 uxLastBlockingCycles = uxBlockingCycles;\r
308         }\r
309 \r
310         /* Is the polling task still cycling? */\r
311         if( uxLastPollingCycles == uxPollingCycles )\r
312         {\r
313                 xErrorOccurred = pdTRUE;\r
314         }\r
315         else\r
316         {\r
317                 uxLastPollingCycles = uxPollingCycles;\r
318         }\r
319 \r
320         if( xErrorOccurred == pdTRUE )\r
321         {\r
322                 xReturn = pdFAIL;\r
323         }\r
324         else\r
325         {\r
326                 xReturn = pdTRUE;\r
327         }\r
328 \r
329         return xReturn;\r
330 }\r
331 \r
332 \r
333 \r
334 \r