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