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