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