OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [WIN32-MSVC/] [DemosModifiedForLowTickRate/] [recmutex.c] - Blame information for rev 866

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.