]> begriffs open source - freertos/blob - Demo/Common/Minimal/countsem.c
Update to allow use with the cooperative scheduler.
[freertos] / Demo / Common / Minimal / countsem.c
1 /*\r
2         FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com a version that has been certified for use\r
32         in safety critical systems, plus commercial licensing, development and\r
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 \r
38 /* \r
39  * Simple demonstration of the usage of counting semaphore.\r
40  */\r
41 \r
42 /* Scheduler include files. */\r
43 #include "FreeRTOS.h"\r
44 #include "task.h"\r
45 #include "semphr.h"\r
46 \r
47 /* Demo program include files. */\r
48 #include "countsem.h"\r
49 \r
50 /* The maximum count value that the semaphore used for the demo can hold. */\r
51 #define countMAX_COUNT_VALUE    ( 200 )\r
52 \r
53 /* Constants used to indicate whether or not the semaphore should have been\r
54 created with its maximum count value, or its minimum count value.  These \r
55 numbers are used to ensure that the pointers passed in as the task parameters\r
56 are valid. */\r
57 #define countSTART_AT_MAX_COUNT ( 0xaa )\r
58 #define countSTART_AT_ZERO              ( 0x55 )\r
59 \r
60 /* Two tasks are created for the test.  One uses a semaphore created with its\r
61 count value set to the maximum, and one with the count value set to zero. */\r
62 #define countNUM_TEST_TASKS             ( 2 )\r
63 #define countDONT_BLOCK                 ( 0 )\r
64 \r
65 /*-----------------------------------------------------------*/\r
66 \r
67 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
68 detected in any of the tasks. */\r
69 static portBASE_TYPE xErrorDetected = pdFALSE;\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /*\r
74  * The demo task.  This simply counts the semaphore up to its maximum value,\r
75  * the counts it back down again.  The result of each semaphore 'give' and\r
76  * 'take' is inspected, with an error being flagged if it is found not to be\r
77  * the expected result.\r
78  */\r
79 static void prvCountingSemaphoreTask( void *pvParameters );\r
80 \r
81 /*\r
82  * Utility function to increment the semaphore count value up from zero to\r
83  * countMAX_COUNT_VALUE.\r
84  */\r
85 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
86 \r
87 /*\r
88  * Utility function to decrement the semaphore count value up from \r
89  * countMAX_COUNT_VALUE to zero.\r
90  */\r
91 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /* The structure that is passed into the task as the task parameter. */\r
96 typedef struct COUNT_SEM_STRUCT\r
97 {\r
98         /* The semaphore to be used for the demo. */\r
99         xSemaphoreHandle xSemaphore;\r
100 \r
101         /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with\r
102         its count value set to its max count value, or countSTART_AT_ZERO if it\r
103         should have been created with its count value set to 0. */\r
104         unsigned portBASE_TYPE uxExpectedStartCount;    \r
105 \r
106         /* Incremented on each cycle of the demo task.  Used to detect a stalled\r
107         task. */\r
108         unsigned portBASE_TYPE uxLoopCounter;                   \r
109 } xCountSemStruct;\r
110 \r
111 /* Two structures are defined, one is passed to each test task. */\r
112 static xCountSemStruct xParameters[ countNUM_TEST_TASKS ];\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 void vStartCountingSemaphoreTasks( void )\r
117 {\r
118         /* Create the semaphores that we are going to use for the test/demo.  The\r
119         first should be created such that it starts at its maximum count value,\r
120         the second should be created such that it starts with a count value of zero. */\r
121         xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );\r
122         xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;\r
123         xParameters[ 0 ].uxLoopCounter = 0;\r
124 \r
125         xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );\r
126         xParameters[ 1 ].uxExpectedStartCount = 0;\r
127         xParameters[ 1 ].uxLoopCounter = 0;\r
128 \r
129         /* Were the semaphores created? */\r
130         if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )\r
131         {\r
132                 /* Create the demo tasks, passing in the semaphore to use as the parameter. */\r
133                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );\r
134                 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );              \r
135         }\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
140 {\r
141 unsigned portBASE_TYPE 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                 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )\r
154                 {\r
155                         /* We expected to be able to take the semaphore. */\r
156                         xErrorDetected = pdTRUE;\r
157                 }\r
158 \r
159                 ( *puxLoopCounter )++;\r
160         }\r
161 \r
162         #if configUSE_PREEMPTION == 0\r
163                 taskYIELD();\r
164         #endif\r
165 \r
166         /* If the semaphore count is zero then we should not be able to 'take' \r
167         the semaphore. */\r
168         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
169         {\r
170                 xErrorDetected = pdTRUE;\r
171         }\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )\r
176 {\r
177 unsigned portBASE_TYPE ux;\r
178 \r
179         /* If the semaphore count is zero then we should not be able to 'take' \r
180         the semaphore. */\r
181         if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )\r
182         {\r
183                 xErrorDetected = pdTRUE;\r
184         }\r
185 \r
186         /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */\r
187         for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )\r
188         {\r
189                 if( xSemaphoreGive( xSemaphore ) != pdPASS )\r
190                 {\r
191                         /* We expected to be able to take the semaphore. */\r
192                         xErrorDetected = pdTRUE;\r
193                 }\r
194 \r
195                 ( *puxLoopCounter )++;\r
196         }\r
197 \r
198         #if configUSE_PREEMPTION == 0\r
199                 taskYIELD();\r
200         #endif\r
201 \r
202         /* If the semaphore count is at its maximum then we should not be able to\r
203         'give' the semaphore. */\r
204         if( xSemaphoreGive( xSemaphore ) == pdPASS )\r
205         {\r
206                 xErrorDetected = pdTRUE;\r
207         }\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void prvCountingSemaphoreTask( void *pvParameters )\r
212 {\r
213 xCountSemStruct *pxParameter;\r
214 \r
215         #ifdef USE_STDIO\r
216         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
217         \r
218                 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";\r
219 \r
220                 /* Queue a message for printing to say the task has started. */\r
221                 vPrintDisplayMessage( &pcTaskStartMsg );\r
222         #endif\r
223 \r
224         /* The semaphore to be used was passed as the parameter. */\r
225         pxParameter = ( xCountSemStruct * ) pvParameters;\r
226 \r
227         /* Did we expect to find the semaphore already at its max count value, or\r
228         at zero? */\r
229         if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )\r
230         {\r
231                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
232         }\r
233 \r
234         /* Now we expect the semaphore count to be 0, so this time there is an\r
235         error if we can take the semaphore. */\r
236         if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )\r
237         {\r
238                 xErrorDetected = pdTRUE;\r
239         }\r
240 \r
241         for( ;; )\r
242         {\r
243                 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
244                 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );\r
245         }\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )\r
250 {\r
251 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;\r
252 portBASE_TYPE xReturn = pdPASS;\r
253 \r
254         /* Return fail if any 'give' or 'take' did not result in the expected\r
255         behaviour. */\r
256         if( xErrorDetected != pdFALSE )\r
257         {\r
258                 xReturn = pdFAIL;\r
259         }\r
260 \r
261         /* Return fail if either task is not still incrementing its loop counter. */\r
262         if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )\r
263         {\r
264                 xReturn = pdFAIL;\r
265         }\r
266         else\r
267         {\r
268                 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;\r
269         }\r
270 \r
271         if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )\r
272         {\r
273                 xReturn = pdFAIL;\r
274         }\r
275         else\r
276         {\r
277                 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;\r
278         }\r
279 \r
280         return xReturn;\r
281 }\r
282 \r
283 \r