2 * FreeRTOS Kernel V10.1.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.
\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
30 * Simple demonstration of the usage of counting semaphore.
\r
33 /* Scheduler include files. */
\r
34 #include "FreeRTOS.h"
\r
38 /* Demo program include files. */
\r
39 #include "countsem.h"
\r
41 /* The maximum count value that the semaphore used for the demo can hold. */
\r
42 #define countMAX_COUNT_VALUE ( 200 )
\r
44 /* Constants used to indicate whether or not the semaphore should have been
\r
45 created with its maximum count value, or its minimum count value. These
\r
46 numbers are used to ensure that the pointers passed in as the task parameters
\r
48 #define countSTART_AT_MAX_COUNT ( 0xaa )
\r
49 #define countSTART_AT_ZERO ( 0x55 )
\r
51 /* Two tasks are created for the test. One uses a semaphore created with its
\r
52 count value set to the maximum, and one with the count value set to zero. */
\r
53 #define countNUM_TEST_TASKS ( 2 )
\r
54 #define countDONT_BLOCK ( 0 )
\r
56 /*-----------------------------------------------------------*/
\r
58 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
59 detected in any of the tasks. */
\r
60 static volatile BaseType_t xErrorDetected = pdFALSE;
\r
62 /*-----------------------------------------------------------*/
\r
65 * The demo task. This simply counts the semaphore up to its maximum value,
\r
66 * the counts it back down again. The result of each semaphore 'give' and
\r
67 * 'take' is inspected, with an error being flagged if it is found not to be
\r
68 * the expected result.
\r
70 static void prvCountingSemaphoreTask( void *pvParameters );
\r
73 * Utility function to increment the semaphore count value up from zero to
\r
74 * countMAX_COUNT_VALUE.
\r
76 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter );
\r
79 * Utility function to decrement the semaphore count value up from
\r
80 * countMAX_COUNT_VALUE to zero.
\r
82 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter );
\r
84 /*-----------------------------------------------------------*/
\r
86 /* The structure that is passed into the task as the task parameter. */
\r
87 typedef struct COUNT_SEM_STRUCT
\r
89 /* The semaphore to be used for the demo. */
\r
90 SemaphoreHandle_t xSemaphore;
\r
92 /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
\r
93 its count value set to its max count value, or countSTART_AT_ZERO if it
\r
94 should have been created with its count value set to 0. */
\r
95 UBaseType_t uxExpectedStartCount;
\r
97 /* Incremented on each cycle of the demo task. Used to detect a stalled
\r
99 volatile UBaseType_t uxLoopCounter;
\r
102 /* Two structures are defined, one is passed to each test task. */
\r
103 static xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
\r
105 /*-----------------------------------------------------------*/
\r
107 void vStartCountingSemaphoreTasks( void )
\r
109 /* Create the semaphores that we are going to use for the test/demo. The
\r
110 first should be created such that it starts at its maximum count value,
\r
111 the second should be created such that it starts with a count value of zero. */
\r
112 xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
\r
113 xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
\r
114 xParameters[ 0 ].uxLoopCounter = 0;
\r
116 xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
\r
117 xParameters[ 1 ].uxExpectedStartCount = 0;
\r
118 xParameters[ 1 ].uxLoopCounter = 0;
\r
120 /* Were the semaphores created? */
\r
121 if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
\r
123 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
124 in use. The registry is provided as a means for kernel aware
\r
125 debuggers to locate semaphores and has no purpose if a kernel aware
\r
126 debugger is not being used. The call to vQueueAddToRegistry() will be
\r
127 removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
\r
128 defined or is defined to be less than 1. */
\r
129 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 0 ].xSemaphore, "Counting_Sem_1" );
\r
130 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 1 ].xSemaphore, "Counting_Sem_2" );
\r
132 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
\r
133 xTaskCreate( prvCountingSemaphoreTask, "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
\r
134 xTaskCreate( prvCountingSemaphoreTask, "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
\r
137 /*-----------------------------------------------------------*/
\r
139 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter )
\r
143 /* If the semaphore count is at its maximum then we should not be able to
\r
144 'give' the semaphore. */
\r
145 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
147 xErrorDetected = pdTRUE;
\r
150 /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
\r
151 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
153 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ( countMAX_COUNT_VALUE - ux ) );
\r
155 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
\r
157 /* We expected to be able to take the semaphore. */
\r
158 xErrorDetected = pdTRUE;
\r
161 ( *puxLoopCounter )++;
\r
164 #if configUSE_PREEMPTION == 0
\r
168 /* If the semaphore count is zero then we should not be able to 'take'
\r
170 configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
\r
171 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
173 xErrorDetected = pdTRUE;
\r
176 /*-----------------------------------------------------------*/
\r
178 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter )
\r
182 /* If the semaphore count is zero then we should not be able to 'take'
\r
184 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
186 xErrorDetected = pdTRUE;
\r
189 /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
\r
190 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
192 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ux );
\r
194 if( xSemaphoreGive( xSemaphore ) != pdPASS )
\r
196 /* We expected to be able to take the semaphore. */
\r
197 xErrorDetected = pdTRUE;
\r
200 ( *puxLoopCounter )++;
\r
203 #if configUSE_PREEMPTION == 0
\r
207 /* If the semaphore count is at its maximum then we should not be able to
\r
208 'give' the semaphore. */
\r
209 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
211 xErrorDetected = pdTRUE;
\r
214 /*-----------------------------------------------------------*/
\r
216 static void prvCountingSemaphoreTask( void *pvParameters )
\r
218 xCountSemStruct *pxParameter;
\r
221 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
223 const char * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
\r
225 /* Queue a message for printing to say the task has started. */
\r
226 vPrintDisplayMessage( &pcTaskStartMsg );
\r
229 /* The semaphore to be used was passed as the parameter. */
\r
230 pxParameter = ( xCountSemStruct * ) pvParameters;
\r
232 /* Did we expect to find the semaphore already at its max count value, or
\r
234 if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
\r
236 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
239 /* Now we expect the semaphore count to be 0, so this time there is an
\r
240 error if we can take the semaphore. */
\r
241 if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
\r
243 xErrorDetected = pdTRUE;
\r
248 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
249 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
252 /*-----------------------------------------------------------*/
\r
254 BaseType_t xAreCountingSemaphoreTasksStillRunning( void )
\r
256 static UBaseType_t uxLastCount0 = 0, uxLastCount1 = 0;
\r
257 BaseType_t xReturn = pdPASS;
\r
259 /* Return fail if any 'give' or 'take' did not result in the expected
\r
261 if( xErrorDetected != pdFALSE )
\r
266 /* Return fail if either task is not still incrementing its loop counter. */
\r
267 if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
\r
273 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
\r
276 if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
\r
282 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
\r