2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
31 * to which is guarded by a semaphore.
\r
33 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
34 * semaphore a task checks to ensure that the guarded variable has an expected
\r
35 * value. It then clears the variable to zero before counting it back up to the
\r
36 * expected value in increments of 1. After each increment the variable is checked
\r
37 * to ensure it contains the value to which it was just set. When the starting
\r
38 * value is again reached the task releases the semaphore giving the other task in
\r
39 * the set a chance to do exactly the same thing. The starting value is high
\r
40 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
42 * An error is flagged if at any time during the process a shared variable is
\r
43 * found to have a value other than that expected. Such an occurrence would
\r
44 * suggest an error in the mutual exclusion mechanism by which access to the
\r
45 * variable is restricted.
\r
47 * The first set of two tasks poll their semaphore. The second set use blocking
\r
50 * \page SemTestC semtest.c
\r
51 * \ingroup DemoFiles
\r
56 Changes from V1.2.0:
\r
58 + The tasks that operate at the idle priority now use a lower expected
\r
59 count than those running at a higher priority. This prevents the low
\r
60 priority tasks from signaling an error because they have not been
\r
61 scheduled enough time for each of them to count the shared variable to
\r
66 + Delay periods are now specified using variables and constants of
\r
67 TickType_t rather than unsigned long.
\r
71 + The stack size now uses configMINIMAL_STACK_SIZE.
\r
72 + String constants made file scope to decrease stack depth on 8051 port.
\r
77 /* Scheduler include files. */
\r
78 #include "FreeRTOS.h"
\r
82 /* Demo app include files. */
\r
83 #include "semtest.h"
\r
86 /* The value to which the shared variables are counted. */
\r
87 #define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xfff )
\r
88 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned long ) 0xff )
\r
90 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
92 #define semtstNUM_TASKS ( 4 )
\r
94 #define semtstDELAY_FACTOR ( ( TickType_t ) 10 )
\r
96 /* The task function as described at the top of the file. */
\r
97 static void prvSemaphoreTest( void *pvParameters );
\r
99 /* Structure used to pass parameters to each task. */
\r
100 typedef struct SEMAPHORE_PARAMETERS
\r
102 SemaphoreHandle_t xSemaphore;
\r
103 volatile unsigned long *pulSharedVariable;
\r
104 TickType_t xBlockTime;
\r
105 } xSemaphoreParameters;
\r
107 /* Variables used to check that all the tasks are still running without errors. */
\r
108 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
109 static volatile short sNextCheckVariable = 0;
\r
111 /* Strings to print if USE_STDIO is defined. */
\r
112 const char * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";
\r
113 const char * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";
\r
115 /*-----------------------------------------------------------*/
\r
117 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )
\r
119 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
120 const TickType_t xBlockTime = ( TickType_t ) 100;
\r
122 /* Create the structure used to pass parameters to the first two tasks. */
\r
123 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
125 if( pxFirstSemaphoreParameters != NULL )
\r
127 /* Create the semaphore used by the first two tasks. */
\r
128 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );
\r
130 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
132 /* Create the variable which is to be shared by the first two tasks. */
\r
133 pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
135 /* Initialise the share variable to the value the tasks expect. */
\r
136 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
138 /* The first two tasks do not block on semaphore calls. */
\r
139 pxFirstSemaphoreParameters->xBlockTime = ( TickType_t ) 0;
\r
141 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
142 xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
143 xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
147 /* Do exactly the same to create the second set of tasks, only this time
\r
148 provide a block time for the semaphore calls. */
\r
149 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
150 if( pxSecondSemaphoreParameters != NULL )
\r
152 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );
\r
154 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
156 pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );
\r
157 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
158 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;
\r
160 xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );
\r
161 xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );
\r
165 /*-----------------------------------------------------------*/
\r
167 static void prvSemaphoreTest( void *pvParameters )
\r
169 xSemaphoreParameters *pxParameters;
\r
170 volatile unsigned long *pulSharedVariable, ulExpectedValue;
\r
171 unsigned long ulCounter;
\r
172 short sError = pdFALSE, sCheckVariableToUse;
\r
174 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
176 portENTER_CRITICAL();
\r
177 sCheckVariableToUse = sNextCheckVariable;
\r
178 sNextCheckVariable++;
\r
179 portEXIT_CRITICAL();
\r
181 /* Queue a message for printing to say the task has started. */
\r
182 vPrintDisplayMessage( &pcSemaphoreTaskStart );
\r
184 /* A structure is passed in as the parameter. This contains the shared
\r
185 variable being guarded. */
\r
186 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
187 pulSharedVariable = pxParameters->pulSharedVariable;
\r
189 /* If we are blocking we use a much higher count to ensure loads of context
\r
190 switches occur during the count. */
\r
191 if( pxParameters->xBlockTime > ( TickType_t ) 0 )
\r
193 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
197 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
202 /* Try to obtain the semaphore. */
\r
203 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
205 /* We have the semaphore and so expect any other tasks using the
\r
206 shared variable to have left it in the state we expect to find
\r
208 if( *pulSharedVariable != ulExpectedValue )
\r
210 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
214 /* Clear the variable, then count it back up to the expected value
\r
215 before releasing the semaphore. Would expect a context switch or
\r
216 two during this time. */
\r
217 for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
219 *pulSharedVariable = ulCounter;
\r
220 if( *pulSharedVariable != ulCounter )
\r
222 if( sError == pdFALSE )
\r
224 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
230 /* Release the semaphore, and if no errors have occurred increment the check
\r
232 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
234 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );
\r
238 if( sError == pdFALSE )
\r
240 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
242 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
246 /* If we have a block time then we are running at a priority higher
\r
247 than the idle priority. This task takes a long time to complete
\r
248 a cycle (deliberately so to test the guarding) so will be starving
\r
249 out lower priority tasks. Block for some time to allow give lower
\r
250 priority tasks some processor time. */
\r
251 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
255 if( pxParameters->xBlockTime == ( TickType_t ) 0 )
\r
257 /* We have not got the semaphore yet, so no point using the
\r
258 processor. We are not blocking when attempting to obtain the
\r
265 /*-----------------------------------------------------------*/
\r
267 /* This is called to check that all the created tasks are still running. */
\r
268 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )
\r
270 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
271 portBASE_TYPE xTask, xReturn = pdTRUE;
\r
273 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
275 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
280 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r