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