root/webserver/example/freeRTOSexample/Demo/Common/Minimal/recmutex.c

Revision 14, 12.3 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

Line 
1 /*
2         FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.
3
4         This file is part of the FreeRTOS distribution.
5
6         FreeRTOS is free software; you can redistribute it and/or modify it     under
7         the terms of the GNU General Public License (version 2) as published by the
8         Free Software Foundation and modified by the FreeRTOS exception.
9         **NOTE** The exception to the GPL is included to allow you to distribute a
10         combined work that includes FreeRTOS without being obliged to provide the
11         source code for proprietary components outside of the FreeRTOS kernel. 
12         Alternative commercial license and support terms are also available upon
13         request.  See the licensing section of http://www.FreeRTOS.org for full
14         license details.
15
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19         more details.
20
21         You should have received a copy of the GNU General Public License along
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.
24
25
26         ***************************************************************************
27         *                                                                         *
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *
29         * See http://www.FreeRTOS.org/Documentation for details                   *
30         *                                                                         *
31         ***************************************************************************
32
33         1 tab == 4 spaces!
34
35         Please ensure to read the configuration and relevant port sections of the
36         online documentation.
37
38         http://www.FreeRTOS.org - Documentation, latest information, license and
39         contact details.
40
41         http://www.SafeRTOS.com - A version that is certified for use in safety
42         critical systems.
43
44         http://www.OpenRTOS.com - Commercial support, development, porting,
45         licensing and training services.
46 */
47
48 /*
49         The tasks defined on this page demonstrate the use of recursive mutexes.
50
51         For recursive mutex functionality the created mutex should be created using
52         xSemaphoreCreateRecursiveMutex(), then be manipulated
53         using the xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() API
54         functions.
55
56         This demo creates three tasks all of which access the same recursive mutex:
57
58         prvRecursiveMutexControllingTask() has the highest priority so executes
59         first and grabs the mutex.  It then performs some recursive accesses -
60         between each of which it sleeps for a short period to let the lower
61         priority tasks execute.  When it has completed its demo functionality
62         it gives the mutex back before suspending itself.
63
64         prvRecursiveMutexBlockingTask() attempts to access the mutex by performing
65         a blocking 'take'.  The blocking task has a lower priority than the
66         controlling     task so by the time it executes the mutex has already been
67         taken by the controlling task,  causing the blocking task to block.  It
68         does not unblock until the controlling task has given the mutex back,
69         and it does not actually run until the controlling task has suspended
70         itself (due to the relative priorities).  When it eventually does obtain
71         the mutex all it does is give the mutex back prior to also suspending
72         itself.  At this point both the controlling task and the blocking task are
73         suspended.
74
75         prvRecursiveMutexPollingTask() runs at the idle priority.  It spins round
76         a tight loop attempting to obtain the mutex with a non-blocking call.  As
77         the lowest priority task it will not successfully obtain the mutex until
78         both the controlling and blocking tasks are suspended.  Once it eventually
79         does obtain the mutex it first unsuspends both the controlling task and
80         blocking task prior to giving the mutex back - resulting in the polling
81         task temporarily inheriting the controlling tasks priority.
82 */
83
84 /* Scheduler include files. */
85 #include "FreeRTOS.h"
86 #include "task.h"
87 #include "semphr.h"
88
89 /* Demo app include files. */
90 #include "recmutex.h"
91
92 /* Priorities assigned to the three tasks. */
93 #define recmuCONTROLLING_TASK_PRIORITY  ( tskIDLE_PRIORITY + 2 )
94 #define recmuBLOCKING_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )
95 #define recmuPOLLING_TASK_PRIORITY              ( tskIDLE_PRIORITY + 0 )
96
97 /* The recursive call depth. */
98 #define recmuMAX_COUNT                                  ( 10 )
99
100 /* Misc. */
101 #define recmuSHORT_DELAY                                ( 20 / portTICK_RATE_MS )
102 #define recmuNO_DELAY                                   ( ( portTickType ) 0 )
103 #define recmuTWO_TICK_DELAY                             ( ( portTickType ) 2 )
104
105 /* The three tasks as described at the top of this file. */
106 static void prvRecursiveMutexControllingTask( void *pvParameters );
107 static void prvRecursiveMutexBlockingTask( void *pvParameters );
108 static void prvRecursiveMutexPollingTask( void *pvParameters );
109
110 /* The mutex used by the demo. */
111 static xSemaphoreHandle xMutex;
112
113 /* Variables used to detect and latch errors. */
114 static volatile portBASE_TYPE xErrorOccurred = pdFALSE, xControllingIsSuspended = pdFALSE, xBlockingIsSuspended = pdFALSE;
115 static volatile unsigned portBASE_TYPE uxControllingCycles = 0, uxBlockingCycles, uxPollingCycles = 0;
116
117 /* Handles of the two higher priority tasks, required so they can be resumed
118 (unsuspended). */
119 static xTaskHandle xControllingTaskHandle, xBlockingTaskHandle;
120
121 /*-----------------------------------------------------------*/
122
123 void vStartRecursiveMutexTasks( void )
124 {
125         /* Just creates the mutex and the three tasks. */
126
127         xMutex = xSemaphoreCreateRecursiveMutex();
128
129         /* vQueueAddToRegistry() adds the mutex to the registry, if one is
130         in use.  The registry is provided as a means for kernel aware
131         debuggers to locate mutex and has no purpose if a kernel aware debugger
132         is not being used.  The call to vQueueAddToRegistry() will be removed
133         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
134         defined to be less than 1. */
135         vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Recursive_Mutex" );
136
137
138         if( xMutex != NULL )
139         {
140                 xTaskCreate( prvRecursiveMutexControllingTask, ( signed portCHAR * ) "Rec1", configMINIMAL_STACK_SIZE, NULL, recmuCONTROLLING_TASK_PRIORITY, &xControllingTaskHandle );
141         xTaskCreate( prvRecursiveMutexBlockingTask, ( signed portCHAR * ) "Rec2", configMINIMAL_STACK_SIZE, NULL, recmuBLOCKING_TASK_PRIORITY, &xBlockingTaskHandle );
142         xTaskCreate( prvRecursiveMutexPollingTask, ( signed portCHAR * ) "Rec3", configMINIMAL_STACK_SIZE, NULL, recmuPOLLING_TASK_PRIORITY, NULL );
143         }
144 }
145 /*-----------------------------------------------------------*/
146
147 static void prvRecursiveMutexControllingTask( void *pvParameters )
148 {
149 unsigned portBASE_TYPE ux;
150
151         /* Just to remove compiler warning. */
152         ( void ) pvParameters;
153
154         for( ;; )
155         {
156                 /* Should not be able to 'give' the mutex, as we have not yet 'taken'
157                 it. */
158                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
159                 {
160                         xErrorOccurred = pdTRUE;
161                 }
162
163                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
164                 {
165                         /* We should now be able to take the mutex as many times as
166                         we like.  A one tick delay is used so the polling task will
167                         inherit our priority on all but the first cycle of this task.
168                         If we did not block attempting to receive the mutex then no
169                         priority inheritance would occur. */
170                         if( xSemaphoreTakeRecursive( xMutex, recmuTWO_TICK_DELAY ) != pdPASS )
171                         {
172                                 xErrorOccurred = pdTRUE;
173                         }
174
175                         /* Ensure the other task attempting to access the mutex (and the
176                         other demo tasks) are able to execute. */
177                         vTaskDelay( recmuSHORT_DELAY );
178                 }
179
180                 /* For each time we took the mutex, give it back. */
181                 for( ux = 0; ux < recmuMAX_COUNT; ux++ )
182                 {
183                         /* Ensure the other task attempting to access the mutex (and the
184                         other demo tasks) are able to execute. */
185                         vTaskDelay( recmuSHORT_DELAY );
186
187                         /* We should now be able to give the mutex as many times as we
188                         took it. */
189                         if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
190                         {
191                                 xErrorOccurred = pdTRUE;
192                         }
193                 }
194
195                 /* Having given it back the same number of times as it was taken, we
196                 should no longer be the mutex owner, so the next give sh ould fail. */
197                 if( xSemaphoreGiveRecursive( xMutex ) == pdPASS )
198                 {
199                         xErrorOccurred = pdTRUE;
200                 }
201
202                 /* Keep count of the number of cycles this task has performed so a
203                 stall can be detected. */
204                 uxControllingCycles++;
205
206                 /* Suspend ourselves to the blocking task can execute. */
207                 xControllingIsSuspended = pdTRUE;
208                 vTaskSuspend( NULL );
209                 xControllingIsSuspended = pdFALSE;
210         }
211 }
212 /*-----------------------------------------------------------*/
213
214 static void prvRecursiveMutexBlockingTask( void *pvParameters )
215 {
216         /* Just to remove compiler warning. */
217         ( void ) pvParameters;
218
219         for( ;; )
220         {
221                 /* Attempt to obtain the mutex.  We should block until the
222                 controlling task has given up the mutex, and not actually execute
223                 past this call until the controlling task is suspended. */
224                 if( xSemaphoreTakeRecursive( xMutex, portMAX_DELAY ) == pdPASS )
225                 {
226                         if( xControllingIsSuspended != pdTRUE )
227                         {
228                                 /* Did not expect to execute until the controlling task was
229                                 suspended. */
230                                 xErrorOccurred = pdTRUE;
231                         }
232                         else
233                         {
234                                 /* Give the mutex back before suspending ourselves to allow
235                                 the polling task to obtain the mutex. */
236                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
237                                 {
238                                         xErrorOccurred = pdTRUE;
239                                 }
240
241                                 xBlockingIsSuspended = pdTRUE;
242                                 vTaskSuspend( NULL );
243                                 xBlockingIsSuspended = pdFALSE;
244                         }
245                 }
246                 else
247                 {
248                         /* We should not leave the xSemaphoreTakeRecursive() function
249                         until the mutex was obtained. */
250                         xErrorOccurred = pdTRUE;
251                 }
252
253                 /* The controlling and blocking tasks should be in lock step. */
254                 if( uxControllingCycles != ( uxBlockingCycles + 1 ) )
255                 {
256                         xErrorOccurred = pdTRUE;
257                 }
258
259                 /* Keep count of the number of cycles this task has performed so a
260                 stall can be detected. */
261                 uxBlockingCycles++;
262         }
263 }
264 /*-----------------------------------------------------------*/
265
266 static void prvRecursiveMutexPollingTask( void *pvParameters )
267 {
268         /* Just to remove compiler warning. */
269         ( void ) pvParameters;
270
271         for( ;; )
272         {
273                 /* Keep attempting to obtain the mutex.  We should only obtain it when
274                 the blocking task has suspended itself. */
275                 if( xSemaphoreTakeRecursive( xMutex, recmuNO_DELAY ) == pdPASS )
276                 {
277                         /* Is the blocking task suspended? */
278                         if( xBlockingIsSuspended != pdTRUE )
279                         {
280                                 xErrorOccurred = pdTRUE;
281                         }
282                         else
283                         {
284                                 /* Keep count of the number of cycles this task has performed so
285                                 a stall can be detected. */
286                                 uxPollingCycles++;
287
288                                 /* We can resume the other tasks here even though they have a
289                                 higher priority than the polling task.  When they execute they
290                                 will attempt to obtain the mutex but fail because the polling
291                                 task is still the mutex holder.  The polling task (this task)
292                                 will then inherit the higher priority. */                               
293                                 vTaskResume( xBlockingTaskHandle );
294                 vTaskResume( xControllingTaskHandle );
295                        
296                                 /* Release the mutex, disinheriting the higher priority again. */
297                                 if( xSemaphoreGiveRecursive( xMutex ) != pdPASS )
298                                 {
299                                         xErrorOccurred = pdTRUE;
300                                 }
301                         }
302                 }
303
304                 #if configUSE_PREEMPTION == 0
305                 {
306                         taskYIELD();
307                 }
308                 #endif
309         }
310 }
311 /*-----------------------------------------------------------*/
312
313 /* This is called to check that all the created tasks are still running. */
314 portBASE_TYPE xAreRecursiveMutexTasksStillRunning( void )
315 {
316 portBASE_TYPE xReturn;
317 static unsigned portBASE_TYPE uxLastControllingCycles = 0, uxLastBlockingCycles = 0, uxLastPollingCycles = 0;
318
319         /* Is the controlling task still cycling? */
320         if( uxLastControllingCycles == uxControllingCycles )
321         {
322                 xErrorOccurred = pdTRUE;
323         }
324         else
325         {
326                 uxLastControllingCycles = uxControllingCycles;
327         }
328
329         /* Is the blocking task still cycling? */
330         if( uxLastBlockingCycles == uxBlockingCycles )
331         {
332                 xErrorOccurred = pdTRUE;
333         }
334         else
335         {
336                 uxLastBlockingCycles = uxBlockingCycles;
337         }
338
339         /* Is the polling task still cycling? */
340         if( uxLastPollingCycles == uxPollingCycles )
341         {
342                 xErrorOccurred = pdTRUE;
343         }
344         else
345         {
346                 uxLastPollingCycles = uxPollingCycles;
347         }
348
349         if( xErrorOccurred == pdTRUE )
350         {
351                 xReturn = pdFAIL;
352         }
353         else
354         {
355                 xReturn = pdTRUE;
356         }
357
358         return xReturn;
359 }
360
361
362
363
Note: See TracBrowser for help on using the browser.