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