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