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