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
31 * Simple demonstration of the usage of counting semaphore.
\r
34 /* Scheduler include files. */
\r
35 #include "FreeRTOS.h"
\r
39 /* Demo program include files. */
\r
40 #include "countsem.h"
\r
42 /* The maximum count value that the semaphore used for the demo can hold. */
\r
43 #define countMAX_COUNT_VALUE ( 200 )
\r
45 /* Constants used to indicate whether or not the semaphore should have been
\r
46 created with its maximum count value, or its minimum count value. These
\r
47 numbers are used to ensure that the pointers passed in as the task parameters
\r
49 #define countSTART_AT_MAX_COUNT ( 0xaa )
\r
50 #define countSTART_AT_ZERO ( 0x55 )
\r
52 /* Two tasks are created for the test. One uses a semaphore created with its
\r
53 count value set to the maximum, and one with the count value set to zero. */
\r
54 #define countNUM_TEST_TASKS ( 2 )
\r
55 #define countDONT_BLOCK ( 0 )
\r
57 /*-----------------------------------------------------------*/
\r
59 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
60 detected in any of the tasks. */
\r
61 static volatile BaseType_t xErrorDetected = pdFALSE;
\r
63 /*-----------------------------------------------------------*/
\r
66 * The demo task. This simply counts the semaphore up to its maximum value,
\r
67 * the counts it back down again. The result of each semaphore 'give' and
\r
68 * 'take' is inspected, with an error being flagged if it is found not to be
\r
69 * the expected result.
\r
71 static void prvCountingSemaphoreTask( void *pvParameters );
\r
74 * Utility function to increment the semaphore count value up from zero to
\r
75 * countMAX_COUNT_VALUE.
\r
77 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter );
\r
80 * Utility function to decrement the semaphore count value up from
\r
81 * countMAX_COUNT_VALUE to zero.
\r
83 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter );
\r
85 /*-----------------------------------------------------------*/
\r
87 /* The structure that is passed into the task as the task parameter. */
\r
88 typedef struct COUNT_SEM_STRUCT
\r
90 /* The semaphore to be used for the demo. */
\r
91 SemaphoreHandle_t xSemaphore;
\r
93 /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
\r
94 its count value set to its max count value, or countSTART_AT_ZERO if it
\r
95 should have been created with its count value set to 0. */
\r
96 UBaseType_t uxExpectedStartCount;
\r
98 /* Incremented on each cycle of the demo task. Used to detect a stalled
\r
100 volatile UBaseType_t uxLoopCounter;
\r
103 /* Two structures are defined, one is passed to each test task. */
\r
104 static xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
\r
106 /*-----------------------------------------------------------*/
\r
108 void vStartCountingSemaphoreTasks( void )
\r
110 /* Create the semaphores that we are going to use for the test/demo. The
\r
111 first should be created such that it starts at its maximum count value,
\r
112 the second should be created such that it starts with a count value of zero. */
\r
113 xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
\r
114 xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
\r
115 xParameters[ 0 ].uxLoopCounter = 0;
\r
117 xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
\r
118 xParameters[ 1 ].uxExpectedStartCount = 0;
\r
119 xParameters[ 1 ].uxLoopCounter = 0;
\r
121 /* Were the semaphores created? */
\r
122 if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
\r
124 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
125 in use. The registry is provided as a means for kernel aware
\r
126 debuggers to locate semaphores and has no purpose if a kernel aware
\r
127 debugger is not being used. The call to vQueueAddToRegistry() will be
\r
128 removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not
\r
129 defined or is defined to be less than 1. */
\r
130 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 0 ].xSemaphore, "Counting_Sem_1" );
\r
131 vQueueAddToRegistry( ( QueueHandle_t ) xParameters[ 1 ].xSemaphore, "Counting_Sem_2" );
\r
133 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
\r
134 xTaskCreate( prvCountingSemaphoreTask, "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
\r
135 xTaskCreate( prvCountingSemaphoreTask, "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
\r
138 /*-----------------------------------------------------------*/
\r
140 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter )
\r
144 /* If the semaphore count is at its maximum then we should not be able to
\r
145 'give' the semaphore. */
\r
146 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
148 xErrorDetected = pdTRUE;
\r
151 /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
\r
152 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
154 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ( countMAX_COUNT_VALUE - ux ) );
\r
156 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
\r
158 /* We expected to be able to take the semaphore. */
\r
159 xErrorDetected = pdTRUE;
\r
162 ( *puxLoopCounter )++;
\r
165 #if configUSE_PREEMPTION == 0
\r
169 /* If the semaphore count is zero then we should not be able to 'take'
\r
171 configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
\r
172 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
174 xErrorDetected = pdTRUE;
\r
177 /*-----------------------------------------------------------*/
\r
179 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter )
\r
183 /* If the semaphore count is zero then we should not be able to 'take'
\r
185 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
187 xErrorDetected = pdTRUE;
\r
190 /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
\r
191 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
193 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ux );
\r
195 if( xSemaphoreGive( xSemaphore ) != pdPASS )
\r
197 /* We expected to be able to take the semaphore. */
\r
198 xErrorDetected = pdTRUE;
\r
201 ( *puxLoopCounter )++;
\r
204 #if configUSE_PREEMPTION == 0
\r
208 /* If the semaphore count is at its maximum then we should not be able to
\r
209 'give' the semaphore. */
\r
210 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
212 xErrorDetected = pdTRUE;
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvCountingSemaphoreTask( void *pvParameters )
\r
219 xCountSemStruct *pxParameter;
\r
222 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
224 const char * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
\r
226 /* Queue a message for printing to say the task has started. */
\r
227 vPrintDisplayMessage( &pcTaskStartMsg );
\r
230 /* The semaphore to be used was passed as the parameter. */
\r
231 pxParameter = ( xCountSemStruct * ) pvParameters;
\r
233 /* Did we expect to find the semaphore already at its max count value, or
\r
235 if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
\r
237 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
240 /* Now we expect the semaphore count to be 0, so this time there is an
\r
241 error if we can take the semaphore. */
\r
242 if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
\r
244 xErrorDetected = pdTRUE;
\r
249 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
250 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
253 /*-----------------------------------------------------------*/
\r
255 BaseType_t xAreCountingSemaphoreTasksStillRunning( void )
\r
257 static UBaseType_t uxLastCount0 = 0, uxLastCount1 = 0;
\r
258 BaseType_t xReturn = pdPASS;
\r
260 /* Return fail if any 'give' or 'take' did not result in the expected
\r
262 if( xErrorDetected != pdFALSE )
\r
267 /* Return fail if either task is not still incrementing its loop counter. */
\r
268 if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
\r
274 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
\r
277 if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
\r
283 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
\r