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