]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MingW/DemosModifiedForLowTickRate/recmutex.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / WIN32-MingW / DemosModifiedForLowTickRate / recmutex.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30         The tasks defined on this page demonstrate the use of recursive mutexes.\r
31 \r
32         For recursive mutex functionality the created mutex should be created using\r
33         xSemaphoreCreateRecursiveMutex(), then be manipulated\r
34         using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API\r
35         functions.\r
36 \r
37         This demo creates three tasks all of which access the same recursive mutex:\r
38 \r
39         prvRecursiveMutexControllingTask() has the highest priority so executes \r
40         first and grabs the mutex.  It then performs some recursive accesses - \r
41         between each of which it sleeps for a short period to let the lower \r
42         priority tasks execute.  When it has completed its demo functionality\r
43         it gives the mutex back before suspending itself.\r
44 \r
45         prvRecursiveMutexBlockingTask() attempts to access the mutex by performing\r
46         a blocking 'take'.  The blocking task has a lower priority than the \r
47         controlling     task so by the time it executes the mutex has already been\r
48         taken by the controlling task,  causing the blocking task to block.  It \r
49         does not unblock until the controlling task has given the mutex back, \r
50         and it does not actually run until the controlling task has suspended \r
51         itself (due to the relative priorities).  When it eventually does obtain\r
52         the mutex all it does is give the mutex back prior to also suspending \r
53         itself.  At this point both the controlling task and the blocking task are \r
54         suspended.\r
55 \r
56         prvRecursiveMutexPollingTask() runs at the idle priority.  It spins round\r
57         a tight loop attempting to obtain the mutex with a non-blocking call.  As\r
58         the lowest priority task it will not successfully obtain the mutex until\r
59         both the controlling and blocking tasks are suspended.  Once it eventually \r
60         does obtain the mutex it first unsuspends both the controlling task and\r
61         blocking task prior to giving the mutex back - resulting in the polling\r
62         task temporarily inheriting the controlling tasks priority.\r
63 */\r
64 \r
65 /* Scheduler include files. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 #include "semphr.h"\r
69 \r
70 /* Demo app include files. */\r
71 #include "recmutex.h"\r
72 \r
73 /* Priorities assigned to the three tasks. */\r
74 #define recmuCONTROLLING_TASK_PRIORITY  ( tskIDLE_PRIORITY + 2 )\r
75 #define recmuBLOCKING_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
76 #define recmuPOLLING_TASK_PRIORITY              ( tskIDLE_PRIORITY + 0 )\r
77 \r
78 /* In this version the tick period is very long, so the short delay cannot be\r
79 for too many ticks, or the check task will execute and find that the recmutex\r
80 tasks have not completed their functionality and then signal an error.  The\r
81 delay does however have to be long enough to allow the lower priority tasks\r
82 a chance of executing - this is basically achieved by reducing the number\r
83 of times the loop that takes/gives the recursive mutex executes. */\r
84 #define recmuMAX_COUNT                                  ( 2 )\r
85 #define recmuSHORT_DELAY                                ( 20 )\r
86 #define recmuNO_DELAY                                   ( ( TickType_t ) 0 )\r
87 #define recmuFIVE_TICK_DELAY                    ( ( TickType_t ) 5 )\r
88 \r
89 /* The three tasks as described at the top of this file. */\r
90 static void prvRecursiveMutexControllingTask( void *pvParameters );\r
91 static void prvRecursiveMutexBlockingTask( void *pvParameters );\r
92 static void prvRecursiveMutexPollingTask( void *pvParameters );\r
93 \r
94 /* The mutex used by the demo. */\r
95 static SemaphoreHandle_t xMutex;\r
96 \r
97 /* Variables used to detect and latch errors. */\r
98 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;\r
99 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles = 0, uxPollingCycles = 0;\r
100 \r
101 /* Handles of the two higher priority tasks, required so they can be resumed \r
102 (unsuspended). */\r
103 static TaskHandle_t xControllingTaskHandle, xBlockingTaskHandle, xPollingTaskHandle;\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vStartRecursiveMutexTasks( void )\r
108 {\r
109         /* Just creates the mutex and the three tasks. */\r
110 \r
111         xMutex = xSemaphoreCreateRecursiveMutex();\r
112 \r
113         /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
114         in use.  The registry is provided as a means for kernel aware \r
115         debuggers to locate mutex and has no purpose if a kernel aware debugger\r
116         is not being used.  The call to vQueueAddToRegistry() will be removed\r
117         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
118         defined to be less than 1. */\r
119         vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Recursive_Mutex" );\r
120 \r
121 \r
122         if( xMutex != NULL )\r
123         {\r
124                 xTaskCreate( prvRecursiveMutexControllingTask, "Rec1Ctrl", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );\r
125         xTaskCreate( prvRecursiveMutexBlockingTask, "Rec2Blck", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );\r
126         xTaskCreate( prvRecursiveMutexPollingTask, "Rec3Poll", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, &xPollingTaskHandle );\r
127         }\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 static void prvRecursiveMutexControllingTask( void *pvParameters )\r
132 {\r
133 unsigned portBASE_TYPE ux;\r
134 \r
135         /* Just to remove compiler warning. */\r
136         ( void ) pvParameters;\r
137 \r
138         for( ;; )\r
139         {\r
140                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'\r
141                 it.   The first time through, the mutex will not have been used yet,\r
142                 subsequent times through, at this point the mutex will be held by the\r
143                 polling task. */\r
144                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
145                 {\r
146                         xErrorOccurred = pdTRUE;\r
147                 }\r
148 \r
149                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
150                 {\r
151                         /* We should now be able to take the mutex as many times as\r
152                         we like.\r
153                         \r
154                         The first time through the mutex will be immediately available, on\r
155                         subsequent times through the mutex will be held by the polling task\r
156                         at this point and this Take will cause the polling task to inherit\r
157                         the priority of this task.  In this case the block time must be\r
158                         long enough to ensure the polling task will execute again before the\r
159                         block time expires.  If the block time does expire then the error\r
160                         flag will be set here. */\r
161                         if( xSemaphoreTakeRecursive( xMutex, recmuFIVE_TICK_DELAY ) != pdPASS )\r
162                         {\r
163                                 xErrorOccurred = pdTRUE;\r
164                         }\r
165 \r
166                         /* Ensure the other task attempting to access the mutex (and the\r
167                         other demo tasks) are able to execute to ensure they either block\r
168                         (where a block time is specified) or return an error (where no \r
169                         block time is specified) as the mutex is held by this task. */\r
170                         vTaskDelay( recmuSHORT_DELAY );\r
171                 }\r
172 \r
173                 /* For each time we took the mutex, give it back. */\r
174                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )\r
175                 {\r
176                         /* Ensure the other task attempting to access the mutex (and the\r
177                         other demo tasks) are able to execute. */\r
178                         vTaskDelay( recmuSHORT_DELAY );\r
179 \r
180                         /* We should now be able to give the mutex as many times as we\r
181                         took it.  When the mutex is available again the Blocking task\r
182                         should be unblocked but not run because it has a lower priority\r
183                         than this task.  The polling task should also not run at this point\r
184                         as it too has a lower priority than this task. */\r
185                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
186                         {\r
187                                 xErrorOccurred = pdTRUE;\r
188                         }\r
189 \r
190                         #if configUSE_PREEMPTION == 0\r
191                                 taskYIELD();\r
192                         #endif\r
193                 }\r
194 \r
195                 /* Having given it back the same number of times as it was taken, we\r
196                 should no longer be the mutex owner, so the next give should fail. */\r
197                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )\r
198                 {\r
199                         xErrorOccurred = pdTRUE;\r
200                 }\r
201 \r
202                 /* Keep count of the number of cycles this task has performed so a \r
203                 stall can be detected. */\r
204                 uxControllingCycles++;\r
205 \r
206                 /* Suspend ourselves so the blocking task can execute. */\r
207                 xControllingIsSuspended = pdTRUE;\r
208                 vTaskSuspend( NULL );\r
209                 xControllingIsSuspended = pdFALSE;\r
210         }\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 static void prvRecursiveMutexBlockingTask( void *pvParameters )\r
215 {\r
216         /* Just to remove compiler warning. */\r
217         ( void ) pvParameters;\r
218 \r
219         for( ;; )\r
220         {\r
221                 /* This task will run while the controlling task is blocked, and the\r
222                 controlling task will block only once it has the mutex - therefore\r
223                 this call should block until the controlling task has given up the \r
224                 mutex, and not actually execute past this call until the controlling \r
225                 task is suspended. */\r
226                 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == 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.  We should only obtain it 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                         /* Is the blocking task suspended? */\r
281                         if( ( xBlockingIsSuspended != pdTRUE ) || ( xControllingIsSuspended != pdTRUE ) )\r
282                         {\r
283                                 xErrorOccurred = pdTRUE;\r
284                         }\r
285                         else\r
286                         {\r
287                                 /* Keep count of the number of cycles this task has performed \r
288                                 so a stall can be detected. */\r
289                                 uxPollingCycles++;\r
290 \r
291                                 /* We can resume the other tasks here even though they have a\r
292                                 higher priority than the polling task.  When they execute they\r
293                                 will attempt to obtain the mutex but fail because the polling\r
294                                 task is still the mutex holder.  The polling task (this task)\r
295                                 will then inherit the higher priority.  The Blocking task will\r
296                                 block indefinitely when it attempts to obtain the mutex, the\r
297                                 Controlling task will only block for a fixed period and an\r
298                                 error will be latched if the polling task has not returned the\r
299                                 mutex by the time this fixed period has expired. */                             \r
300                                 vTaskResume( xBlockingTaskHandle );\r
301                                 #if configUSE_PREEMPTION == 0\r
302                                         taskYIELD();\r
303                                 #endif\r
304 \r
305                                 vTaskResume( xControllingTaskHandle );\r
306                                 #if configUSE_PREEMPTION == 0\r
307                                         taskYIELD();\r
308                                 #endif\r
309 \r
310                                 /* The other two tasks should now have executed and no longer\r
311                                 be suspended. */\r
312                                 if( ( xBlockingIsSuspended == pdTRUE ) || ( xControllingIsSuspended == pdTRUE ) )\r
313                                 {\r
314                                         xErrorOccurred = pdTRUE;\r
315                                 }                               \r
316                         \r
317                                 /* Release the mutex, disinheriting the higher priority again. */\r
318                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )\r
319                                 {\r
320                                         xErrorOccurred = pdTRUE;\r
321                                 }\r
322 \r
323                                 #if configUSE_PREEMPTION == 0\r
324                                         taskYIELD();\r
325                                 #endif\r
326                         }\r
327                 }\r
328 \r
329                 #if configUSE_PREEMPTION == 0\r
330                 {\r
331                         taskYIELD();\r
332                 }\r
333                 #endif\r
334         }\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 /* This is called to check that all the created tasks are still running. */\r
339 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )\r
340 {\r
341 portBASE_TYPE xReturn;\r
342 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;\r
343 \r
344         /* Is the controlling task still cycling? */\r
345         if( uxLastControllingCycles == uxControllingCycles )\r
346         {\r
347                 xErrorOccurred = pdTRUE;\r
348         }\r
349         else\r
350         {\r
351                 uxLastControllingCycles = uxControllingCycles;\r
352         }\r
353 \r
354         /* Is the blocking task still cycling? */\r
355         if( uxLastBlockingCycles == uxBlockingCycles )\r
356         {\r
357                 xErrorOccurred = pdTRUE;\r
358         }\r
359         else\r
360         {\r
361                 uxLastBlockingCycles = uxBlockingCycles;\r
362         }\r
363 \r
364         /* Is the polling task still cycling? */\r
365         if( uxLastPollingCycles == uxPollingCycles )\r
366         {\r
367                 xErrorOccurred = pdTRUE;\r
368         }\r
369         else\r
370         {\r
371                 uxLastPollingCycles = uxPollingCycles;\r
372         }\r
373 \r
374         if( xErrorOccurred == pdTRUE )\r
375         {\r
376                 xReturn = pdFAIL;\r
377         }\r
378         else\r
379         {\r
380                 xReturn = pdTRUE;\r
381         }\r
382 \r
383         return xReturn;\r
384 }\r
385 \r
386 \r
387 \r
388 \r