]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/recmutex.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Demo / Common / Minimal / recmutex.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 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 /* The three tasks as described at the top of this file. */\r
89 static void prvRecursiveMutexControllingTask( void *pvParameters );\r
90 static void prvRecursiveMutexBlockingTask( void *pvParameters );\r
91 static void prvRecursiveMutexPollingTask( void *pvParameters );\r
92 \r
93 /* The mutex used by the demo. */\r
94 static SemaphoreHandle_t xMutex;\r
95 \r
96 /* Variables used to detect and latch errors. */\r
97 static volatile BaseType_t xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
98 static volatile UBaseType_t uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;\r
99 \r
100 /* Handles of the two higher priority tasks, required so they can be resumed\r
101 (unsuspended). */\r
102 static TaskHandle_t xControllingTaskHandle, xBlockingTaskHandle;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 void vStartRecursiveMutexTasks( void )\r
107 {\r
108         /* Just creates the mutex and the three tasks. */\r
109 \r
110         xMutex = xSemaphoreCreateRecursiveMutex();\r
111 \r
112         if( xMutex != NULL )\r
113         {\r
114                 /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
115                 in use.  The registry is provided as a means for kernel aware\r
116                 debuggers to locate mutex and has no purpose if a kernel aware debugger\r
117                 is not being used.  The call to vQueueAddToRegistry() will be removed\r
118                 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
119                 defined to be less than 1. */\r
120                 vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Recursive_Mutex" );\r
121 \r
122                 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );\r
123                 xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );\r
124                 xTaskCreate( prvRecursiveMutexPollingTask, "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );\r
125         }\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
130 {\r
131 UBaseType_t ux;\r
132 \r
133         /* Just to remove compiler warning. */\r
134         ( void ) pvParameters;\r
135 \r
136         for( ;; )\r
137         {\r
138                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
139                 it.   The first time through, the mutex will not have been used yet,\r
140                 subsequent times through, at this point the mutex will be held by the\r
141                 polling task. */\r
142                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
143                 {\r
144                         xErrorOccurred = pdTRUE;\r
145                 }\r
146 \r
147                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
148                 {\r
149                         /* We should now be able to take the mutex as many times as\r
150                         we like.\r
151 \r
152                         The first time through the mutex will be immediately available, on\r
153                         subsequent times through the mutex will be held by the polling task\r
154                         at this point and this Take will cause the polling task to inherit\r
155                         the priority of this task.  In this case the block time must be\r
156                         long enough to ensure the polling task will execute again before the\r
157                         block time expires.  If the block time does expire then the error\r
158                         flag will be set here. */\r
159                         if( xSemaphoreTakeRecursive( xMutex, recmu15ms_DELAY ) != pdPASS )\r
160                         {\r
161                                 xErrorOccurred = pdTRUE;\r
162                         }\r
163 \r
164                         /* Ensure the other task attempting to access the mutex (and the\r
165                         other demo tasks) are able to execute to ensure they either block\r
166                         (where a block time is specified) or return an error (where no\r
167                         block time is specified) as the mutex is held by this task. */\r
168                         vTaskDelay( recmuSHORT_DELAY );\r
169                 }\r
170 \r
171                 /* For each time we took the mutex, give it back. */\r
172                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
173                 {\r
174                         /* Ensure the other task attempting to access the mutex (and the\r
175                         other demo tasks) are able to execute. */\r
176                         vTaskDelay( recmuSHORT_DELAY );\r
177 \r
178                         /* We should now be able to give the mutex as many times as we\r
179                         took it.  When the mutex is available again the Blocking task\r
180                         should be unblocked but not run because it has a lower priority\r
181                         than this task.  The polling task should also not run at this point\r
182                         as it too has a lower priority than this task. */\r
183                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
184                         {\r
185                                 xErrorOccurred = pdTRUE;\r
186                         }\r
187 \r
188                         #if( configUSE_PREEMPTION == 0 )\r
189                                 taskYIELD();\r
190                         #endif\r
191                 }\r
192 \r
193                 /* Having given it back the same number of times as it was taken, we\r
194                 should no longer be the mutex owner, so the next give should fail. */\r
195                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
196                 {\r
197                         xErrorOccurred = pdTRUE;\r
198                 }\r
199 \r
200                 /* Keep count of the number of cycles this task has performed so a\r
201                 stall can be detected. */\r
202                 uxControllingCycles++;\r
203 \r
204                 /* Suspend ourselves so the blocking task can execute. */\r
205                 xControllingIsSuspended = pdTRUE;\r
206                 vTaskSuspend( NULL );\r
207                 xControllingIsSuspended = pdFALSE;\r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
213 {\r
214         /* Just to remove compiler warning. */\r
215         ( void ) pvParameters;\r
216 \r
217         for( ;; )\r
218         {\r
219                 /* This task will run while the controlling task is blocked, and the\r
220                 controlling task will block only once it has the mutex - therefore\r
221                 this call should block until the controlling task has given up the\r
222                 mutex, and not actually execute past this call until the controlling\r
223                 task is suspended.  portMAX_DELAY - 1 is used instead of portMAX_DELAY\r
224                 to ensure the task's state is reported as Blocked and not Suspended in\r
225                 a later call to configASSERT() (within the polling task). */\r
226                 if( xSemaphoreTakeRecursive( xMutex, ( portMAX_DELAY - 1 ) ) == pdPASS )\r
227                 {\r
228                         if( xControllingIsSuspended != pdTRUE )\r
229                         {\r
230                                 /* Did not expect to execute until the controlling task was\r
231                                 suspended. */\r
232                                 xErrorOccurred = pdTRUE;\r
233                         }\r
234                         else\r
235                         {\r
236                                 /* Give the mutex back before suspending ourselves to allow\r
237                                 the polling task to obtain the mutex. */\r
238                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
239                                 {\r
240                                         xErrorOccurred = pdTRUE;\r
241                                 }\r
242 \r
243                                 xBlockingIsSuspended = pdTRUE;\r
244                                 vTaskSuspend( NULL );\r
245                                 xBlockingIsSuspended = pdFALSE;\r
246                         }\r
247                 }\r
248                 else\r
249                 {\r
250                         /* We should not leave the xSemaphoreTakeRecursive() function\r
251                         until the mutex was obtained. */\r
252                         xErrorOccurred = pdTRUE;\r
253                 }\r
254 \r
255                 /* The controlling and blocking tasks should be in lock step. */\r
256                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\r
257                 {\r
258                         xErrorOccurred = pdTRUE;\r
259                 }\r
260 \r
261                 /* Keep count of the number of cycles this task has performed so a\r
262                 stall can be detected. */\r
263                 uxBlockingCycles++;\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
269 {\r
270         /* Just to remove compiler warning. */\r
271         ( void ) pvParameters;\r
272 \r
273         for( ;; )\r
274         {\r
275                 /* Keep attempting to obtain the mutex.  It should only be obtained when\r
276                 the blocking task has suspended itself, which in turn should only\r
277                 happen when the controlling task is also suspended. */\r
278                 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
279                 {\r
280                         #if( INCLUDE_eTaskGetState == 1 )\r
281                         {\r
282                                 configASSERT( eTaskGetState( xControllingTaskHandle ) == eSuspended );\r
283                                 configASSERT( eTaskGetState( xBlockingTaskHandle ) == eSuspended );\r
284                         }\r
285                         #endif /* INCLUDE_eTaskGetState */\r
286 \r
287                         /* Is the blocking task suspended? */\r
288                         if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
289                         {\r
290                                 xErrorOccurred = pdTRUE;\r
291                         }\r
292                         else\r
293                         {\r
294                                 /* Keep count of the number of cycles this task has performed\r
295                                 so a stall can be detected. */\r
296                                 uxPollingCycles++;\r
297 \r
298                                 /* We can resume the other tasks here even though they have a\r
299                                 higher priority than the polling task.  When they execute they\r
300                                 will attempt to obtain the mutex but fail because the polling\r
301                                 task is still the mutex holder.  The polling task (this task)\r
302                                 will then inherit the higher priority.  The Blocking task will\r
303                                 block indefinitely when it attempts to obtain the mutex, the\r
304                                 Controlling task will only block for a fixed period and an\r
305                                 error will be latched if the polling task has not returned the\r
306                                 mutex by the time this fixed period has expired. */\r
307                                 vTaskResume( xBlockingTaskHandle );\r
308                                 #if( configUSE_PREEMPTION == 0 )\r
309                                         taskYIELD();\r
310                                 #endif\r
311 \r
312                                 vTaskResume( xControllingTaskHandle );\r
313                                 #if( configUSE_PREEMPTION == 0 )\r
314                                         taskYIELD();\r
315                                 #endif\r
316 \r
317                                 /* The other two tasks should now have executed and no longer\r
318                                 be suspended. */\r
319                                 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
320                                 {\r
321                                         xErrorOccurred = pdTRUE;\r
322                                 }\r
323 \r
324                                 #if( INCLUDE_uxTaskPriorityGet == 1 )\r
325                                 {\r
326                                         /* Check priority inherited. */\r
327                                         configASSERT( uxTaskPriorityGet( NULL ) == recmuCONTROLLING_TASK_PRIORITY );\r
328                                 }\r
329                                 #endif /* INCLUDE_uxTaskPriorityGet */\r
330 \r
331                                 #if( INCLUDE_eTaskGetState == 1 )\r
332                                 {\r
333                                         configASSERT( eTaskGetState( xControllingTaskHandle ) == eBlocked );\r
334                                         configASSERT( eTaskGetState( xBlockingTaskHandle ) == eBlocked );\r
335                                 }\r
336                                 #endif /* INCLUDE_eTaskGetState */\r
337 \r
338                                 /* Release the mutex, disinheriting the higher priority again. */\r
339                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
340                                 {\r
341                                         xErrorOccurred = pdTRUE;\r
342                                 }\r
343 \r
344                                 #if( INCLUDE_uxTaskPriorityGet == 1 )\r
345                                 {\r
346                                         /* Check priority disinherited. */\r
347                                         configASSERT( uxTaskPriorityGet( NULL ) == recmuPOLLING_TASK_PRIORITY );\r
348                                 }\r
349                                 #endif /* INCLUDE_uxTaskPriorityGet */\r
350                         }\r
351                 }\r
352 \r
353                 #if configUSE_PREEMPTION == 0\r
354                 {\r
355                         taskYIELD();\r
356                 }\r
357                 #endif\r
358         }\r
359 }\r
360 /*-----------------------------------------------------------*/\r
361 \r
362 /* This is called to check that all the created tasks are still running. */\r
363 BaseType_t xAreRecursiveMutexTasksStillRunning( void )\r
364 {\r
365 BaseType_t xReturn;\r
366 static UBaseType_t uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
367 \r
368         /* Is the controlling task still cycling? */\r
369         if( uxLastControllingCycles == uxControllingCycles )\r
370         {\r
371                 xErrorOccurred = pdTRUE;\r
372         }\r
373         else\r
374         {\r
375                 uxLastControllingCycles = uxControllingCycles;\r
376         }\r
377 \r
378         /* Is the blocking task still cycling? */\r
379         if( uxLastBlockingCycles == uxBlockingCycles )\r
380         {\r
381                 xErrorOccurred = pdTRUE;\r
382         }\r
383         else\r
384         {\r
385                 uxLastBlockingCycles = uxBlockingCycles;\r
386         }\r
387 \r
388         /* Is the polling task still cycling? */\r
389         if( uxLastPollingCycles == uxPollingCycles )\r
390         {\r
391                 xErrorOccurred = pdTRUE;\r
392         }\r
393         else\r
394         {\r
395                 uxLastPollingCycles = uxPollingCycles;\r
396         }\r
397 \r
398         if( xErrorOccurred == pdTRUE )\r
399         {\r
400                 xReturn = pdFAIL;\r
401         }\r
402         else\r
403         {\r
404                 xReturn = pdPASS;\r
405         }\r
406 \r
407         return xReturn;\r
408 }\r
409 \r
410 \r
411 \r
412 \r