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