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