]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/semtest.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Full / semtest.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /**\r
30  * Creates two sets of two tasks.  The tasks within a set share a variable, access \r
31  * to which is guarded by a semaphore.\r
32  * \r
33  * Each task starts by attempting to obtain the semaphore.  On obtaining a \r
34  * semaphore a task checks to ensure that the guarded variable has an expected \r
35  * value.  It then clears the variable to zero before counting it back up to the \r
36  * expected value in increments of 1.  After each increment the variable is checked \r
37  * to ensure it contains the value to which it was just set. When the starting \r
38  * value is again reached the task releases the semaphore giving the other task in \r
39  * the set a chance to do exactly the same thing.  The starting value is high \r
40  * enough to ensure that a tick is likely to occur during the incrementing loop.\r
41  *\r
42  * An error is flagged if at any time during the process a shared variable is \r
43  * found to have a value other than that expected.  Such an occurrence would \r
44  * suggest an error in the mutual exclusion mechanism by which access to the \r
45  * variable is restricted.\r
46  *\r
47  * The first set of two tasks poll their semaphore.  The second set use blocking \r
48  * calls.\r
49  *\r
50  * \page SemTestC semtest.c\r
51  * \ingroup DemoFiles\r
52  * <HR>\r
53  */\r
54 \r
55 /*\r
56 Changes from V1.2.0:\r
57 \r
58         + The tasks that operate at the idle priority now use a lower expected\r
59           count than those running at a higher priority.  This prevents the low\r
60           priority tasks from signaling an error because they have not been \r
61           scheduled enough time for each of them to count the shared variable to\r
62           the high value.\r
63 \r
64 Changes from V2.0.0\r
65 \r
66         + Delay periods are now specified using variables and constants of\r
67           TickType_t rather than unsigned long.\r
68 \r
69 Changes from V2.1.1\r
70 \r
71         + The stack size now uses configMINIMAL_STACK_SIZE.\r
72         + String constants made file scope to decrease stack depth on 8051 port.\r
73 */\r
74 \r
75 #include <stdlib.h>\r
76 \r
77 /* Scheduler include files. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "semphr.h"\r
81 \r
82 /* Demo app include files. */\r
83 #include "semtest.h"\r
84 #include "print.h"\r
85 \r
86 /* The value to which the shared variables are counted. */\r
87 #define semtstBLOCKING_EXPECTED_VALUE           ( ( unsigned long ) 0xfff )\r
88 #define semtstNON_BLOCKING_EXPECTED_VALUE       ( ( unsigned long ) 0xff  )\r
89 \r
90 #define semtstSTACK_SIZE                        configMINIMAL_STACK_SIZE\r
91 \r
92 #define semtstNUM_TASKS                         ( 4 )\r
93 \r
94 #define semtstDELAY_FACTOR                      ( ( TickType_t ) 10 )\r
95 \r
96 /* The task function as described at the top of the file. */\r
97 static void prvSemaphoreTest( void *pvParameters );\r
98 \r
99 /* Structure used to pass parameters to each task. */\r
100 typedef struct SEMAPHORE_PARAMETERS\r
101 {\r
102         SemaphoreHandle_t xSemaphore;\r
103         volatile unsigned long *pulSharedVariable;\r
104         TickType_t xBlockTime;\r
105 } xSemaphoreParameters;\r
106 \r
107 /* Variables used to check that all the tasks are still running without errors. */\r
108 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
109 static volatile short sNextCheckVariable = 0;\r
110 \r
111 /* Strings to print if USE_STDIO is defined. */\r
112 const char * const pcPollingSemaphoreTaskError = "Guarded shared variable in unexpected state.\r\n";\r
113 const char * const pcSemaphoreTaskStart = "Guarded shared variable task started.\r\n";\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority )\r
118 {\r
119 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;\r
120 const TickType_t xBlockTime = ( TickType_t ) 100;\r
121 \r
122         /* Create the structure used to pass parameters to the first two tasks. */\r
123         pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
124 \r
125         if( pxFirstSemaphoreParameters != NULL )\r
126         {\r
127                 /* Create the semaphore used by the first two tasks. */\r
128                 vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore );\r
129 \r
130                 if( pxFirstSemaphoreParameters->xSemaphore != NULL )\r
131                 {\r
132                         /* Create the variable which is to be shared by the first two tasks. */\r
133                         pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
134 \r
135                         /* Initialise the share variable to the value the tasks expect. */\r
136                         *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;\r
137 \r
138                         /* The first two tasks do not block on semaphore calls. */\r
139                         pxFirstSemaphoreParameters->xBlockTime = ( TickType_t ) 0;\r
140 \r
141                         /* Spawn the first two tasks.  As they poll they operate at the idle priority. */\r
142                         xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );\r
143                         xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );\r
144                 }\r
145         }\r
146 \r
147         /* Do exactly the same to create the second set of tasks, only this time \r
148         provide a block time for the semaphore calls. */\r
149         pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );\r
150         if( pxSecondSemaphoreParameters != NULL )\r
151         {\r
152                 vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore );\r
153 \r
154                 if( pxSecondSemaphoreParameters->xSemaphore != NULL )\r
155                 {\r
156                         pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned long * ) pvPortMalloc( sizeof( unsigned long ) );\r
157                         *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;\r
158                         pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;\r
159 \r
160                         xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
161                         xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );\r
162                 }\r
163         }\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 static void prvSemaphoreTest( void *pvParameters )\r
168 {\r
169 xSemaphoreParameters *pxParameters;\r
170 volatile unsigned long *pulSharedVariable, ulExpectedValue;\r
171 unsigned long ulCounter;\r
172 short sError = pdFALSE, sCheckVariableToUse;\r
173 \r
174         /* See which check variable to use.  sNextCheckVariable is not semaphore \r
175         protected! */\r
176         portENTER_CRITICAL();\r
177                 sCheckVariableToUse = sNextCheckVariable;\r
178                 sNextCheckVariable++;\r
179         portEXIT_CRITICAL();\r
180 \r
181         /* Queue a message for printing to say the task has started. */\r
182         vPrintDisplayMessage( &pcSemaphoreTaskStart );\r
183 \r
184         /* A structure is passed in as the parameter.  This contains the shared \r
185         variable being guarded. */\r
186         pxParameters = ( xSemaphoreParameters * ) pvParameters;\r
187         pulSharedVariable = pxParameters->pulSharedVariable;\r
188 \r
189         /* If we are blocking we use a much higher count to ensure loads of context\r
190         switches occur during the count. */\r
191         if( pxParameters->xBlockTime > ( TickType_t ) 0 )\r
192         {\r
193                 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;\r
194         }\r
195         else\r
196         {\r
197                 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;\r
198         }\r
199 \r
200         for( ;; )\r
201         {\r
202                 /* Try to obtain the semaphore. */\r
203                 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )\r
204                 {\r
205                         /* We have the semaphore and so expect any other tasks using the\r
206                         shared variable to have left it in the state we expect to find\r
207                         it. */\r
208                         if( *pulSharedVariable != ulExpectedValue )\r
209                         {\r
210                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
211                                 sError = pdTRUE;\r
212                         }\r
213                         \r
214                         /* Clear the variable, then count it back up to the expected value\r
215                         before releasing the semaphore.  Would expect a context switch or\r
216                         two during this time. */\r
217                         for( ulCounter = ( unsigned long ) 0; ulCounter <= ulExpectedValue; ulCounter++ )\r
218                         {\r
219                                 *pulSharedVariable = ulCounter;\r
220                                 if( *pulSharedVariable != ulCounter )\r
221                                 {\r
222                                         if( sError == pdFALSE )\r
223                                         {\r
224                                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
225                                         }\r
226                                         sError = pdTRUE;\r
227                                 }\r
228                         }\r
229 \r
230                         /* Release the semaphore, and if no errors have occurred increment the check\r
231                         variable. */\r
232                         if(     xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )\r
233                         {\r
234                                 vPrintDisplayMessage( &pcPollingSemaphoreTaskError );\r
235                                 sError = pdTRUE;\r
236                         }\r
237 \r
238                         if( sError == pdFALSE )\r
239                         {\r
240                                 if( sCheckVariableToUse < semtstNUM_TASKS )\r
241                                 {\r
242                                         ( sCheckVariables[ sCheckVariableToUse ] )++;\r
243                                 }\r
244                         }\r
245 \r
246                         /* If we have a block time then we are running at a priority higher\r
247                         than the idle priority.  This task takes a long time to complete\r
248                         a cycle (deliberately so to test the guarding) so will be starving\r
249                         out lower priority tasks.  Block for some time to allow give lower\r
250                         priority tasks some processor time. */\r
251                         vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );\r
252                 }\r
253                 else\r
254                 {\r
255                         if( pxParameters->xBlockTime == ( TickType_t ) 0 )\r
256                         {\r
257                                 /* We have not got the semaphore yet, so no point using the\r
258                                 processor.  We are not blocking when attempting to obtain the\r
259                                 semaphore. */\r
260                                 taskYIELD();\r
261                         }\r
262                 }\r
263         }\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 /* This is called to check that all the created tasks are still running. */\r
268 portBASE_TYPE xAreSemaphoreTasksStillRunning( void )\r
269 {\r
270 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };\r
271 portBASE_TYPE xTask, xReturn = pdTRUE;\r
272 \r
273         for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )\r
274         {\r
275                 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )\r
276                 {\r
277                         xReturn = pdFALSE;\r
278                 }\r
279 \r
280                 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];\r
281         }\r
282 \r
283         return xReturn;\r
284 }\r
285 \r
286 \r