]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/semtest.c
Update version number.
[freertos] / Demo / Common / Full / semtest.c
1 /*\r
2         FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 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 it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /**\r
53  * Creates two sets of two tasks.  The tasks within a set share a variable, access \r
54  * to which is guarded by a semaphore.\r
55  * \r
56  * Each task starts by attempting to obtain the semaphore.  On obtaining a \r
57  * semaphore a task checks to ensure that the guarded variable has an expected \r
58  * value.  It then clears the variable to zero before counting it back up to the \r
59  * expected value in increments of 1.  After each increment the variable is checked \r
60  * to ensure it contains the value to which it was just set. When the starting \r
61  * value is again reached the task releases the semaphore giving the other task in \r
62  * the set a chance to do exactly the same thing.  The starting value is high \r
63  * enough to ensure that a tick is likely to occur during the incrementing loop.\r
64  *\r
65  * An error is flagged if at any time during the process a shared variable is \r
66  * found to have a value other than that expected.  Such an occurrence would \r
67  * suggest an error in the mutual exclusion mechanism by which access to the \r
68  * variable is restricted.\r
69  *\r
70  * The first set of two tasks poll their semaphore.  The second set use blocking \r
71  * calls.\r
72  *\r
73  * \page SemTestC semtest.c\r
74  * \ingroup DemoFiles\r
75  * <HR>\r
76  */\r
77 \r
78 /*\r
79 Changes from V1.2.0:\r
80 \r
81         + The tasks that operate at the idle priority now use a lower expected\r
82           count than those running at a higher priority.  This prevents the low\r
83           priority tasks from signaling an error because they have not been \r
84           scheduled enough time for each of them to count the shared variable to\r
85           the high value.\r
86 \r
87 Changes from V2.0.0\r
88 \r
89         + Delay periods are now specified using variables and constants of\r
90           portTickType rather than unsigned portLONG.\r
91 \r
92 Changes from V2.1.1\r
93 \r
94         + The stack size now uses configMINIMAL_STACK_SIZE.\r
95         + String constants made file scope to decrease stack depth on 8051 port.\r
96 */\r
97 \r
98 #include <stdlib.h>\r
99 \r
100 /* Scheduler include files. */\r
101 #include "FreeRTOS.h"\r
102 #include "task.h"\r
103 #include "semphr.h"\r
104 \r
105 /* Demo app include files. */\r
106 #include "semtest.h"\r
107 #include "print.h"\r
108 \r
109 /* The value to which the shared variables are counted. */\r
110 #define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned portLONG ) 0xfff )\r
111 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned portLONG ) 0xff  )\r
112 \r
113 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
114 \r
115 #define semtstNUM_TASKS                         ( 4 )\r
116 \r
117 #define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )\r
118 \r
119 /* The task function as described at the top of the file. */\r
120 static void prvSemaphoreTest( void *pvParameters );\r
121 \r
122 /* Structure used to pass parameters to each task. */\r
123 typedef struct SEMAPHORE_PARAMETERS\r
124 {\r
125         xSemaphoreHandle xSemaphore;\r
126         volatile unsigned portLONG *pulSharedVariable;\r
127         portTickType xBlockTime;\r
128 } xSemaphoreParameters;\r
129 \r
130 /* Variables used to check that all the tasks are still running without errors. */\r
131 static volatile portSHORT sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
132 static volatile portSHORT sNextCheckVariable = 0;\r
133 \r
134 /* Strings to print if USE_STDIO is defined. */\r
135 const portCHAR * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";\r
136 const portCHAR * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
141 {\r
142 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
143 const portTickType xBlockTime = ( portTickType ) 100;\r
144 \r
145         /* Create the structure used to pass parameters to the first two tasks. */\r
146         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
147 \r
148         if( pxFirstSemaphoreParameters != NULL )\r
149         {\r
150                 /* Create the semaphore used by the first two tasks. */\r
151                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
152 \r
153                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
154                 {\r
155                         /* Create the variable which is to be shared by the first two tasks. */\r
156                         pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );\r
157 \r
158                         /* Initialise the share variable to the value the tasks expect. */\r
159                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
160 \r
161                         /* The first two tasks do not block on semaphore calls. */\r
162                         pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;\r
163 \r
164                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
165                         xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
166                         xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
167                 }\r
168         }\r
169 \r
170         /* Do exactly the same to create the second set of tasks, only this time \r
171         provide a block time for the semaphore calls. */\r
172         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
173         if( pxSecondSemaphoreParameters != NULL )\r
174         {\r
175                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
176 \r
177                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
178                 {\r
179                         pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );\r
180                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
181                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;\r
182 \r
183                         xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
184                         xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
185                 }\r
186         }\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static void prvSemaphoreTest( void *pvParameters )\r
191 {\r
192 xSemaphoreParameters *pxParameters;\r
193 volatile unsigned portLONG *pulSharedVariable, ulExpectedValue;\r
194 unsigned portLONG ulCounter;\r
195 portSHORT sError = pdFALSE, sCheckVariableToUse;\r
196 \r
197         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
198         protected! */\r
199         portENTER_CRITICAL();\r
200                 sCheckVariableToUse = sNextCheckVariable;\r
201                 sNextCheckVariable++;\r
202         portEXIT_CRITICAL();\r
203 \r
204         /* Queue a message for printing to say the task has started. */\r
205         vPrintDisplayMessage( &pcSemaphoreTaskStart );\r
206 \r
207         /* A structure is passed in as the parameter.  This contains the shared \r
208         variable being guarded. */\r
209         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
210         pulSharedVariable = pxParameters->pulSharedVariable;\r
211 \r
212         /* If we are blocking we use a much higher count to ensure loads of context\r
213         switches occur during the count. */\r
214         if( pxParameters->xBlockTime > ( portTickType ) 0 )\r
215         {\r
216                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
217         }\r
218         else\r
219         {\r
220                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
221         }\r
222 \r
223         for( ;; )\r
224         {\r
225                 /* Try to obtain the semaphore. */\r
226                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
227                 {\r
228                         /* We have the semaphore and so expect any other tasks using the\r
229                         shared variable to have left it in the state we expect to find\r
230                         it. */\r
231                         if( *pulSharedVariable != ulExpectedValue )\r
232                         {\r
233                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
234                                 sError = pdTRUE;\r
235                         }\r
236                         \r
237                         /* Clear the variable, then count it back up to the expected value\r
238                         before releasing the semaphore.  Would expect a context switch or\r
239                         two during this time. */\r
240                         for( ulCounter = ( unsigned portLONG ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
241                         {\r
242                                 *pulSharedVariable = ulCounter;\r
243                                 if( *pulSharedVariable != ulCounter )\r
244                                 {\r
245                                         if( sError == pdFALSE )\r
246                                         {\r
247                                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
248                                         }\r
249                                         sError = pdTRUE;\r
250                                 }\r
251                         }\r
252 \r
253                         /* Release the semaphore, and if no errors have occurred increment the check\r
254                         variable. */\r
255                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
256                         {\r
257                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
258                                 sError = pdTRUE;\r
259                         }\r
260 \r
261                         if( sError == pdFALSE )\r
262                         {\r
263                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
264                                 {\r
265                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
266                                 }\r
267                         }\r
268 \r
269                         /* If we have a block time then we are running at a priority higher\r
270                         than the idle priority.  This task takes a long time to complete\r
271                         a cycle (deliberately so to test the guarding) so will be starving\r
272                         out lower priority tasks.  Block for some time to allow give lower\r
273                         priority tasks some processor time. */\r
274                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
275                 }\r
276                 else\r
277                 {\r
278                         if( pxParameters->xBlockTime == ( portTickType ) 0 )\r
279                         {\r
280                                 /* We have not got the semaphore yet, so no point using the\r
281                                 processor.  We are not blocking when attempting to obtain the\r
282                                 semaphore. */\r
283                                 taskYIELD();\r
284                         }\r
285                 }\r
286         }\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 /* This is called to check that all the created tasks are still running. */\r
291 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
292 {\r
293 static portSHORT sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
294 portBASE_TYPE xTask, xReturn = pdTRUE;\r
295 \r
296         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
297         {\r
298                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
299                 {\r
300                         xReturn = pdFALSE;\r
301                 }\r
302 \r
303                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
304         }\r
305 \r
306         return xReturn;\r
307 }\r
308 \r
309 \r