2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
30 * to which is guarded by a semaphore.
\r
32 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
33 * semaphore a task checks to ensure that the guarded variable has an expected
\r
34 * value. It then clears the variable to zero before counting it back up to the
\r
35 * expected value in increments of 1. After each increment the variable is checked
\r
36 * to ensure it contains the value to which it was just set. When the starting
\r
37 * value is again reached the task releases the semaphore giving the other task in
\r
38 * the set a chance to do exactly the same thing. The starting value is high
\r
39 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
41 * An error is flagged if at any time during the process a shared variable is
\r
42 * found to have a value other than that expected. Such an occurrence would
\r
43 * suggest an error in the mutual exclusion mechanism by which access to the
\r
44 * variable is restricted.
\r
46 * The first set of two tasks poll their semaphore. The second set use blocking
\r
54 /* Scheduler include files. */
\r
55 #include "FreeRTOS.h"
\r
59 /* Demo app include files. */
\r
60 #include "semtest.h"
\r
62 /* The value to which the shared variables are counted. */
\r
63 #define semtstBLOCKING_EXPECTED_VALUE ( ( uint32_t ) 0xfff )
\r
64 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( uint32_t ) 0xff )
\r
66 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
68 #define semtstNUM_TASKS ( 4 )
\r
70 #define semtstDELAY_FACTOR ( ( TickType_t ) 10 )
\r
72 /* The task function as described at the top of the file. */
\r
73 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );
\r
75 /* Structure used to pass parameters to each task. */
\r
76 typedef struct SEMAPHORE_PARAMETERS
\r
78 SemaphoreHandle_t xSemaphore;
\r
79 volatile uint32_t *pulSharedVariable;
\r
80 TickType_t xBlockTime;
\r
81 } xSemaphoreParameters;
\r
83 /* Variables used to check that all the tasks are still running without errors. */
\r
84 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
85 static volatile short sNextCheckVariable = 0;
\r
87 /*-----------------------------------------------------------*/
\r
89 void vStartSemaphoreTasks( UBaseType_t uxPriority )
\r
91 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
92 const TickType_t xBlockTime = ( TickType_t ) 100;
\r
94 /* Create the structure used to pass parameters to the first two tasks. */
\r
95 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
97 if( pxFirstSemaphoreParameters != NULL )
\r
99 /* Create the semaphore used by the first two tasks. */
\r
100 pxFirstSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();
\r
102 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
104 xSemaphoreGive( pxFirstSemaphoreParameters->xSemaphore );
\r
106 /* Create the variable which is to be shared by the first two tasks. */
\r
107 pxFirstSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );
\r
109 /* Initialise the share variable to the value the tasks expect. */
\r
110 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
112 /* The first two tasks do not block on semaphore calls. */
\r
113 pxFirstSemaphoreParameters->xBlockTime = ( TickType_t ) 0;
\r
115 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
116 xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
117 xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
119 /* vQueueAddToRegistry() adds the semaphore to the registry, if one
\r
120 is in use. The registry is provided as a means for kernel aware
\r
121 debuggers to locate semaphores and has no purpose if a kernel aware
\r
122 debugger is not being used. The call to vQueueAddToRegistry() will
\r
123 be removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
\r
124 defined or is defined to be less than 1. */
\r
125 vQueueAddToRegistry( ( QueueHandle_t ) pxFirstSemaphoreParameters->xSemaphore, "Counting_Sem_1" );
\r
129 /* Do exactly the same to create the second set of tasks, only this time
\r
130 provide a block time for the semaphore calls. */
\r
131 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
132 if( pxSecondSemaphoreParameters != NULL )
\r
134 pxSecondSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();
\r
136 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
138 xSemaphoreGive( pxSecondSemaphoreParameters->xSemaphore );
\r
140 pxSecondSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );
\r
141 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
142 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;
\r
144 xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );
\r
145 xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );
\r
147 /* vQueueAddToRegistry() adds the semaphore to the registry, if one
\r
148 is in use. The registry is provided as a means for kernel aware
\r
149 debuggers to locate semaphores and has no purpose if a kernel aware
\r
150 debugger is not being used. The call to vQueueAddToRegistry() will
\r
151 be removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
\r
152 defined or is defined to be less than 1. */
\r
153 vQueueAddToRegistry( ( QueueHandle_t ) pxSecondSemaphoreParameters->xSemaphore, "Counting_Sem_2" );
\r
157 /*-----------------------------------------------------------*/
\r
159 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
\r
161 xSemaphoreParameters *pxParameters;
\r
162 volatile uint32_t *pulSharedVariable, ulExpectedValue;
\r
163 uint32_t ulCounter;
\r
164 short sError = pdFALSE, sCheckVariableToUse;
\r
166 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
168 portENTER_CRITICAL();
\r
169 sCheckVariableToUse = sNextCheckVariable;
\r
170 sNextCheckVariable++;
\r
171 portEXIT_CRITICAL();
\r
173 /* A structure is passed in as the parameter. This contains the shared
\r
174 variable being guarded. */
\r
175 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
176 pulSharedVariable = pxParameters->pulSharedVariable;
\r
178 /* If we are blocking we use a much higher count to ensure loads of context
\r
179 switches occur during the count. */
\r
180 if( pxParameters->xBlockTime > ( TickType_t ) 0 )
\r
182 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
186 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
191 /* Try to obtain the semaphore. */
\r
192 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
194 /* We have the semaphore and so expect any other tasks using the
\r
195 shared variable to have left it in the state we expect to find
\r
197 if( *pulSharedVariable != ulExpectedValue )
\r
202 /* Clear the variable, then count it back up to the expected value
\r
203 before releasing the semaphore. Would expect a context switch or
\r
204 two during this time. */
\r
205 for( ulCounter = ( uint32_t ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
207 *pulSharedVariable = ulCounter;
\r
208 if( *pulSharedVariable != ulCounter )
\r
214 /* Release the semaphore, and if no errors have occurred increment the check
\r
216 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
221 if( sError == pdFALSE )
\r
223 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
225 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
229 /* If we have a block time then we are running at a priority higher
\r
230 than the idle priority. This task takes a long time to complete
\r
231 a cycle (deliberately so to test the guarding) so will be starving
\r
232 out lower priority tasks. Block for some time to allow give lower
\r
233 priority tasks some processor time. */
\r
234 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
238 if( pxParameters->xBlockTime == ( TickType_t ) 0 )
\r
240 /* We have not got the semaphore yet, so no point using the
\r
241 processor. We are not blocking when attempting to obtain the
\r
248 /*-----------------------------------------------------------*/
\r
250 /* This is called to check that all the created tasks are still running. */
\r
251 BaseType_t xAreSemaphoreTasksStillRunning( void )
\r
253 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
254 BaseType_t xTask, xReturn = pdTRUE;
\r
256 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
258 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
263 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r