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