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