3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
30 * Simple demonstration of the usage of counting semaphore.
33 /* Scheduler include files. */
38 /* Demo program include files. */
41 /* The maximum count value that the semaphore used for the demo can hold. */
42 #define countMAX_COUNT_VALUE ( 200 )
44 /* Constants used to indicate whether or not the semaphore should have been
45 * created with its maximum count value, or its minimum count value. These
46 * numbers are used to ensure that the pointers passed in as the task parameters
48 #define countSTART_AT_MAX_COUNT ( 0xaa )
49 #define countSTART_AT_ZERO ( 0x55 )
51 /* Two tasks are created for the test. One uses a semaphore created with its
52 * count value set to the maximum, and one with the count value set to zero. */
53 #define countNUM_TEST_TASKS ( 2 )
54 #define countDONT_BLOCK ( 0 )
56 /*-----------------------------------------------------------*/
58 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
59 * detected in any of the tasks. */
60 static volatile BaseType_t xErrorDetected = pdFALSE;
62 /*-----------------------------------------------------------*/
65 * The demo task. This simply counts the semaphore up to its maximum value,
66 * the counts it back down again. The result of each semaphore 'give' and
67 * 'take' is inspected, with an error being flagged if it is found not to be
68 * the expected result.
70 static void prvCountingSemaphoreTask( void * pvParameters );
73 * Utility function to increment the semaphore count value up from zero to
74 * countMAX_COUNT_VALUE.
76 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
77 volatile UBaseType_t * puxLoopCounter );
80 * Utility function to decrement the semaphore count value up from
81 * countMAX_COUNT_VALUE to zero.
83 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
84 volatile UBaseType_t * puxLoopCounter );
86 /*-----------------------------------------------------------*/
88 /* The structure that is passed into the task as the task parameter. */
89 typedef struct COUNT_SEM_STRUCT
91 /* The semaphore to be used for the demo. */
92 SemaphoreHandle_t xSemaphore;
94 /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
95 * its count value set to its max count value, or countSTART_AT_ZERO if it
96 * should have been created with its count value set to 0. */
97 UBaseType_t uxExpectedStartCount;
99 /* Incremented on each cycle of the demo task. Used to detect a stalled
101 volatile UBaseType_t uxLoopCounter;
104 /* Two structures are defined, one is passed to each test task. */
105 static xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
107 /*-----------------------------------------------------------*/
109 void vStartCountingSemaphoreTasks( void )
111 /* Create the semaphores that we are going to use for the test/demo. The
112 * first should be created such that it starts at its maximum count value,
113 * the second should be created such that it starts with a count value of zero. */
114 xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
115 xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
116 xParameters[ 0 ].uxLoopCounter = 0;
118 xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
119 xParameters[ 1 ].uxExpectedStartCount = 0;
120 xParameters[ 1 ].uxLoopCounter = 0;
122 /* Were the semaphores created? */
123 if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
125 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
126 * in use. The registry is provided as a means for kernel aware
127 * debuggers to locate semaphores and has no purpose if a kernel aware
128 * debugger is not being used. The call to vQueueAddToRegistry() will be
129 * removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
130 * defined or is defined to be less than 1. */
131 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 0 ].xSemaphore, "Counting_Sem_1" );
132 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 1 ].xSemaphore, "Counting_Sem_2" );
134 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
135 xTaskCreate( prvCountingSemaphoreTask, "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
136 xTaskCreate( prvCountingSemaphoreTask, "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
139 /*-----------------------------------------------------------*/
141 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
142 volatile UBaseType_t * puxLoopCounter )
146 /* If the semaphore count is at its maximum then we should not be able to
147 * 'give' the semaphore. */
148 if( xSemaphoreGive( xSemaphore ) == pdPASS )
150 xErrorDetected = pdTRUE;
153 /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
154 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
156 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ( countMAX_COUNT_VALUE - ux ) );
158 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
160 /* We expected to be able to take the semaphore. */
161 xErrorDetected = pdTRUE;
164 ( *puxLoopCounter )++;
167 #if configUSE_PREEMPTION == 0
171 /* If the semaphore count is zero then we should not be able to 'take'
173 configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
175 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
177 xErrorDetected = pdTRUE;
180 /*-----------------------------------------------------------*/
182 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore,
183 volatile UBaseType_t * puxLoopCounter )
187 /* If the semaphore count is zero then we should not be able to 'take'
189 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
191 xErrorDetected = pdTRUE;
194 /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
195 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
197 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ux );
199 if( xSemaphoreGive( xSemaphore ) != pdPASS )
201 /* We expected to be able to take the semaphore. */
202 xErrorDetected = pdTRUE;
205 ( *puxLoopCounter )++;
208 #if configUSE_PREEMPTION == 0
212 /* If the semaphore count is at its maximum then we should not be able to
213 * 'give' the semaphore. */
214 if( xSemaphoreGive( xSemaphore ) == pdPASS )
216 xErrorDetected = pdTRUE;
219 /*-----------------------------------------------------------*/
221 static void prvCountingSemaphoreTask( void * pvParameters )
223 xCountSemStruct * pxParameter;
226 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
228 const char * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
230 /* Queue a message for printing to say the task has started. */
231 vPrintDisplayMessage( &pcTaskStartMsg );
234 /* The semaphore to be used was passed as the parameter. */
235 pxParameter = ( xCountSemStruct * ) pvParameters;
237 /* Did we expect to find the semaphore already at its max count value, or
239 if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
241 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
244 /* Now we expect the semaphore count to be 0, so this time there is an
245 * error if we can take the semaphore. */
246 if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
248 xErrorDetected = pdTRUE;
253 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
254 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
257 /*-----------------------------------------------------------*/
259 BaseType_t xAreCountingSemaphoreTasksStillRunning( void )
261 static UBaseType_t uxLastCount0 = 0, uxLastCount1 = 0;
262 BaseType_t xReturn = pdPASS;
264 /* Return fail if any 'give' or 'take' did not result in the expected
266 if( xErrorDetected != pdFALSE )
271 /* Return fail if either task is not still incrementing its loop counter. */
272 if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
278 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
281 if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
287 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;