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