]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/semtest.c
Ready for V5.1.1 release.
[freertos] / Demo / Common / Minimal / 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  */\r
72 \r
73 \r
74 #include <stdlib.h>\r
75 \r
76 /* Scheduler include files. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "semphr.h"\r
80 \r
81 /* Demo app include files. */\r
82 #include "semtest.h"\r
83 \r
84 /* The value to which the shared variables are counted. */\r
85 #define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned portLONG ) 0xfff )\r
86 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned portLONG ) 0xff  )\r
87 \r
88 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
89 \r
90 #define semtstNUM_TASKS                         ( 4 )\r
91 \r
92 #define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )\r
93 \r
94 /* The task function as described at the top of the file. */\r
95 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );\r
96 \r
97 /* Structure used to pass parameters to each task. */\r
98 typedef struct SEMAPHORE_PARAMETERS\r
99 {\r
100         xSemaphoreHandle xSemaphore;\r
101         volatile unsigned portLONG *pulSharedVariable;\r
102         portTickType xBlockTime;\r
103 } xSemaphoreParameters;\r
104 \r
105 /* Variables used to check that all the tasks are still running without errors. */\r
106 static volatile portSHORT sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
107 static volatile portSHORT sNextCheckVariable = 0;\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
112 {\r
113 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
114 const portTickType xBlockTime = ( portTickType ) 100;\r
115 \r
116         /* Create the structure used to pass parameters to the first two tasks. */\r
117         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
118 \r
119         if( pxFirstSemaphoreParameters != NULL )\r
120         {\r
121                 /* Create the semaphore used by the first two tasks. */\r
122                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
123 \r
124                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
125                 {\r
126                         /* Create the variable which is to be shared by the first two tasks. */\r
127                         pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );\r
128 \r
129                         /* Initialise the share variable to the value the tasks expect. */\r
130                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
131 \r
132                         /* The first two tasks do not block on semaphore calls. */\r
133                         pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;\r
134 \r
135                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
136                         xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
137                         xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
138                 }\r
139         }\r
140 \r
141         /* Do exactly the same to create the second set of tasks, only this time \r
142         provide a block time for the semaphore calls. */\r
143         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
144         if( pxSecondSemaphoreParameters != NULL )\r
145         {\r
146                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
147 \r
148                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
149                 {\r
150                         pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) );\r
151                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
152                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;\r
153 \r
154                         xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
155                         xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
156                 }\r
157         }\r
158 \r
159         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
160         in use.  The registry is provided as a means for kernel aware \r
161         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
162         is not being used.  The call to vQueueAddToRegistry() will be removed\r
163         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
164         defined to be less than 1. */\r
165         vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );\r
166         vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )\r
171 {\r
172 xSemaphoreParameters *pxParameters;\r
173 volatile unsigned portLONG *pulSharedVariable, ulExpectedValue;\r
174 unsigned portLONG ulCounter;\r
175 portSHORT sError = pdFALSE, sCheckVariableToUse;\r
176 \r
177         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
178         protected! */\r
179         portENTER_CRITICAL();\r
180                 sCheckVariableToUse = sNextCheckVariable;\r
181                 sNextCheckVariable++;\r
182         portEXIT_CRITICAL();\r
183 \r
184         /* A structure is passed in as the parameter.  This contains the shared \r
185         variable being guarded. */\r
186         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
187         pulSharedVariable = pxParameters->pulSharedVariable;\r
188 \r
189         /* If we are blocking we use a much higher count to ensure loads of context\r
190         switches occur during the count. */\r
191         if( pxParameters->xBlockTime > ( portTickType ) 0 )\r
192         {\r
193                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
194         }\r
195         else\r
196         {\r
197                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
198         }\r
199 \r
200         for( ;; )\r
201         {\r
202                 /* Try to obtain the semaphore. */\r
203                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
204                 {\r
205                         /* We have the semaphore and so expect any other tasks using the\r
206                         shared variable to have left it in the state we expect to find\r
207                         it. */\r
208                         if( *pulSharedVariable != ulExpectedValue )\r
209                         {\r
210                                 sError = pdTRUE;\r
211                         }\r
212                         \r
213                         /* Clear the variable, then count it back up to the expected value\r
214                         before releasing the semaphore.  Would expect a context switch or\r
215                         two during this time. */\r
216                         for( ulCounter = ( unsigned portLONG ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
217                         {\r
218                                 *pulSharedVariable = ulCounter;\r
219                                 if( *pulSharedVariable != ulCounter )\r
220                                 {\r
221                                         sError = pdTRUE;\r
222                                 }\r
223                         }\r
224 \r
225                         /* Release the semaphore, and if no errors have occurred increment the check\r
226                         variable. */\r
227                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
228                         {\r
229                                 sError = pdTRUE;\r
230                         }\r
231 \r
232                         if( sError == pdFALSE )\r
233                         {\r
234                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
235                                 {\r
236                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
237                                 }\r
238                         }\r
239 \r
240                         /* If we have a block time then we are running at a priority higher\r
241                         than the idle priority.  This task takes a long time to complete\r
242                         a cycle (deliberately so to test the guarding) so will be starving\r
243                         out lower priority tasks.  Block for some time to allow give lower\r
244                         priority tasks some processor time. */\r
245                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
246                 }\r
247                 else\r
248                 {\r
249                         if( pxParameters->xBlockTime == ( portTickType ) 0 )\r
250                         {\r
251                                 /* We have not got the semaphore yet, so no point using the\r
252                                 processor.  We are not blocking when attempting to obtain the\r
253                                 semaphore. */\r
254                                 taskYIELD();\r
255                         }\r
256                 }\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 /* This is called to check that all the created tasks are still running. */\r
262 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
263 {\r
264 static portSHORT sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
265 portBASE_TYPE xTask, xReturn = pdTRUE;\r
266 \r
267         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
268         {\r
269                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
270                 {\r
271                         xReturn = pdFALSE;\r
272                 }\r
273 \r
274                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
275         }\r
276 \r
277         return xReturn;\r
278 }\r
279 \r
280 \r