]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/recmutex.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / Common / Minimal / recmutex.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97         The tasks defined on this page demonstrate the use of recursive mutexes.\r
98 \r
99         For recursive mutex functionality the created mutex should be created using\r
100         xSemaphoreCreateRecursiveMutex(), then be manipulated\r
101         using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API\r
102         functions.\r
103 \r
104         This demo creates three tasks all of which access the same recursive mutex:\r
105 \r
106         prvRecursiveMutexControllingTask() has the highest priority so executes\r
107         first and grabs the mutex.  It then performs some recursive accesses -\r
108         between each of which it sleeps for a short period to let the lower\r
109         priority tasks execute.  When it has completed its demo functionality\r
110         it gives the mutex back before suspending itself.\r
111 \r
112         prvRecursiveMutexBlockingTask() attempts to access the mutex by performing\r
113         a blocking 'take'.  The blocking task has a lower priority than the\r
114         controlling     task so by the time it executes the mutex has already been\r
115         taken by the controlling task,  causing the blocking task to block.  It\r
116         does not unblock until the controlling task has given the mutex back,\r
117         and it does not actually run until the controlling task has suspended\r
118         itself (due to the relative priorities).  When it eventually does obtain\r
119         the mutex all it does is give the mutex back prior to also suspending\r
120         itself.  At this point both the controlling task and the blocking task are\r
121         suspended.\r
122 \r
123         prvRecursiveMutexPollingTask() runs at the idle priority.  It spins round\r
124         a tight loop attempting to obtain the mutex with a non-blocking call.  As\r
125         the lowest priority task it will not successfully obtain the mutex until\r
126         both the controlling and blocking tasks are suspended.  Once it eventually\r
127         does obtain the mutex it first unsuspends both the controlling task and\r
128         blocking task prior to giving the mutex back - resulting in the polling\r
129         task temporarily inheriting the controlling tasks priority.\r
130 */\r
131 \r
132 /* Scheduler include files. */\r
133 #include "FreeRTOS.h"\r
134 #include "task.h"\r
135 #include "semphr.h"\r
136 \r
137 /* Demo app include files. */\r
138 #include "recmutex.h"\r
139 \r
140 /* Priorities assigned to the three tasks.  recmuCONTROLLING_TASK_PRIORITY can\r
141 be overridden by a definition in FreeRTOSConfig.h. */\r
142 #ifndef recmuCONTROLLING_TASK_PRIORITY\r
143         #define recmuCONTROLLING_TASK_PRIORITY  ( tskIDLE_PRIORITY + 2 )\r
144 #endif\r
145 #define recmuBLOCKING_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
146 #define recmuPOLLING_TASK_PRIORITY              ( tskIDLE_PRIORITY + 0 )\r
147 \r
148 /* The recursive call depth. */\r
149 #define recmuMAX_COUNT                                  ( 10 )\r
150 \r
151 /* Misc. */\r
152 #define recmuSHORT_DELAY                                ( 20 / portTICK_PERIOD_MS )\r
153 #define recmuNO_DELAY                                   ( ( TickType_t ) 0 )\r
154 #define recmuEIGHT_TICK_DELAY                   ( ( TickType_t ) 8 )\r
155 \r
156 /* The three tasks as described at the top of this file. */\r
157 static void prvRecursiveMutexControllingTask( void *pvParameters );\r
158 static void prvRecursiveMutexBlockingTask( void *pvParameters );\r
159 static void prvRecursiveMutexPollingTask( void *pvParameters );\r
160 \r
161 /* The mutex used by the demo. */\r
162 static SemaphoreHandle_t xMutex;\r
163 \r
164 /* Variables used to detect and latch errors. */\r
165 static volatile BaseType_t xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
166 static volatile UBaseType_t uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;\r
167 \r
168 /* Handles of the two higher priority tasks, required so they can be resumed\r
169 (unsuspended). */\r
170 static TaskHandle_t xControllingTaskHandle, xBlockingTaskHandle;\r
171 \r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void vStartRecursiveMutexTasks( void )\r
175 {\r
176         /* Just creates the mutex and the three tasks. */\r
177 \r
178         xMutex = xSemaphoreCreateRecursiveMutex();\r
179 \r
180         /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
181         in use.  The registry is provided as a means for kernel aware\r
182         debuggers to locate mutex and has no purpose if a kernel aware debugger\r
183         is not being used.  The call to vQueueAddToRegistry() will be removed\r
184         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
185         defined to be less than 1. */\r
186         vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Recursive_Mutex" );\r
187 \r
188 \r
189         if( xMutex != NULL )\r
190         {\r
191                 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );\r
192         xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );\r
193         xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
199 {\r
200 UBaseType_t ux;\r
201 \r
202         /* Just to remove compiler warning. */\r
203         ( void ) pvParameters;\r
204 \r
205         for( ;; )\r
206         {\r
207                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
208                 it.   The first time through, the mutex will not have been used yet,\r
209                 subsequent times through, at this point the mutex will be held by the\r
210                 polling task. */\r
211                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
212                 {\r
213                         xErrorOccurred = pdTRUE;\r
214                 }\r
215 \r
216                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
217                 {\r
218                         /* We should now be able to take the mutex as many times as\r
219                         we like.\r
220 \r
221                         The first time through the mutex will be immediately available, on\r
222                         subsequent times through the mutex will be held by the polling task\r
223                         at this point and this Take will cause the polling task to inherit\r
224                         the priority of this task.  In this case the block time must be\r
225                         long enough to ensure the polling task will execute again before the\r
226                         block time expires.  If the block time does expire then the error\r
227                         flag will be set here. */\r
228                         if( xSemaphoreTakeRecursive( xMutex, recmuEIGHT_TICK_DELAY ) != pdPASS )\r
229                         {\r
230                                 xErrorOccurred = pdTRUE;\r
231                         }\r
232 \r
233                         /* Ensure the other task attempting to access the mutex (and the\r
234                         other demo tasks) are able to execute to ensure they either block\r
235                         (where a block time is specified) or return an error (where no\r
236                         block time is specified) as the mutex is held by this task. */\r
237                         vTaskDelay( recmuSHORT_DELAY );\r
238                 }\r
239 \r
240                 /* For each time we took the mutex, give it back. */\r
241                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
242                 {\r
243                         /* Ensure the other task attempting to access the mutex (and the\r
244                         other demo tasks) are able to execute. */\r
245                         vTaskDelay( recmuSHORT_DELAY );\r
246 \r
247                         /* We should now be able to give the mutex as many times as we\r
248                         took it.  When the mutex is available again the Blocking task\r
249                         should be unblocked but not run because it has a lower priority\r
250                         than this task.  The polling task should also not run at this point\r
251                         as it too has a lower priority than this task. */\r
252                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
253                         {\r
254                                 xErrorOccurred = pdTRUE;\r
255                         }\r
256                 }\r
257 \r
258                 /* Having given it back the same number of times as it was taken, we\r
259                 should no longer be the mutex owner, so the next give should fail. */\r
260                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
261                 {\r
262                         xErrorOccurred = pdTRUE;\r
263                 }\r
264 \r
265                 /* Keep count of the number of cycles this task has performed so a\r
266                 stall can be detected. */\r
267                 uxControllingCycles++;\r
268 \r
269                 /* Suspend ourselves so the blocking task can execute. */\r
270                 xControllingIsSuspended = pdTRUE;\r
271                 vTaskSuspend( NULL );\r
272                 xControllingIsSuspended = pdFALSE;\r
273         }\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
278 {\r
279         /* Just to remove compiler warning. */\r
280         ( void ) pvParameters;\r
281 \r
282         for( ;; )\r
283         {\r
284                 /* This task will run while the controlling task is blocked, and the\r
285                 controlling task will block only once it has the mutex - therefore\r
286                 this call should block until the controlling task has given up the\r
287                 mutex, and not actually execute past this call until the controlling\r
288                 task is suspended.  portMAX_DELAY - 1 is used instead of portMAX_DELAY\r
289                 to ensure the task's state is reported as Blocked and not Suspended in\r
290                 a later call to configASSERT() (within the polling task). */\r
291                 if( xSemaphoreTakeRecursive( xMutex, ( portMAX_DELAY - 1 ) ) == pdPASS )\r
292                 {\r
293                         if( xControllingIsSuspended != pdTRUE )\r
294                         {\r
295                                 /* Did not expect to execute until the controlling task was\r
296                                 suspended. */\r
297                                 xErrorOccurred = pdTRUE;\r
298                         }\r
299                         else\r
300                         {\r
301                                 /* Give the mutex back before suspending ourselves to allow\r
302                                 the polling task to obtain the mutex. */\r
303                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
304                                 {\r
305                                         xErrorOccurred = pdTRUE;\r
306                                 }\r
307 \r
308                                 xBlockingIsSuspended = pdTRUE;\r
309                                 vTaskSuspend( NULL );\r
310                                 xBlockingIsSuspended = pdFALSE;\r
311                         }\r
312                 }\r
313                 else\r
314                 {\r
315                         /* We should not leave the xSemaphoreTakeRecursive() function\r
316                         until the mutex was obtained. */\r
317                         xErrorOccurred = pdTRUE;\r
318                 }\r
319 \r
320                 /* The controlling and blocking tasks should be in lock step. */\r
321                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\r
322                 {\r
323                         xErrorOccurred = pdTRUE;\r
324                 }\r
325 \r
326                 /* Keep count of the number of cycles this task has performed so a\r
327                 stall can be detected. */\r
328                 uxBlockingCycles++;\r
329         }\r
330 }\r
331 /*-----------------------------------------------------------*/\r
332 \r
333 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
334 {\r
335         /* Just to remove compiler warning. */\r
336         ( void ) pvParameters;\r
337 \r
338         for( ;; )\r
339         {\r
340                 /* Keep attempting to obtain the mutex.  We should only obtain it when\r
341                 the blocking task has suspended itself, which in turn should only\r
342                 happen when the controlling task is also suspended. */\r
343                 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
344                 {\r
345                         #if( INCLUDE_eTaskGetState == 1 )\r
346                         {\r
347                                 configASSERT( eTaskGetState( xControllingTaskHandle ) == eSuspended );\r
348                                 configASSERT( eTaskGetState( xBlockingTaskHandle ) == eSuspended );\r
349                         }\r
350                         #endif /* INCLUDE_eTaskGetState */\r
351 \r
352                         /* Is the blocking task suspended? */\r
353                         if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
354                         {\r
355                                 xErrorOccurred = pdTRUE;\r
356                         }\r
357                         else\r
358                         {\r
359                                 /* Keep count of the number of cycles this task has performed\r
360                                 so a stall can be detected. */\r
361                                 uxPollingCycles++;\r
362 \r
363                                 /* We can resume the other tasks here even though they have a\r
364                                 higher priority than the polling task.  When they execute they\r
365                                 will attempt to obtain the mutex but fail because the polling\r
366                                 task is still the mutex holder.  The polling task (this task)\r
367                                 will then inherit the higher priority.  The Blocking task will\r
368                                 block indefinitely when it attempts to obtain the mutex, the\r
369                                 Controlling task will only block for a fixed period and an\r
370                                 error will be latched if the polling task has not returned the\r
371                                 mutex by the time this fixed period has expired. */\r
372                                 vTaskResume( xBlockingTaskHandle );\r
373                 vTaskResume( xControllingTaskHandle );\r
374 \r
375                                 /* The other two tasks should now have executed and no longer\r
376                                 be suspended. */\r
377                                 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
378                                 {\r
379                                         xErrorOccurred = pdTRUE;\r
380                                 }\r
381 \r
382                                 #if( INCLUDE_uxTaskPriorityGet == 1 )\r
383                                 {\r
384                                         /* Check priority inherited. */\r
385                                         configASSERT( uxTaskPriorityGet( NULL ) == recmuCONTROLLING_TASK_PRIORITY );\r
386                                 }\r
387                                 #endif /* INCLUDE_uxTaskPriorityGet */\r
388 \r
389                                 #if( INCLUDE_eTaskGetState == 1 )\r
390                                 {\r
391                                         configASSERT( eTaskGetState( xControllingTaskHandle ) == eBlocked );\r
392                                         configASSERT( eTaskGetState( xBlockingTaskHandle ) == eBlocked );\r
393                                 }\r
394                                 #endif /* INCLUDE_eTaskGetState */\r
395 \r
396                                 /* Release the mutex, disinheriting the higher priority again. */\r
397                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
398                                 {\r
399                                         xErrorOccurred = pdTRUE;\r
400                                 }\r
401 \r
402                                 #if( INCLUDE_uxTaskPriorityGet == 1 )\r
403                                 {\r
404                                         /* Check priority disinherited. */\r
405                                         configASSERT( uxTaskPriorityGet( NULL ) == recmuPOLLING_TASK_PRIORITY );\r
406                                 }\r
407                                 #endif /* INCLUDE_uxTaskPriorityGet */\r
408                         }\r
409                 }\r
410 \r
411                 #if configUSE_PREEMPTION == 0\r
412                 {\r
413                         taskYIELD();\r
414                 }\r
415                 #endif\r
416         }\r
417 }\r
418 /*-----------------------------------------------------------*/\r
419 \r
420 /* This is called to check that all the created tasks are still running. */\r
421 BaseType_t xAreRecursiveMutexTasksStillRunning( void )\r
422 {\r
423 BaseType_t xReturn;\r
424 static UBaseType_t uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
425 \r
426         /* Is the controlling task still cycling? */\r
427         if( uxLastControllingCycles == uxControllingCycles )\r
428         {\r
429                 xErrorOccurred = pdTRUE;\r
430         }\r
431         else\r
432         {\r
433                 uxLastControllingCycles = uxControllingCycles;\r
434         }\r
435 \r
436         /* Is the blocking task still cycling? */\r
437         if( uxLastBlockingCycles == uxBlockingCycles )\r
438         {\r
439                 xErrorOccurred = pdTRUE;\r
440         }\r
441         else\r
442         {\r
443                 uxLastBlockingCycles = uxBlockingCycles;\r
444         }\r
445 \r
446         /* Is the polling task still cycling? */\r
447         if( uxLastPollingCycles == uxPollingCycles )\r
448         {\r
449                 xErrorOccurred = pdTRUE;\r
450         }\r
451         else\r
452         {\r
453                 uxLastPollingCycles = uxPollingCycles;\r
454         }\r
455 \r
456         if( xErrorOccurred == pdTRUE )\r
457         {\r
458                 xReturn = pdFAIL;\r
459         }\r
460         else\r
461         {\r
462                 xReturn = pdTRUE;\r
463         }\r
464 \r
465         return xReturn;\r
466 }\r
467 \r
468 \r
469 \r
470 \r