]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/countsem.c
70eafaf8a43044484abbe3bab460c0a67acde469
[freertos] / FreeRTOS / Demo / Common / Minimal / countsem.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /*\r
30  * Simple demonstration of the usage of counting semaphore.\r
31  */\r
32 \r
33 /* Scheduler include files. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 #include "semphr.h"\r
37 \r
38 /* Demo program include files. */\r
39 #include "countsem.h"\r
40 \r
41 /* The maximum count value that the semaphore used for the demo can hold. */\r
42 #define countMAX_COUNT_VALUE    ( 200 )\r
43 \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
47 are valid. */\r
48 #define countSTART_AT_MAX_COUNT ( 0xaa )\r
49 #define countSTART_AT_ZERO              ( 0x55 )\r
50 \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
55 \r
56 /*-----------------------------------------------------------*/\r
57 \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
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /*\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
69  */\r
70 static void prvCountingSemaphoreTask( void *pvParameters );\r
71 \r
72 /*\r
73  * Utility function to increment the semaphore count value up from zero to\r
74  * countMAX_COUNT_VALUE.\r
75  */\r
76 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter );\r
77 \r
78 /*\r
79  * Utility function to decrement the semaphore count value up from\r
80  * countMAX_COUNT_VALUE to zero.\r
81  */\r
82 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter );\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /* The structure that is passed into the task as the task parameter. */\r
87 typedef struct COUNT_SEM_STRUCT\r
88 {\r
89         /* The semaphore to be used for the demo. */\r
90         SemaphoreHandle_t xSemaphore;\r
91 \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
96 \r
97         /* Incremented on each cycle of the demo task.  Used to detect a stalled\r
98         task. */\r
99         volatile UBaseType_t uxLoopCounter;\r
100 } xCountSemStruct;\r
101 \r
102 /* Two structures are defined, one is passed to each test task. */\r
103 static xCountSemStruct xParameters[ countNUM_TEST_TASKS ];\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vStartCountingSemaphoreTasks( void )\r
108 {\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
115 \r
116         xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );\r
117         xParameters[ 1 ].uxExpectedStartCount = 0;\r
118         xParameters[ 1 ].uxLoopCounter = 0;\r
119 \r
120         /* Were the semaphores created? */\r
121         if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )\r
122         {\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
131 \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
135         }\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 static void prvDecrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter )\r
140 {\r
141 UBaseType_t ux;\r
142 \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
146         {\r
147                 xErrorDetected = pdTRUE;\r
148         }\r
149 \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
152         {\r
153                 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ( countMAX_COUNT_VALUE - ux ) );\r
154 \r
155                 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )\r
156                 {\r
157                         /* We expected to be able to take the semaphore. */\r
158                         xErrorDetected = pdTRUE;\r
159                 }\r
160 \r
161                 ( *puxLoopCounter )++;\r
162         }\r
163 \r
164         #if configUSE_PREEMPTION == 0\r
165                 taskYIELD();\r
166         #endif\r
167 \r
168         /* If the semaphore count is zero then we should not be able to 'take'\r
169         the semaphore. */\r
170         configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );\r
171         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
172         {\r
173                 xErrorDetected = pdTRUE;\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void prvIncrementSemaphoreCount( SemaphoreHandle_t xSemaphore, volatile UBaseType_t *puxLoopCounter )\r
179 {\r
180 UBaseType_t ux;\r
181 \r
182         /* If the semaphore count is zero then we should not be able to 'take'\r
183         the semaphore. */\r
184         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
185         {\r
186                 xErrorDetected = pdTRUE;\r
187         }\r
188 \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
191         {\r
192                 configASSERT( uxSemaphoreGetCount( xSemaphore ) == ux );\r
193 \r
194                 if( xSemaphoreGive( xSemaphore ) != pdPASS )\r
195                 {\r
196                         /* We expected to be able to take the semaphore. */\r
197                         xErrorDetected = pdTRUE;\r
198                 }\r
199 \r
200                 ( *puxLoopCounter )++;\r
201         }\r
202 \r
203         #if configUSE_PREEMPTION == 0\r
204                 taskYIELD();\r
205         #endif\r
206 \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
210         {\r
211                 xErrorDetected = pdTRUE;\r
212         }\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void prvCountingSemaphoreTask( void *pvParameters )\r
217 {\r
218 xCountSemStruct *pxParameter;\r
219 \r
220         #ifdef USE_STDIO\r
221         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
222 \r
223                 const char * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";\r
224 \r
225                 /* Queue a message for printing to say the task has started. */\r
226                 vPrintDisplayMessage( &pcTaskStartMsg );\r
227         #endif\r
228 \r
229         /* The semaphore to be used was passed as the parameter. */\r
230         pxParameter = ( xCountSemStruct * ) pvParameters;\r
231 \r
232         /* Did we expect to find the semaphore already at its max count value, or\r
233         at zero? */\r
234         if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )\r
235         {\r
236                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
237         }\r
238 \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
242         {\r
243                 xErrorDetected = pdTRUE;\r
244         }\r
245 \r
246         for( ;; )\r
247         {\r
248                 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
249                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
250         }\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 BaseType_t xAreCountingSemaphoreTasksStillRunning( void )\r
255 {\r
256 static UBaseType_t uxLastCount0 = 0, uxLastCount1 = 0;\r
257 BaseType_t xReturn = pdPASS;\r
258 \r
259         /* Return fail if any 'give' or 'take' did not result in the expected\r
260         behaviour. */\r
261         if( xErrorDetected != pdFALSE )\r
262         {\r
263                 xReturn = pdFAIL;\r
264         }\r
265 \r
266         /* Return fail if either task is not still incrementing its loop counter. */\r
267         if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )\r
268         {\r
269                 xReturn = pdFAIL;\r
270         }\r
271         else\r
272         {\r
273                 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;\r
274         }\r
275 \r
276         if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )\r
277         {\r
278                 xReturn = pdFAIL;\r
279         }\r
280         else\r
281         {\r
282                 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;\r
283         }\r
284 \r
285         return xReturn;\r
286 }\r
287 \r
288 \r