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