]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/semtest.c
Update version number.
[freertos] / FreeRTOS / Demo / Common / Minimal / semtest.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * Creates two sets of two tasks.  The tasks within a set share a variable, access \r
67  * to which is guarded by a semaphore.\r
68  * \r
69  * Each task starts by attempting to obtain the semaphore.  On obtaining a \r
70  * semaphore a task checks to ensure that the guarded variable has an expected \r
71  * value.  It then clears the variable to zero before counting it back up to the \r
72  * expected value in increments of 1.  After each increment the variable is checked \r
73  * to ensure it contains the value to which it was just set. When the starting \r
74  * value is again reached the task releases the semaphore giving the other task in \r
75  * the set a chance to do exactly the same thing.  The starting value is high \r
76  * enough to ensure that a tick is likely to occur during the incrementing loop.\r
77  *\r
78  * An error is flagged if at any time during the process a shared variable is \r
79  * found to have a value other than that expected.  Such an occurrence would \r
80  * suggest an error in the mutual exclusion mechanism by which access to the \r
81  * variable is restricted.\r
82  *\r
83  * The first set of two tasks poll their semaphore.  The second set use blocking \r
84  * calls.\r
85  *\r
86  */\r
87 \r
88 \r
89 #include <stdlib.h>\r
90 \r
91 /* Scheduler include files. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 #include "semphr.h"\r
95 \r
96 /* Demo app include files. */\r
97 #include "semtest.h"\r
98 \r
99 /* The value to which the shared variables are counted. */\r
100 #define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned long ) 0xfff )\r
101 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned long ) 0xff  )\r
102 \r
103 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
104 \r
105 #define semtstNUM_TASKS                         ( 4 )\r
106 \r
107 #define semtstDELAY_FACTOR                      ( ( portTickType ) 10 )\r
108 \r
109 /* The task function as described at the top of the file. */\r
110 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );\r
111 \r
112 /* Structure used to pass parameters to each task. */\r
113 typedef struct SEMAPHORE_PARAMETERS\r
114 {\r
115         xSemaphoreHandle xSemaphore;\r
116         volatile unsigned long *pulSharedVariable;\r
117         portTickType xBlockTime;\r
118 } xSemaphoreParameters;\r
119 \r
120 /* Variables used to check that all the tasks are still running without errors. */\r
121 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
122 static volatile short sNextCheckVariable = 0;\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
127 {\r
128 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
129 const portTickType xBlockTime = ( portTickType ) 100;\r
130 \r
131         /* Create the structure used to pass parameters to the first two tasks. */\r
132         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
133 \r
134         if( pxFirstSemaphoreParameters != NULL )\r
135         {\r
136                 /* Create the semaphore used by the first two tasks. */\r
137                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
138 \r
139                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
140                 {\r
141                         /* Create the variable which is to be shared by the first two tasks. */\r
142                         pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
143 \r
144                         /* Initialise the share variable to the value the tasks expect. */\r
145                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
146 \r
147                         /* The first two tasks do not block on semaphore calls. */\r
148                         pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0;\r
149 \r
150                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
151                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
152                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL );\r
153                 }\r
154         }\r
155 \r
156         /* Do exactly the same to create the second set of tasks, only this time \r
157         provide a block time for the semaphore calls. */\r
158         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
159         if( pxSecondSemaphoreParameters != NULL )\r
160         {\r
161                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
162 \r
163                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
164                 {\r
165                         pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
166                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
167                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS;\r
168 \r
169                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
170                         xTaskCreate( prvSemaphoreTest, ( signed char * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL );\r
171                 }\r
172         }\r
173 \r
174         /* vQueueAddToRegistry() adds the semaphore to the registry, if one is\r
175         in use.  The registry is provided as a means for kernel aware \r
176         debuggers to locate semaphores and has no purpose if a kernel aware debugger\r
177         is not being used.  The call to vQueueAddToRegistry() will be removed\r
178         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
179         defined to be less than 1. */\r
180         vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_1" );\r
181         vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed char * ) "Counting_Sem_2" );\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )\r
186 {\r
187 xSemaphoreParameters *pxParameters;\r
188 volatile unsigned long *pulSharedVariable, ulExpectedValue;\r
189 unsigned long ulCounter;\r
190 short sError = pdFALSE, sCheckVariableToUse;\r
191 \r
192         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
193         protected! */\r
194         portENTER_CRITICAL();\r
195                 sCheckVariableToUse = sNextCheckVariable;\r
196                 sNextCheckVariable++;\r
197         portEXIT_CRITICAL();\r
198 \r
199         /* A structure is passed in as the parameter.  This contains the shared \r
200         variable being guarded. */\r
201         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
202         pulSharedVariable = pxParameters->pulSharedVariable;\r
203 \r
204         /* If we are blocking we use a much higher count to ensure loads of context\r
205         switches occur during the count. */\r
206         if( pxParameters->xBlockTime > ( portTickType ) 0 )\r
207         {\r
208                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
209         }\r
210         else\r
211         {\r
212                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
213         }\r
214 \r
215         for( ;; )\r
216         {\r
217                 /* Try to obtain the semaphore. */\r
218                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
219                 {\r
220                         /* We have the semaphore and so expect any other tasks using the\r
221                         shared variable to have left it in the state we expect to find\r
222                         it. */\r
223                         if( *pulSharedVariable != ulExpectedValue )\r
224                         {\r
225                                 sError = pdTRUE;\r
226                         }\r
227                         \r
228                         /* Clear the variable, then count it back up to the expected value\r
229                         before releasing the semaphore.  Would expect a context switch or\r
230                         two during this time. */\r
231                         for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
232                         {\r
233                                 *pulSharedVariable = ulCounter;\r
234                                 if( *pulSharedVariable != ulCounter )\r
235                                 {\r
236                                         sError = pdTRUE;\r
237                                 }\r
238                         }\r
239 \r
240                         /* Release the semaphore, and if no errors have occurred increment the check\r
241                         variable. */\r
242                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
243                         {\r
244                                 sError = pdTRUE;\r
245                         }\r
246 \r
247                         if( sError == pdFALSE )\r
248                         {\r
249                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
250                                 {\r
251                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
252                                 }\r
253                         }\r
254 \r
255                         /* If we have a block time then we are running at a priority higher\r
256                         than the idle priority.  This task takes a long time to complete\r
257                         a cycle (deliberately so to test the guarding) so will be starving\r
258                         out lower priority tasks.  Block for some time to allow give lower\r
259                         priority tasks some processor time. */\r
260                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
261                 }\r
262                 else\r
263                 {\r
264                         if( pxParameters->xBlockTime == ( portTickType ) 0 )\r
265                         {\r
266                                 /* We have not got the semaphore yet, so no point using the\r
267                                 processor.  We are not blocking when attempting to obtain the\r
268                                 semaphore. */\r
269                                 taskYIELD();\r
270                         }\r
271                 }\r
272         }\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 /* This is called to check that all the created tasks are still running. */\r
277 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
278 {\r
279 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
280 portBASE_TYPE xTask, xReturn = pdTRUE;\r
281 \r
282         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
283         {\r
284                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
285                 {\r
286                         xReturn = pdFALSE;\r
287                 }\r
288 \r
289                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
290         }\r
291 \r
292         return xReturn;\r
293 }\r
294 \r
295 \r