]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/recmutex.c
UpdUpdate IAR projects to use Embedded Workbench V5.11.
[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         All mutexes are created using a call to xSemaphoreCreateMutex().  For\r
41         recursive mutex functionality the created mutex should then be manipulated\r
42         using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API\r
43         functions.  Recursive mutexes must NOT be passed as a parameter to the\r
44         standard mutex API functions xSemaphoreTake() and xSemaphoreGive().\r
45 \r
46         This demo creates three tasks all of which access the same recursive mutex:\r
47 \r
48         prvRecursiveMutexControllingTask() has the highest priority so executes \r
49         first and grabs the mutex.  It then performs some recursive accesses - \r
50         between each of which it sleeps for a short period to let the lower \r
51         priority tasks execute.  When it has completed its demo functionality\r
52         it gives the mutex back before suspending itself.\r
53 \r
54         prvRecursiveMutexBlockingTask() attempts to access the mutex by performing\r
55         a blocking 'take'.  The blocking task has a lower priority than the \r
56         controlling     task so by the time it executes the mutex has already been\r
57         taken by the controlling task,  causing the blocking task to block.  It \r
58         does not unblock until the controlling task has given the mutex back, \r
59         and it does not actually run until the controlling task has suspended \r
60         itself (due to the relative priorities).  When it eventually does obtain\r
61         the mutex all it does is give the mutex back prior to also suspending \r
62         itself.  At this point both the controlling task and the blocking task are \r
63         suspended.\r
64 \r
65         prvRecursiveMutexPollingTask() runs at the idle priority.  It spins round\r
66         a tight loop attempting to obtain the mutex with a non-blocking call.  As\r
67         the lowest priority task it will not successfully obtain the mutex until\r
68         both the controlling and blocking tasks are suspended.  Once it eventually \r
69         does obtain the mutex it first unsuspends both the controlling task and\r
70         blocking task prior to giving the mutex back - resulting in the polling\r
71         task temporarily inheriting the controlling tasks priority.\r
72 */\r
73 \r
74 /* Scheduler include files. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "semphr.h"\r
78 \r
79 /* Demo app include files. */\r
80 #include "recmutex.h"\r
81 \r
82 /* Priorities assigned to the three tasks. */\r
83 #define recmuCONTROLLING_TASK_PRIORITY  ( tskIDLE_PRIORITY + 2 )\r
84 #define recmuBLOCKING_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
85 #define recmuPOLLING_TASK_PRIORITY              ( tskIDLE_PRIORITY + 0 )\r
86 \r
87 /* The recursive call depth. */\r
88 #define recmuMAX_COUNT                                  ( 10 )\r
89 \r
90 /* Misc. */\r
91 #define recmuSHORT_DELAY                                ( 5 / portTICK_RATE_MS )\r
92 #define recmuNO_DELAY                                   ( ( portTickType ) 0 )\r
93 #define recmuONE_TICK_DELAY                             ( ( portTickType ) 1 )\r
94 \r
95 /* The three tasks as described at the top of this file. */\r
96 static void prvRecursiveMutexControllingTask( void *pvParameters );\r
97 static void prvRecursiveMutexBlockingTask( void *pvParameters );\r
98 static void prvRecursiveMutexPollingTask( void *pvParameters );\r
99 \r
100 /* The mutex used by the demo. */\r
101 static xSemaphoreHandle xMutex;\r
102 \r
103 /* Variables used to detect and latch errors. */\r
104 static portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
105 static unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0;\r
106 \r
107 /* Handles of the two higher priority tasks, required so they can be resumed \r
108 (unsuspended). */\r
109 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 void vStartRecursiveMutexTasks( void )\r
114 {\r
115         /* Just creates the mutex and the three tasks. */\r
116 \r
117         xMutex = xSemaphoreCreateMutex();\r
118 \r
119         if( xMutex != NULL )\r
120         {\r
121                 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );\r
122         xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );\r
123         xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );\r
124         }\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
129 {\r
130 unsigned portBASE_TYPE ux;\r
131 \r
132         for( ;; )\r
133         {\r
134                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
135                 it. */\r
136                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
137                 {\r
138                         xErrorOccurred = pdTRUE;\r
139                 }\r
140 \r
141                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
142                 {\r
143                         /* We should now be able to take the mutex as many times as\r
144                         we like.  A one tick delay is used so the polling task will\r
145                         inherit our priority on all but the first cycle of this task. \r
146                         If we did not block attempting to receive the mutex then no\r
147                         priority inheritance would occur. */\r
148                         if( xSemaphoreTakeRecursive( xMutex, recmuONE_TICK_DELAY ) != pdPASS )\r
149                         {\r
150                                 xErrorOccurred = pdTRUE;\r
151                         }\r
152 \r
153                         /* Ensure the other task attempting to access the mutex (and the\r
154                         other demo tasks) are able to execute. */\r
155                         vTaskDelay( recmuSHORT_DELAY );\r
156                 }\r
157 \r
158                 /* For each time we took the mutex, give it back. */\r
159                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
160                 {\r
161                         /* Ensure the other task attempting to access the mutex (and the\r
162                         other demo tasks) are able to execute. */\r
163                         vTaskDelay( recmuSHORT_DELAY );\r
164 \r
165                         /* We should now be able to give the mutex as many times as we\r
166                         took it. */\r
167                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
168                         {\r
169                                 xErrorOccurred = pdTRUE;\r
170                         }\r
171                 }\r
172 \r
173                 /* Having given it back the same number of times as it was taken, we\r
174                 should no longer be the mutex owner, so the next give should fail. */\r
175                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
176                 {\r
177                         xErrorOccurred = pdTRUE;\r
178                 }\r
179 \r
180                 /* Keep count of the number of cycles this task has performed so a \r
181                 stall can be detected. */\r
182                 uxControllingCycles++;\r
183 \r
184                 /* Suspend ourselves to the blocking task can execute. */\r
185                 xControllingIsSuspended = pdTRUE;\r
186                 vTaskSuspend( NULL );\r
187                 xControllingIsSuspended = pdFALSE;\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
193 {\r
194         for( ;; )\r
195         {\r
196                 /* Attempt to obtain the mutex.  We should block until the \r
197                 controlling task has given up the mutex, and not actually execute\r
198                 past this call until the controlling task is suspended. */\r
199                 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )\r
200                 {\r
201                         if( xControllingIsSuspended != pdTRUE )\r
202                         {\r
203                                 /* Did not expect to execute until the controlling task was\r
204                                 suspended. */\r
205                                 xErrorOccurred = pdTRUE;\r
206                         }\r
207                         else\r
208                         {\r
209                                 /* Give the mutex back before suspending ourselves to allow\r
210                                 the polling task to obtain the mutex. */\r
211                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
212                                 {\r
213                                         xErrorOccurred = pdTRUE;\r
214                                 }\r
215 \r
216                                 xBlockingIsSuspended = pdTRUE;\r
217                                 vTaskSuspend( NULL );\r
218                                 xBlockingIsSuspended = pdFALSE;\r
219                         }\r
220                 }\r
221                 else\r
222                 {\r
223                         /* We should not leave the xSemaphoreTakeRecursive() function\r
224                         until the mutex was obtained. */\r
225                         xErrorOccurred = pdTRUE;\r
226                 }\r
227 \r
228                 /* The controlling and blocking tasks should be in lock step. */\r
229                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\r
230                 {\r
231                         xErrorOccurred = pdTRUE;\r
232                 }\r
233 \r
234                 /* Keep count of the number of cycles this task has performed so a \r
235                 stall can be detected. */\r
236                 uxBlockingCycles++;\r
237         }\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
242 {\r
243         for( ;; )\r
244         {\r
245                 /* Keep attempting to obtain the mutex.  We should only obtain it when\r
246                 the blocking task has suspended itself. */\r
247                 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
248                 {\r
249                         /* Is the blocking task suspended? */\r
250                         if( xBlockingIsSuspended != pdTRUE )\r
251                         {\r
252                                 xErrorOccurred = pdTRUE;\r
253                         }\r
254                         else\r
255                         {\r
256                                 /* Keep count of the number of cycles this task has performed so \r
257                                 a stall can be detected. */\r
258                                 uxPollingCycles++;\r
259 \r
260                                 /* We can resume the other tasks here even though they have a\r
261                                 higher priority than the polling task.  When they execute they\r
262                                 will attempt to obtain the mutex but fail because the polling\r
263                                 task is still the mutex holder.  The polling task (this task)\r
264                                 will then inherit the higher priority. */                               \r
265                                 vTaskResume( xBlockingTaskHandle );\r
266                 vTaskResume( xControllingTaskHandle );\r
267                         \r
268                                 /* Release the mutex, disinheriting the higher priority again. */\r
269                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
270                                 {\r
271                                         xErrorOccurred = pdTRUE;\r
272                                 }\r
273                         }\r
274                 }\r
275 \r
276                 #if configUSE_PREEMPTION == 0\r
277                 {\r
278                         taskYIELD();\r
279                 }\r
280                 #endif\r
281         }\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 /* This is called to check that all the created tasks are still running. */\r
286 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )\r
287 {\r
288 portBASE_TYPE xReturn;\r
289 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
290 \r
291         /* Is the controlling task still cycling? */\r
292         if( uxLastControllingCycles == uxControllingCycles )\r
293         {\r
294                 xErrorOccurred = pdTRUE;\r
295         }\r
296         else\r
297         {\r
298                 uxLastControllingCycles = uxControllingCycles;\r
299         }\r
300 \r
301         /* Is the blocking task still cycling? */\r
302         if( uxLastBlockingCycles == uxBlockingCycles )\r
303         {\r
304                 xErrorOccurred = pdTRUE;\r
305         }\r
306         else\r
307         {\r
308                 uxLastBlockingCycles = uxBlockingCycles;\r
309         }\r
310 \r
311         /* Is the polling task still cycling? */\r
312         if( uxLastPollingCycles == uxPollingCycles )\r
313         {\r
314                 xErrorOccurred = pdTRUE;\r
315         }\r
316         else\r
317         {\r
318                 uxLastPollingCycles = uxPollingCycles;\r
319         }\r
320 \r
321         if( xErrorOccurred == pdTRUE )\r
322         {\r
323                 xReturn = pdFAIL;\r
324         }\r
325         else\r
326         {\r
327                 xReturn = pdTRUE;\r
328         }\r
329 \r
330         return xReturn;\r
331 }\r
332 \r
333 \r
334 \r
335 \r