]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MingW/DemosModifiedForLowTickRate/recmutex.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / WIN32-MingW / DemosModifiedForLowTickRate / 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. */\r
73 #define recmuCONTROLLING_TASK_PRIORITY  ( tskIDLE_PRIORITY + 2 )\r
74 #define recmuBLOCKING_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
75 #define recmuPOLLING_TASK_PRIORITY              ( tskIDLE_PRIORITY + 0 )\r
76 \r
77 /* In this version the tick period is very long, so the short delay cannot be\r
78 for too many ticks, or the check task will execute and find that the recmutex\r
79 tasks have not completed their functionality and then signal an error.  The\r
80 delay does however have to be long enough to allow the lower priority tasks\r
81 a chance of executing - this is basically achieved by reducing the number\r
82 of times the loop that takes/gives the recursive mutex executes. */\r
83 #define recmuMAX_COUNT                                  ( 2 )\r
84 #define recmuSHORT_DELAY                                ( 20 )\r
85 #define recmuNO_DELAY                                   ( ( TickType_t ) 0 )\r
86 #define recmuFIVE_TICK_DELAY                    ( ( TickType_t ) 5 )\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 portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
98 static volatile unsigned portBASE_TYPE 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, xPollingTaskHandle;\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         /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
113         in use.  The registry is provided as a means for kernel aware \r
114         debuggers to locate mutex and has no purpose if a kernel aware debugger\r
115         is not being used.  The call to vQueueAddToRegistry() will be removed\r
116         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
117         defined to be less than 1. */\r
118         vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Recursive_Mutex" );\r
119 \r
120 \r
121         if( xMutex != NULL )\r
122         {\r
123                 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1Ctrl", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );\r
124         xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2Blck", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );\r
125         xTaskCreate( prvRecursiveMutexPollingTask, "Rec3Poll", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, &xPollingTaskHandle );\r
126         }\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
131 {\r
132 unsigned portBASE_TYPE ux;\r
133 \r
134         /* Just to remove compiler warning. */\r
135         ( void ) pvParameters;\r
136 \r
137         for( ;; )\r
138         {\r
139                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
140                 it.   The first time through, the mutex will not have been used yet,\r
141                 subsequent times through, at this point the mutex will be held by the\r
142                 polling task. */\r
143                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
144                 {\r
145                         xErrorOccurred = pdTRUE;\r
146                 }\r
147 \r
148                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
149                 {\r
150                         /* We should now be able to take the mutex as many times as\r
151                         we like.\r
152                         \r
153                         The first time through the mutex will be immediately available, on\r
154                         subsequent times through the mutex will be held by the polling task\r
155                         at this point and this Take will cause the polling task to inherit\r
156                         the priority of this task.  In this case the block time must be\r
157                         long enough to ensure the polling task will execute again before the\r
158                         block time expires.  If the block time does expire then the error\r
159                         flag will be set here. */\r
160                         if( xSemaphoreTakeRecursive( xMutex, recmuFIVE_TICK_DELAY ) != pdPASS )\r
161                         {\r
162                                 xErrorOccurred = pdTRUE;\r
163                         }\r
164 \r
165                         /* Ensure the other task attempting to access the mutex (and the\r
166                         other demo tasks) are able to execute to ensure they either block\r
167                         (where a block time is specified) or return an error (where no \r
168                         block time is specified) as the mutex is held by this task. */\r
169                         vTaskDelay( recmuSHORT_DELAY );\r
170                 }\r
171 \r
172                 /* For each time we took the mutex, give it back. */\r
173                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
174                 {\r
175                         /* Ensure the other task attempting to access the mutex (and the\r
176                         other demo tasks) are able to execute. */\r
177                         vTaskDelay( recmuSHORT_DELAY );\r
178 \r
179                         /* We should now be able to give the mutex as many times as we\r
180                         took it.  When the mutex is available again the Blocking task\r
181                         should be unblocked but not run because it has a lower priority\r
182                         than this task.  The polling task should also not run at this point\r
183                         as it too has a lower priority than this task. */\r
184                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
185                         {\r
186                                 xErrorOccurred = pdTRUE;\r
187                         }\r
188 \r
189                         #if configUSE_PREEMPTION == 0\r
190                                 taskYIELD();\r
191                         #endif\r
192                 }\r
193 \r
194                 /* Having given it back the same number of times as it was taken, we\r
195                 should no longer be the mutex owner, so the next give should fail. */\r
196                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
197                 {\r
198                         xErrorOccurred = pdTRUE;\r
199                 }\r
200 \r
201                 /* Keep count of the number of cycles this task has performed so a \r
202                 stall can be detected. */\r
203                 uxControllingCycles++;\r
204 \r
205                 /* Suspend ourselves so the blocking task can execute. */\r
206                 xControllingIsSuspended = pdTRUE;\r
207                 vTaskSuspend( NULL );\r
208                 xControllingIsSuspended = pdFALSE;\r
209         }\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
214 {\r
215         /* Just to remove compiler warning. */\r
216         ( void ) pvParameters;\r
217 \r
218         for( ;; )\r
219         {\r
220                 /* This task will run while the controlling task is blocked, and the\r
221                 controlling task will block only once it has the mutex - therefore\r
222                 this call should block until the controlling task has given up the \r
223                 mutex, and not actually execute past this call until the controlling \r
224                 task is suspended. */\r
225                 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )\r
226                 {\r
227                         if( xControllingIsSuspended != pdTRUE )\r
228                         {\r
229                                 /* Did not expect to execute until the controlling task was\r
230                                 suspended. */\r
231                                 xErrorOccurred = pdTRUE;\r
232                         }\r
233                         else\r
234                         {\r
235                                 /* Give the mutex back before suspending ourselves to allow\r
236                                 the polling task to obtain the mutex. */\r
237                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
238                                 {\r
239                                         xErrorOccurred = pdTRUE;\r
240                                 }\r
241 \r
242                                 xBlockingIsSuspended = pdTRUE;\r
243                                 vTaskSuspend( NULL );\r
244                                 xBlockingIsSuspended = pdFALSE;\r
245                         }\r
246                 }\r
247                 else\r
248                 {\r
249                         /* We should not leave the xSemaphoreTakeRecursive() function\r
250                         until the mutex was obtained. */\r
251                         xErrorOccurred = pdTRUE;\r
252                 }\r
253 \r
254                 /* The controlling and blocking tasks should be in lock step. */\r
255                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )\r
256                 {\r
257                         xErrorOccurred = pdTRUE;\r
258                 }\r
259 \r
260                 /* Keep count of the number of cycles this task has performed so a \r
261                 stall can be detected. */\r
262                 uxBlockingCycles++;\r
263         }\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 static void prvRecursiveMutexPollingTask( void *pvParameters )\r
268 {\r
269         /* Just to remove compiler warning. */\r
270         ( void ) pvParameters;\r
271 \r
272         for( ;; )\r
273         {\r
274                 /* Keep attempting to obtain the mutex.  We should only obtain it when\r
275                 the blocking task has suspended itself, which in turn should only\r
276                 happen when the controlling task is also suspended. */\r
277                 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )\r
278                 {\r
279                         /* Is the blocking task suspended? */\r
280                         if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
281                         {\r
282                                 xErrorOccurred = pdTRUE;\r
283                         }\r
284                         else\r
285                         {\r
286                                 /* Keep count of the number of cycles this task has performed \r
287                                 so a stall can be detected. */\r
288                                 uxPollingCycles++;\r
289 \r
290                                 /* We can resume the other tasks here even though they have a\r
291                                 higher priority than the polling task.  When they execute they\r
292                                 will attempt to obtain the mutex but fail because the polling\r
293                                 task is still the mutex holder.  The polling task (this task)\r
294                                 will then inherit the higher priority.  The Blocking task will\r
295                                 block indefinitely when it attempts to obtain the mutex, the\r
296                                 Controlling task will only block for a fixed period and an\r
297                                 error will be latched if the polling task has not returned the\r
298                                 mutex by the time this fixed period has expired. */                             \r
299                                 vTaskResume( xBlockingTaskHandle );\r
300                                 #if configUSE_PREEMPTION == 0\r
301                                         taskYIELD();\r
302                                 #endif\r
303 \r
304                                 vTaskResume( xControllingTaskHandle );\r
305                                 #if configUSE_PREEMPTION == 0\r
306                                         taskYIELD();\r
307                                 #endif\r
308 \r
309                                 /* The other two tasks should now have executed and no longer\r
310                                 be suspended. */\r
311                                 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
312                                 {\r
313                                         xErrorOccurred = pdTRUE;\r
314                                 }                               \r
315                         \r
316                                 /* Release the mutex, disinheriting the higher priority again. */\r
317                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
318                                 {\r
319                                         xErrorOccurred = pdTRUE;\r
320                                 }\r
321 \r
322                                 #if configUSE_PREEMPTION == 0\r
323                                         taskYIELD();\r
324                                 #endif\r
325                         }\r
326                 }\r
327 \r
328                 #if configUSE_PREEMPTION == 0\r
329                 {\r
330                         taskYIELD();\r
331                 }\r
332                 #endif\r
333         }\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 /* This is called to check that all the created tasks are still running. */\r
338 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )\r
339 {\r
340 portBASE_TYPE xReturn;\r
341 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
342 \r
343         /* Is the controlling task still cycling? */\r
344         if( uxLastControllingCycles == uxControllingCycles )\r
345         {\r
346                 xErrorOccurred = pdTRUE;\r
347         }\r
348         else\r
349         {\r
350                 uxLastControllingCycles = uxControllingCycles;\r
351         }\r
352 \r
353         /* Is the blocking task still cycling? */\r
354         if( uxLastBlockingCycles == uxBlockingCycles )\r
355         {\r
356                 xErrorOccurred = pdTRUE;\r
357         }\r
358         else\r
359         {\r
360                 uxLastBlockingCycles = uxBlockingCycles;\r
361         }\r
362 \r
363         /* Is the polling task still cycling? */\r
364         if( uxLastPollingCycles == uxPollingCycles )\r
365         {\r
366                 xErrorOccurred = pdTRUE;\r
367         }\r
368         else\r
369         {\r
370                 uxLastPollingCycles = uxPollingCycles;\r
371         }\r
372 \r
373         if( xErrorOccurred == pdTRUE )\r
374         {\r
375                 xReturn = pdFAIL;\r
376         }\r
377         else\r
378         {\r
379                 xReturn = pdTRUE;\r
380         }\r
381 \r
382         return xReturn;\r
383 }\r
384 \r
385 \r
386 \r
387 \r