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