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