]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/recmutex.c
81fd493080d119d37ecaeca39dbbd428138725ae
[freertos] / FreeRTOS / Demo / Common / Minimal / recmutex.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29         The tasks defined on this page demonstrate the use of recursive mutexes.\r
30 \r
31         For recursive mutex functionality the created mutex should be created using\r
32         xSemaphoreCreateRecursiveMutex(), then be manipulated\r
33         using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API\r
34         functions.\r
35 \r
36         This demo creates three tasks all of which access the same recursive mutex:\r
37 \r
38         prvRecursiveMutexControllingTask() has the highest priority so executes\r
39         first and grabs the mutex.  It then performs some recursive accesses -\r
40         between each of which it sleeps for a short period to let the lower\r
41         priority tasks execute.  When it has completed its demo functionality\r
42         it gives the mutex back before suspending itself.\r
43 \r
44         prvRecursiveMutexBlockingTask() attempts to access the mutex by performing\r
45         a blocking 'take'.  The blocking task has a lower priority than the\r
46         controlling     task so by the time it executes the mutex has already been\r
47         taken by the controlling task,  causing the blocking task to block.  It\r
48         does not unblock until the controlling task has given the mutex back,\r
49         and it does not actually run until the controlling task has suspended\r
50         itself (due to the relative priorities).  When it eventually does obtain\r
51         the mutex all it does is give the mutex back prior to also suspending\r
52         itself.  At this point both the controlling task and the blocking task are\r
53         suspended.\r
54 \r
55         prvRecursiveMutexPollingTask() runs at the idle priority.  It spins round\r
56         a tight loop attempting to obtain the mutex with a non-blocking call.  As\r
57         the lowest priority task it will not successfully obtain the mutex until\r
58         both the controlling and blocking tasks are suspended.  Once it eventually\r
59         does obtain the mutex it first unsuspends both the controlling task and\r
60         blocking task prior to giving the mutex back - resulting in the polling\r
61         task temporarily inheriting the controlling tasks priority.\r
62 */\r
63 \r
64 /* Scheduler include files. */\r
65 #include "FreeRTOS.h"\r
66 #include "task.h"\r
67 #include "semphr.h"\r
68 \r
69 /* Demo app include files. */\r
70 #include "recmutex.h"\r
71 \r
72 /* Priorities assigned to the three tasks.  recmuCONTROLLING_TASK_PRIORITY can\r
73 be overridden by a definition in FreeRTOSConfig.h. */\r
74 #ifndef recmuCONTROLLING_TASK_PRIORITY\r
75         #define recmuCONTROLLING_TASK_PRIORITY  ( tskIDLE_PRIORITY + 2 )\r
76 #endif\r
77 #define recmuBLOCKING_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
78 #define recmuPOLLING_TASK_PRIORITY              ( tskIDLE_PRIORITY + 0 )\r
79 \r
80 /* The recursive call depth. */\r
81 #define recmuMAX_COUNT                                  ( 10 )\r
82 \r
83 /* Misc. */\r
84 #define recmuSHORT_DELAY                                ( pdMS_TO_TICKS( 20 ) )\r
85 #define recmuNO_DELAY                                   ( ( TickType_t ) 0 )\r
86 #define recmu15ms_DELAY                                 ( pdMS_TO_TICKS( 15 ) )\r
87 \r
88 #ifndef recmuRECURSIVE_MUTEX_TEST_TASK_STACK_SIZE\r
89         #define recmuRECURSIVE_MUTEX_TEST_TASK_STACK_SIZE configMINIMAL_STACK_SIZE\r
90 #endif\r
91 \r
92 /* The three tasks as described at the top of this file. */\r
93 static void prvRecursiveMutexControllingTask( void *pvParameters );\r
94 static void prvRecursiveMutexBlockingTask( void *pvParameters );\r
95 static void prvRecursiveMutexPollingTask( void *pvParameters );\r
96 \r
97 /* The mutex used by the demo. */\r
98 static SemaphoreHandle_t xMutex;\r
99 \r
100 /* Variables used to detect and latch errors. */\r
101 static volatile BaseType_t xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
102 static volatile UBaseType_t uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;\r
103 \r
104 /* Handles of the two higher priority tasks, required so they can be resumed\r
105 (unsuspended). */\r
106 static TaskHandle_t xControllingTaskHandle, xBlockingTaskHandle;\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 void vStartRecursiveMutexTasks( void )\r
111 {\r
112         /* Just creates the mutex and the three tasks. */\r
113 \r
114         xMutex = xSemaphoreCreateRecursiveMutex();\r
115 \r
116         if( xMutex != NULL )\r
117         {\r
118                 /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
119                 in use.  The registry is provided as a means for kernel aware\r
120                 debuggers to locate mutex and has no purpose if a kernel aware debugger\r
121                 is not being used.  The call to vQueueAddToRegistry() will be removed\r
122                 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
123                 defined to be less than 1. */\r
124                 vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Recursive_Mutex" );\r
125 \r
126                 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", recmuRECURSIVE_MUTEX_TEST_TASK_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );\r
127                 xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", recmuRECURSIVE_MUTEX_TEST_TASK_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );\r
128                 xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", recmuRECURSIVE_MUTEX_TEST_TASK_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );\r
129         }\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
134 {\r
135 UBaseType_t ux;\r
136 \r
137         /* Just to remove compiler warning. */\r
138         ( void ) pvParameters;\r
139 \r
140         for( ;; )\r
141         {\r
142                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
143                 it.   The first time through, the mutex will not have been used yet,\r
144                 subsequent times through, at this point the mutex will be held by the\r
145                 polling task. */\r
146                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
147                 {\r
148                         xErrorOccurred = pdTRUE;\r
149                 }\r
150 \r
151                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
152                 {\r
153                         /* We should now be able to take the mutex as many times as\r
154                         we like.\r
155 \r
156                         The first time through the mutex will be immediately available, on\r
157                         subsequent times through the mutex will be held by the polling task\r
158                         at this point and this Take will cause the polling task to inherit\r
159                         the priority of this task.  In this case the block time must be\r
160                         long enough to ensure the polling task will execute again before the\r
161                         block time expires.  If the block time does expire then the error\r
162                         flag will be set here. */\r
163                         if( xSemaphoreTakeRecursive( xMutex, recmu15ms_DELAY ) != pdPASS )\r
164                         {\r
165                                 xErrorOccurred = pdTRUE;\r
166                         }\r
167 \r
168                         /* Ensure the other task attempting to access the mutex (and the\r
169                         other demo tasks) are able to execute to ensure they either block\r
170                         (where a block time is specified) or return an error (where no\r
171                         block time is specified) as the mutex is held by this task. */\r
172                         vTaskDelay( recmuSHORT_DELAY );\r
173                 }\r
174 \r
175                 /* For each time we took the mutex, give it back. */\r
176                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
177                 {\r
178                         /* Ensure the other task attempting to access the mutex (and the\r
179                         other demo tasks) are able to execute. */\r
180                         vTaskDelay( recmuSHORT_DELAY );\r
181 \r
182                         /* We should now be able to give the mutex as many times as we\r
183                         took it.  When the mutex is available again the Blocking task\r
184                         should be unblocked but not run because it has a lower priority\r
185                         than this task.  The polling task should also not run at this point\r
186                         as it too has a lower priority than this task. */\r
187                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
188                         {\r
189                                 xErrorOccurred = pdTRUE;\r
190                         }\r
191 \r
192                         #if( configUSE_PREEMPTION == 0 )\r
193                                 taskYIELD();\r
194                         #endif\r
195                 }\r
196 \r
197                 /* Having given it back the same number of times as it was taken, we\r
198                 should no longer be the mutex owner, so the next give should fail. */\r
199                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
200                 {\r
201                         xErrorOccurred = pdTRUE;\r
202                 }\r
203 \r
204                 /* Keep count of the number of cycles this task has performed so a\r
205                 stall can be detected. */\r
206                 uxControllingCycles++;\r
207 \r
208                 /* Suspend ourselves so the blocking task can execute. */\r
209                 xControllingIsSuspended = pdTRUE;\r
210                 vTaskSuspend( NULL );\r
211                 xControllingIsSuspended = pdFALSE;\r
212         }\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
217 {\r
218         /* Just to remove compiler warning. */\r
219         ( void ) pvParameters;\r
220 \r
221         for( ;; )\r
222         {\r
223                 /* This task will run while the controlling task is blocked, and the\r
224                 controlling task will block only once it has the mutex - therefore\r
225                 this call should block until the controlling task has given up the\r
226                 mutex, and not actually execute past this call until the controlling\r
227                 task is suspended.  portMAX_DELAY - 1 is used instead of portMAX_DELAY\r
228                 to ensure the task's state is reported as Blocked and not Suspended in\r
229                 a later call to configASSERT() (within the polling task). */\r
230                 if( xSemaphoreTakeRecursive( xMutex, ( portMAX_DELAY - 1 ) ) == pdPASS )\r
231                 {\r
232                         if( xControllingIsSuspended != pdTRUE )\r
233                         {\r
234                                 /* Did not expect to execute until the controlling task was\r
235                                 suspended. */\r
236                                 xErrorOccurred = pdTRUE;\r
237                         }\r
238                         else\r
239                         {\r
240                                 /* Give the mutex back before suspending ourselves to allow\r
241                                 the polling task to obtain the mutex. */\r
242                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
243                                 {\r
244                                         xErrorOccurred = pdTRUE;\r
245                                 }\r
246 \r
247                                 xBlockingIsSuspended = pdTRUE;\r
248                                 vTaskSuspend( NULL );\r
249                                 xBlockingIsSuspended = pdFALSE;\r
250                         }\r
251                 }\r
252                 else\r
253                 {\r
254                         /* We should not leave the xSemaphoreTakeRecursive() function\r
255                         until the mutex was obtained. */\r
256                         xErrorOccurred = pdTRUE;\r
257                 }\r
258 \r
259                 /* The controlling and blocking tasks should be in lock step. */\r
260                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\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                 uxBlockingCycles++;\r
268         }\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
273 {\r
274         /* Just to remove compiler warning. */\r
275         ( void ) pvParameters;\r
276 \r
277         for( ;; )\r
278         {\r
279                 /* Keep attempting to obtain the mutex.  It should only be obtained when\r
280                 the blocking task has suspended itself, which in turn should only\r
281                 happen when the controlling task is also suspended. */\r
282                 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
283                 {\r
284                         #if( INCLUDE_eTaskGetState == 1 )\r
285                         {\r
286                                 configASSERT( eTaskGetState( xControllingTaskHandle ) == eSuspended );\r
287                                 configASSERT( eTaskGetState( xBlockingTaskHandle ) == eSuspended );\r
288                         }\r
289                         #endif /* INCLUDE_eTaskGetState */\r
290 \r
291                         /* Is the blocking task suspended? */\r
292                         if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
293                         {\r
294                                 xErrorOccurred = pdTRUE;\r
295                         }\r
296                         else\r
297                         {\r
298                                 /* Keep count of the number of cycles this task has performed\r
299                                 so a stall can be detected. */\r
300                                 uxPollingCycles++;\r
301 \r
302                                 /* We can resume the other tasks here even though they have a\r
303                                 higher priority than the polling task.  When they execute they\r
304                                 will attempt to obtain the mutex but fail because the polling\r
305                                 task is still the mutex holder.  The polling task (this task)\r
306                                 will then inherit the higher priority.  The Blocking task will\r
307                                 block indefinitely when it attempts to obtain the mutex, the\r
308                                 Controlling task will only block for a fixed period and an\r
309                                 error will be latched if the polling task has not returned the\r
310                                 mutex by the time this fixed period has expired. */\r
311                                 vTaskResume( xBlockingTaskHandle );\r
312                                 #if( configUSE_PREEMPTION == 0 )\r
313                                         taskYIELD();\r
314                                 #endif\r
315 \r
316                                 vTaskResume( xControllingTaskHandle );\r
317                                 #if( configUSE_PREEMPTION == 0 )\r
318                                         taskYIELD();\r
319                                 #endif\r
320 \r
321                                 /* The other two tasks should now have executed and no longer\r
322                                 be suspended. */\r
323                                 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
324                                 {\r
325                                         xErrorOccurred = pdTRUE;\r
326                                 }\r
327 \r
328                                 #if( INCLUDE_uxTaskPriorityGet == 1 )\r
329                                 {\r
330                                         /* Check priority inherited. */\r
331                                         configASSERT( uxTaskPriorityGet( NULL ) == recmuCONTROLLING_TASK_PRIORITY );\r
332                                 }\r
333                                 #endif /* INCLUDE_uxTaskPriorityGet */\r
334 \r
335                                 #if( INCLUDE_eTaskGetState == 1 )\r
336                                 {\r
337                                         configASSERT( eTaskGetState( xControllingTaskHandle ) == eBlocked );\r
338                                         configASSERT( eTaskGetState( xBlockingTaskHandle ) == eBlocked );\r
339                                 }\r
340                                 #endif /* INCLUDE_eTaskGetState */\r
341 \r
342                                 /* Release the mutex, disinheriting the higher priority again. */\r
343                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
344                                 {\r
345                                         xErrorOccurred = pdTRUE;\r
346                                 }\r
347 \r
348                                 #if( INCLUDE_uxTaskPriorityGet == 1 )\r
349                                 {\r
350                                         /* Check priority disinherited. */\r
351                                         configASSERT( uxTaskPriorityGet( NULL ) == recmuPOLLING_TASK_PRIORITY );\r
352                                 }\r
353                                 #endif /* INCLUDE_uxTaskPriorityGet */\r
354                         }\r
355                 }\r
356 \r
357                 #if configUSE_PREEMPTION == 0\r
358                 {\r
359                         taskYIELD();\r
360                 }\r
361                 #endif\r
362         }\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 /* This is called to check that all the created tasks are still running. */\r
367 BaseType_t xAreRecursiveMutexTasksStillRunning( void )\r
368 {\r
369 BaseType_t xReturn;\r
370 static UBaseType_t uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
371 \r
372         /* Is the controlling task still cycling? */\r
373         if( uxLastControllingCycles == uxControllingCycles )\r
374         {\r
375                 xErrorOccurred = pdTRUE;\r
376         }\r
377         else\r
378         {\r
379                 uxLastControllingCycles = uxControllingCycles;\r
380         }\r
381 \r
382         /* Is the blocking task still cycling? */\r
383         if( uxLastBlockingCycles == uxBlockingCycles )\r
384         {\r
385                 xErrorOccurred = pdTRUE;\r
386         }\r
387         else\r
388         {\r
389                 uxLastBlockingCycles = uxBlockingCycles;\r
390         }\r
391 \r
392         /* Is the polling task still cycling? */\r
393         if( uxLastPollingCycles == uxPollingCycles )\r
394         {\r
395                 xErrorOccurred = pdTRUE;\r
396         }\r
397         else\r
398         {\r
399                 uxLastPollingCycles = uxPollingCycles;\r
400         }\r
401 \r
402         if( xErrorOccurred == pdTRUE )\r
403         {\r
404                 xReturn = pdFAIL;\r
405         }\r
406         else\r
407         {\r
408                 xReturn = pdPASS;\r
409         }\r
410 \r
411         return xReturn;\r
412 }\r
413 \r
414 \r
415 \r
416 \r