]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/recmutex.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Demo / Common / Minimal / recmutex.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*\r
68         The tasks defined on this page demonstrate the use of recursive mutexes.\r
69 \r
70         For recursive mutex functionality the created mutex should be created using\r
71         xSemaphoreCreateRecursiveMutex(), then be manipulated\r
72         using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API\r
73         functions.\r
74 \r
75         This demo creates three tasks all of which access the same recursive mutex:\r
76 \r
77         prvRecursiveMutexControllingTask() has the highest priority so executes \r
78         first and grabs the mutex.  It then performs some recursive accesses - \r
79         between each of which it sleeps for a short period to let the lower \r
80         priority tasks execute.  When it has completed its demo functionality\r
81         it gives the mutex back before suspending itself.\r
82 \r
83         prvRecursiveMutexBlockingTask() attempts to access the mutex by performing\r
84         a blocking 'take'.  The blocking task has a lower priority than the \r
85         controlling     task so by the time it executes the mutex has already been\r
86         taken by the controlling task,  causing the blocking task to block.  It \r
87         does not unblock until the controlling task has given the mutex back, \r
88         and it does not actually run until the controlling task has suspended \r
89         itself (due to the relative priorities).  When it eventually does obtain\r
90         the mutex all it does is give the mutex back prior to also suspending \r
91         itself.  At this point both the controlling task and the blocking task are \r
92         suspended.\r
93 \r
94         prvRecursiveMutexPollingTask() runs at the idle priority.  It spins round\r
95         a tight loop attempting to obtain the mutex with a non-blocking call.  As\r
96         the lowest priority task it will not successfully obtain the mutex until\r
97         both the controlling and blocking tasks are suspended.  Once it eventually \r
98         does obtain the mutex it first unsuspends both the controlling task and\r
99         blocking task prior to giving the mutex back - resulting in the polling\r
100         task temporarily inheriting the controlling tasks priority.\r
101 */\r
102 \r
103 /* Scheduler include files. */\r
104 #include "FreeRTOS.h"\r
105 #include "task.h"\r
106 #include "semphr.h"\r
107 \r
108 /* Demo app include files. */\r
109 #include "recmutex.h"\r
110 \r
111 /* Priorities assigned to the three tasks. */\r
112 #define recmuCONTROLLING_TASK_PRIORITY  ( tskIDLE_PRIORITY + 2 )\r
113 #define recmuBLOCKING_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
114 #define recmuPOLLING_TASK_PRIORITY              ( tskIDLE_PRIORITY + 0 )\r
115 \r
116 /* The recursive call depth. */\r
117 #define recmuMAX_COUNT                                  ( 10 )\r
118 \r
119 /* Misc. */\r
120 #define recmuSHORT_DELAY                                ( 20 / portTICK_RATE_MS )\r
121 #define recmuNO_DELAY                                   ( ( portTickType ) 0 )\r
122 #define recmuTWO_TICK_DELAY                             ( ( portTickType ) 2 )\r
123 \r
124 /* The three tasks as described at the top of this file. */\r
125 static void prvRecursiveMutexControllingTask( void *pvParameters );\r
126 static void prvRecursiveMutexBlockingTask( void *pvParameters );\r
127 static void prvRecursiveMutexPollingTask( void *pvParameters );\r
128 \r
129 /* The mutex used by the demo. */\r
130 static xSemaphoreHandle xMutex;\r
131 \r
132 /* Variables used to detect and latch errors. */\r
133 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
134 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;\r
135 \r
136 /* Handles of the two higher priority tasks, required so they can be resumed \r
137 (unsuspended). */\r
138 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vStartRecursiveMutexTasks( void )\r
143 {\r
144         /* Just creates the mutex and the three tasks. */\r
145 \r
146         xMutex = xSemaphoreCreateRecursiveMutex();\r
147 \r
148         /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
149         in use.  The registry is provided as a means for kernel aware \r
150         debuggers to locate mutex and has no purpose if a kernel aware debugger\r
151         is not being used.  The call to vQueueAddToRegistry() will be removed\r
152         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
153         defined to be less than 1. */\r
154         vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );\r
155 \r
156 \r
157         if( xMutex != NULL )\r
158         {\r
159                 xTaskCreate( prvRecursiveMutexControllingTask, ( signed portCHAR * ) "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );\r
160         xTaskCreate( prvRecursiveMutexBlockingTask, ( signed portCHAR * ) "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );\r
161         xTaskCreate( prvRecursiveMutexPollingTask, ( signed portCHAR * ) "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );\r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
167 {\r
168 unsigned portBASE_TYPE ux;\r
169 \r
170         /* Just to remove compiler warning. */\r
171         ( void ) pvParameters;\r
172 \r
173         for( ;; )\r
174         {\r
175                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
176                 it.   The first time through, the mutex will not have been used yet,\r
177                 subsequent times through, at this point the mutex will be held by the\r
178                 polling task. */\r
179                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
180                 {\r
181                         xErrorOccurred = pdTRUE;\r
182                 }\r
183 \r
184                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
185                 {\r
186                         /* We should now be able to take the mutex as many times as\r
187                         we like.\r
188                         \r
189                         The first time through the mutex will be immediately available, on\r
190                         subsequent times through the mutex will be held by the polling task\r
191                         at this point and this Take will cause the polling task to inherit\r
192                         the priority of this task.  In this case the block time must be\r
193                         long enough to ensure the polling task will execute again before the\r
194                         block time expires.  If the block time does expire then the error\r
195                         flag will be set here. */\r
196                         if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )\r
197                         {\r
198                                 xErrorOccurred = pdTRUE;\r
199                         }\r
200 \r
201                         /* Ensure the other task attempting to access the mutex (and the\r
202                         other demo tasks) are able to execute to ensure they either block\r
203                         (where a block time is specified) or return an error (where no \r
204                         block time is specified) as the mutex is held by this task. */\r
205                         vTaskDelay( recmuSHORT_DELAY );\r
206                 }\r
207 \r
208                 /* For each time we took the mutex, give it back. */\r
209                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
210                 {\r
211                         /* Ensure the other task attempting to access the mutex (and the\r
212                         other demo tasks) are able to execute. */\r
213                         vTaskDelay( recmuSHORT_DELAY );\r
214 \r
215                         /* We should now be able to give the mutex as many times as we\r
216                         took it.  When the mutex is available again the Blocking task\r
217                         should be unblocked but not run because it has a lower priority\r
218                         than this task.  The polling task should also not run at this point\r
219                         as it too has a lower priority than this task. */\r
220                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
221                         {\r
222                                 xErrorOccurred = pdTRUE;\r
223                         }\r
224                 }\r
225 \r
226                 /* Having given it back the same number of times as it was taken, we\r
227                 should no longer be the mutex owner, so the next give sh ould fail. */\r
228                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\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                 uxControllingCycles++;\r
236 \r
237                 /* Suspend ourselves to the blocking task can execute. */\r
238                 xControllingIsSuspended = pdTRUE;\r
239                 vTaskSuspend( NULL );\r
240                 xControllingIsSuspended = pdFALSE;\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
246 {\r
247         /* Just to remove compiler warning. */\r
248         ( void ) pvParameters;\r
249 \r
250         for( ;; )\r
251         {\r
252                 /* This task will run while the controlling task is blocked, and the\r
253                 controlling task will block only once it has the mutex - therefore\r
254                 this call should block until the controlling task has given up the \r
255                 mutex, and not actually execute past this call until the controlling \r
256                 task is suspended. */\r
257                 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )\r
258                 {\r
259                         if( xControllingIsSuspended != pdTRUE )\r
260                         {\r
261                                 /* Did not expect to execute until the controlling task was\r
262                                 suspended. */\r
263                                 xErrorOccurred = pdTRUE;\r
264                         }\r
265                         else\r
266                         {\r
267                                 /* Give the mutex back before suspending ourselves to allow\r
268                                 the polling task to obtain the mutex. */\r
269                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
270                                 {\r
271                                         xErrorOccurred = pdTRUE;\r
272                                 }\r
273 \r
274                                 xBlockingIsSuspended = pdTRUE;\r
275                                 vTaskSuspend( NULL );\r
276                                 xBlockingIsSuspended = pdFALSE;\r
277                         }\r
278                 }\r
279                 else\r
280                 {\r
281                         /* We should not leave the xSemaphoreTakeRecursive() function\r
282                         until the mutex was obtained. */\r
283                         xErrorOccurred = pdTRUE;\r
284                 }\r
285 \r
286                 /* The controlling and blocking tasks should be in lock step. */\r
287                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\r
288                 {\r
289                         xErrorOccurred = pdTRUE;\r
290                 }\r
291 \r
292                 /* Keep count of the number of cycles this task has performed so a \r
293                 stall can be detected. */\r
294                 uxBlockingCycles++;\r
295         }\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
300 {\r
301         /* Just to remove compiler warning. */\r
302         ( void ) pvParameters;\r
303 \r
304         for( ;; )\r
305         {\r
306                 /* Keep attempting to obtain the mutex.  We should only obtain it when\r
307                 the blocking task has suspended itself, which in turn should only\r
308                 happen when the controlling task is also suspended. */\r
309                 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
310                 {\r
311                         /* Is the blocking task suspended? */\r
312                         if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
313                         {\r
314                                 xErrorOccurred = pdTRUE;\r
315                         }\r
316                         else\r
317                         {\r
318                                 /* Keep count of the number of cycles this task has performed \r
319                                 so a stall can be detected. */\r
320                                 uxPollingCycles++;\r
321 \r
322                                 /* We can resume the other tasks here even though they have a\r
323                                 higher priority than the polling task.  When they execute they\r
324                                 will attempt to obtain the mutex but fail because the polling\r
325                                 task is still the mutex holder.  The polling task (this task)\r
326                                 will then inherit the higher priority.  The Blocking task will\r
327                                 block indefinitely when it attempts to obtain the mutex, the\r
328                                 Controlling task will only block for a fixed period and an\r
329                                 error will be latched if the polling task has not returned the\r
330                                 mutex by the time this fixed period has expired. */\r
331                                 vTaskResume( xBlockingTaskHandle );\r
332                 vTaskResume( xControllingTaskHandle );\r
333                         \r
334                                 /* The other two tasks should now have executed and no longer\r
335                                 be suspended. */\r
336                                 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
337                                 {\r
338                                         xErrorOccurred = pdTRUE;\r
339                                 }                               \r
340                         \r
341                                 /* Release the mutex, disinheriting the higher priority again. */\r
342                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
343                                 {\r
344                                         xErrorOccurred = pdTRUE;\r
345                                 }\r
346                         }\r
347                 }\r
348 \r
349                 #if configUSE_PREEMPTION == 0\r
350                 {\r
351                         taskYIELD();\r
352                 }\r
353                 #endif\r
354         }\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 /* This is called to check that all the created tasks are still running. */\r
359 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )\r
360 {\r
361 portBASE_TYPE xReturn;\r
362 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
363 \r
364         /* Is the controlling task still cycling? */\r
365         if( uxLastControllingCycles == uxControllingCycles )\r
366         {\r
367                 xErrorOccurred = pdTRUE;\r
368         }\r
369         else\r
370         {\r
371                 uxLastControllingCycles = uxControllingCycles;\r
372         }\r
373 \r
374         /* Is the blocking task still cycling? */\r
375         if( uxLastBlockingCycles == uxBlockingCycles )\r
376         {\r
377                 xErrorOccurred = pdTRUE;\r
378         }\r
379         else\r
380         {\r
381                 uxLastBlockingCycles = uxBlockingCycles;\r
382         }\r
383 \r
384         /* Is the polling task still cycling? */\r
385         if( uxLastPollingCycles == uxPollingCycles )\r
386         {\r
387                 xErrorOccurred = pdTRUE;\r
388         }\r
389         else\r
390         {\r
391                 uxLastPollingCycles = uxPollingCycles;\r
392         }\r
393 \r
394         if( xErrorOccurred == pdTRUE )\r
395         {\r
396                 xReturn = pdFAIL;\r
397         }\r
398         else\r
399         {\r
400                 xReturn = pdTRUE;\r
401         }\r
402 \r
403         return xReturn;\r
404 }\r
405 \r
406 \r
407 \r
408 \r