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