2 FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
50 * to which is guarded by a semaphore.
\r
52 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
53 * semaphore a task checks to ensure that the guarded variable has an expected
\r
54 * value. It then clears the variable to zero before counting it back up to the
\r
55 * expected value in increments of 1. After each increment the variable is checked
\r
56 * to ensure it contains the value to which it was just set. When the starting
\r
57 * value is again reached the task releases the semaphore giving the other task in
\r
58 * the set a chance to do exactly the same thing. The starting value is high
\r
59 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
61 * An error is flagged if at any time during the process a shared variable is
\r
62 * found to have a value other than that expected. Such an occurrence would
\r
63 * suggest an error in the mutual exclusion mechanism by which access to the
\r
64 * variable is restricted.
\r
66 * The first set of two tasks poll their semaphore. The second set use blocking
\r
74 /* Scheduler include files. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Demo app include files. */
\r
80 #include "semtest.h"
\r
82 /* The value to which the shared variables are counted. */
\r
83 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xfff )
\r
84 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xff )
\r
86 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
88 #define semtstNUM_TASKS ( 4 )
\r
90 #define semtstDELAY_FACTOR ( ( portTickType ) 10 )
\r
92 /* The task function as described at the top of the file. */
\r
93 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );
\r
95 /* Structure used to pass parameters to each task. */
\r
96 typedef struct SEMAPHORE_PARAMETERS
\r
98 xSemaphoreHandle xSemaphore;
\r
99 volatile unsigned portLONG *pulSharedVariable;
\r
100 portTickType xBlockTime;
\r
101 } xSemaphoreParameters;
\r
103 /* Variables used to check that all the tasks are still running without errors. */
\r
104 static volatile portSHORT sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
105 static volatile portSHORT sNextCheckVariable = 0;
\r
107 /*-----------------------------------------------------------*/
\r
109 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
111 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
112 const portTickType xBlockTime = ( portTickType ) 100;
\r
114 /* Create the structure used to pass parameters to the first two tasks. */
\r
115 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
117 if( pxFirstSemaphoreParameters != NULL )
\r
119 /* Create the semaphore used by the first two tasks. */
\r
120 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
122 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
124 /* Create the variable which is to be shared by the first two tasks. */
\r
125 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
127 /* Initialise the share variable to the value the tasks expect. */
\r
128 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
130 /* The first two tasks do not block on semaphore calls. */
\r
131 pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;
\r
133 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
134 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
135 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );
\r
139 /* Do exactly the same to create the second set of tasks, only this time
\r
140 provide a block time for the semaphore calls. */
\r
141 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
142 if( pxSecondSemaphoreParameters != NULL )
\r
144 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
146 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
148 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );
\r
149 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
150 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;
\r
152 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
153 xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );
\r
157 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
158 in use. The registry is provided as a means for kernel aware
\r
159 debuggers to locate semaphores and has no purpose if a kernel aware debugger
\r
160 is not being used. The call to vQueueAddToRegistry() will be removed
\r
161 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
162 defined to be less than 1. */
\r
163 vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );
\r
164 vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );
\r
166 /*-----------------------------------------------------------*/
\r
168 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
\r
170 xSemaphoreParameters *pxParameters;
\r
171 volatile unsigned portLONG *pulSharedVariable, ulExpectedValue;
\r
172 unsigned portLONG ulCounter;
\r
173 portSHORT sError = pdFALSE, sCheckVariableToUse;
\r
175 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
177 portENTER_CRITICAL();
\r
178 sCheckVariableToUse = sNextCheckVariable;
\r
179 sNextCheckVariable++;
\r
180 portEXIT_CRITICAL();
\r
182 /* A structure is passed in as the parameter. This contains the shared
\r
183 variable being guarded. */
\r
184 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
185 pulSharedVariable = pxParameters->pulSharedVariable;
\r
187 /* If we are blocking we use a much higher count to ensure loads of context
\r
188 switches occur during the count. */
\r
189 if( pxParameters->xBlockTime > ( portTickType ) 0 )
\r
191 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
195 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
200 /* Try to obtain the semaphore. */
\r
201 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
203 /* We have the semaphore and so expect any other tasks using the
\r
204 shared variable to have left it in the state we expect to find
\r
206 if( *pulSharedVariable != ulExpectedValue )
\r
211 /* Clear the variable, then count it back up to the expected value
\r
212 before releasing the semaphore. Would expect a context switch or
\r
213 two during this time. */
\r
214 for( ulCounter = ( unsigned portLONG ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
216 *pulSharedVariable = ulCounter;
\r
217 if( *pulSharedVariable != ulCounter )
\r
223 /* Release the semaphore, and if no errors have occurred increment the check
\r
225 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
230 if( sError == pdFALSE )
\r
232 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
234 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
238 /* If we have a block time then we are running at a priority higher
\r
239 than the idle priority. This task takes a long time to complete
\r
240 a cycle (deliberately so to test the guarding) so will be starving
\r
241 out lower priority tasks. Block for some time to allow give lower
\r
242 priority tasks some processor time. */
\r
243 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
247 if( pxParameters->xBlockTime == ( portTickType ) 0 )
\r
249 /* We have not got the semaphore yet, so no point using the
\r
250 processor. We are not blocking when attempting to obtain the
\r
257 /*-----------------------------------------------------------*/
\r
259 /* This is called to check that all the created tasks are still running. */
\r
260 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
262 static portSHORT sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
263 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
265 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
267 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
272 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r