]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/death.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Minimal / death.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  * Create a single persistent task which periodically dynamically creates another\r
31  * two tasks.  The original task is called the creator task, the two tasks it\r
32  * creates are called suicidal tasks.\r
33  *\r
34  * One of the created suicidal tasks kill one other suicidal task before killing\r
35  * itself - leaving just the original task remaining.\r
36  *\r
37  * The creator task must be spawned after all of the other demo application tasks\r
38  * as it keeps a check on the number of tasks under the scheduler control.  The\r
39  * number of tasks it expects to see running should never be greater than the\r
40  * number of tasks that were in existence when the creator task was spawned, plus\r
41  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
42  *\r
43  * \page DeathC death.c\r
44  * \ingroup DemoFiles\r
45  * <HR>\r
46  */\r
47 \r
48 \r
49 #include <stdlib.h>\r
50 \r
51 /* Scheduler include files. */\r
52 #include "FreeRTOS.h"\r
53 #include "task.h"\r
54 \r
55 /* Demo program include files. */\r
56 #include "death.h"\r
57 \r
58 #define deathSTACK_SIZE         ( configMINIMAL_STACK_SIZE + 60 )\r
59 \r
60 /* The task originally created which is responsible for periodically dynamically\r
61 creating another four tasks. */\r
62 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );\r
63 \r
64 /* The task function of the dynamically created tasks. */\r
65 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );\r
66 \r
67 /* A variable which is incremented every time the dynamic tasks are created.  This\r
68 is used to check that the task is still running. */\r
69 static volatile uint16_t usCreationCount = 0;\r
70 \r
71 /* Used to store the number of tasks that were originally running so the creator\r
72 task can tell if any of the suicidal tasks have failed to die.\r
73 */\r
74 static volatile UBaseType_t uxTasksRunningAtStart = 0;\r
75 \r
76 /* When a task deletes itself, it stack and TCB are cleaned up by the Idle task.\r
77 Under heavy load the idle task might not get much processing time, so it would\r
78 be legitimate for several tasks to remain undeleted for a short period.  There\r
79 may also be a few other unexpected tasks if, for example, the tasks that test\r
80 static allocation are also being used. */\r
81 static const UBaseType_t uxMaxNumberOfExtraTasksRunning = 3;\r
82 \r
83 /* Used to store a handle to the task that should be killed by a suicidal task,\r
84 before it kills itself. */\r
85 TaskHandle_t xCreatedTask;\r
86 \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 void vCreateSuicidalTasks( UBaseType_t uxPriority )\r
90 {\r
91         xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) NULL, uxPriority, NULL );\r
92 }\r
93 /*-----------------------------------------------------------*/\r
94 \r
95 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
96 {\r
97 volatile long l1, l2;\r
98 TaskHandle_t xTaskToKill;\r
99 const TickType_t xDelay = pdMS_TO_TICKS( ( TickType_t ) 200 );\r
100 \r
101         /* Test deletion of a task's secure context, if any. */\r
102         portTASK_CALLS_SECURE_FUNCTIONS();\r
103 \r
104         if( pvParameters != NULL )\r
105         {\r
106                 /* This task is periodically created four times.  Two created tasks are\r
107                 passed a handle to the other task so it can kill it before killing itself.\r
108                 The other task is passed in null. */\r
109                 xTaskToKill = *( TaskHandle_t* )pvParameters;\r
110         }\r
111         else\r
112         {\r
113                 xTaskToKill = NULL;\r
114         }\r
115 \r
116         for( ;; )\r
117         {\r
118                 /* Do something random just to use some stack and registers. */\r
119                 l1 = 2;\r
120                 l2 = 89;\r
121                 l2 *= l1;\r
122                 vTaskDelay( xDelay );\r
123 \r
124                 if( xTaskToKill != NULL )\r
125                 {\r
126                         /* Make sure the other task has a go before we delete it. */\r
127                         vTaskDelay( ( TickType_t ) 0 );\r
128 \r
129                         /* Kill the other task that was created by vCreateTasks(). */\r
130                         vTaskDelete( xTaskToKill );\r
131 \r
132                         /* Kill ourselves. */\r
133                         vTaskDelete( NULL );\r
134                 }\r
135         }\r
136 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
140 {\r
141 const TickType_t xDelay = pdMS_TO_TICKS( ( TickType_t ) 1000 );\r
142 UBaseType_t uxPriority;\r
143 \r
144         /* Remove compiler warning about unused parameter. */\r
145         ( void ) pvParameters;\r
146 \r
147         /* Delay at the start to ensure tasks created by other demos have been\r
148         created before storing the current number of tasks. */\r
149         vTaskDelay( xDelay );\r
150         uxTasksRunningAtStart = ( UBaseType_t ) uxTaskGetNumberOfTasks();\r
151 \r
152         uxPriority = uxTaskPriorityGet( NULL );\r
153 \r
154         for( ;; )\r
155         {\r
156                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
157                 vTaskDelay( xDelay );\r
158 \r
159                 xCreatedTask = NULL;\r
160 \r
161                 xTaskCreate( vSuicidalTask, "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );\r
162                 xTaskCreate( vSuicidalTask, "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );\r
163 \r
164                 ++usCreationCount;\r
165         }\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /* This is called to check that the creator task is still running and that there\r
170 are not any more than four extra tasks. */\r
171 BaseType_t xIsCreateTaskStillRunning( void )\r
172 {\r
173 static uint16_t usLastCreationCount = 0xfff;\r
174 BaseType_t xReturn = pdTRUE;\r
175 static UBaseType_t uxTasksRunningNow;\r
176 \r
177         if( usLastCreationCount == usCreationCount )\r
178         {\r
179                 xReturn = pdFALSE;\r
180         }\r
181         else\r
182         {\r
183                 usLastCreationCount = usCreationCount;\r
184         }\r
185 \r
186         uxTasksRunningNow = ( UBaseType_t ) uxTaskGetNumberOfTasks();\r
187 \r
188         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
189         {\r
190                 xReturn = pdFALSE;\r
191         }\r
192         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
193         {\r
194                 xReturn = pdFALSE;\r
195         }\r
196         else\r
197         {\r
198                 /* Everything is okay. */\r
199         }\r
200 \r
201         return xReturn;\r
202 }\r
203 \r
204 \r