]> begriffs open source - freertos/blob - Demo/Common/Minimal/death.c
Update to V5.4.2. See http://www.freertos.org/History.txt .
[freertos] / Demo / Common / Minimal / death.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /**\r
49  * Create a single persistent task which periodically dynamically creates another\r
50  * two tasks.  The original task is called the creator task, the two tasks it\r
51  * creates are called suicidal tasks.\r
52  *\r
53  * One of the created suicidal tasks kill one other suicidal task before killing\r
54  * itself - leaving just the original task remaining.\r
55  *\r
56  * The creator task must be spawned after all of the other demo application tasks\r
57  * as it keeps a check on the number of tasks under the scheduler control.  The\r
58  * number of tasks it expects to see running should never be greater than the\r
59  * number of tasks that were in existence when the creator task was spawned, plus\r
60  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
61  *\r
62  * \page DeathC death.c\r
63  * \ingroup DemoFiles\r
64  * <HR>\r
65  */\r
66 \r
67 /*\r
68 Changes from V3.0.0\r
69         + CreationCount sizes changed from unsigned portBASE_TYPE to\r
70           unsigned portSHORT to minimize the risk of overflowing.\r
71         \r
72         + Reset of usLastCreationCount added\r
73         \r
74 Changes from V3.1.0\r
75         + Changed the dummy calculation to use variables of type long, rather than\r
76           float.  This allows the file to be used with ports that do not support\r
77           floating point.\r
78 \r
79 */\r
80 \r
81 #include <stdlib.h>\r
82 \r
83 /* Scheduler include files. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 \r
87 /* Demo program include files. */\r
88 #include "death.h"\r
89 \r
90 #define deathSTACK_SIZE         ( configMINIMAL_STACK_SIZE + 60 )\r
91 \r
92 /* The task originally created which is responsible for periodically dynamically\r
93 creating another four tasks. */\r
94 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );\r
95 \r
96 /* The task function of the dynamically created tasks. */\r
97 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );\r
98 \r
99 /* A variable which is incremented every time the dynamic tasks are created.  This\r
100 is used to check that the task is still running. */\r
101 static volatile unsigned portSHORT usCreationCount = 0;\r
102 \r
103 /* Used to store the number of tasks that were originally running so the creator\r
104 task can tell if any of the suicidal tasks have failed to die.\r
105 */\r
106 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;\r
107 \r
108 /* Tasks are deleted by the idle task.  Under heavy load the idle task might\r
109 not get much processing time, so it would be legitimate for several tasks to\r
110 remain undeleted for a short period. */\r
111 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;\r
112 \r
113 /* Used to store a handle to the task that should be killed by a suicidal task,\r
114 before it kills itself. */\r
115 xTaskHandle xCreatedTask;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )\r
120 {\r
121 unsigned portBASE_TYPE *puxPriority;\r
122 \r
123         /* Create the Creator tasks - passing in as a parameter the priority at which\r
124         the suicidal tasks should be created. */\r
125         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );\r
126         *puxPriority = uxPriority;\r
127 \r
128         xTaskCreate( vCreateTasks, ( signed portCHAR * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
129 \r
130         /* Record the number of tasks that are running now so we know if any of the\r
131         suicidal tasks have failed to be killed. */\r
132         uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
133         \r
134         /* FreeRTOS.org versions before V3.0 started the idle-task as the very\r
135         first task. The idle task was then already included in uxTasksRunningAtStart.\r
136         From FreeRTOS V3.0 on, the idle task is started when the scheduler is\r
137         started. Therefore the idle task is not yet accounted for. We correct\r
138         this by increasing uxTasksRunningAtStart by 1. */\r
139         uxTasksRunningAtStart++;\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142                                         \r
143 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
144 {\r
145 volatile portLONG l1, l2;\r
146 xTaskHandle xTaskToKill;\r
147 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
148 \r
149         if( pvParameters != NULL )\r
150         {\r
151                 /* This task is periodically created four times.  Two created tasks are\r
152                 passed a handle to the other task so it can kill it before killing itself.\r
153                 The other task is passed in null. */\r
154                 xTaskToKill = *( xTaskHandle* )pvParameters;\r
155         }\r
156         else\r
157         {\r
158                 xTaskToKill = NULL;\r
159         }\r
160 \r
161         for( ;; )\r
162         {\r
163                 /* Do something random just to use some stack and registers. */\r
164                 l1 = 2;\r
165                 l2 = 89;\r
166                 l2 *= l1;\r
167                 vTaskDelay( xDelay );\r
168 \r
169                 if( xTaskToKill != NULL )\r
170                 {\r
171                         /* Make sure the other task has a go before we delete it. */\r
172                         vTaskDelay( ( portTickType ) 0 );\r
173 \r
174                         /* Kill the other task that was created by vCreateTasks(). */\r
175                         vTaskDelete( xTaskToKill );\r
176 \r
177                         /* Kill ourselves. */\r
178                         vTaskDelete( NULL );\r
179                 }\r
180         }\r
181 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
185 {\r
186 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;\r
187 unsigned portBASE_TYPE uxPriority;\r
188 \r
189         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
190         vPortFree( pvParameters );\r
191 \r
192         for( ;; )\r
193         {\r
194                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
195                 vTaskDelay( xDelay );\r
196 \r
197                 xCreatedTask = NULL;\r
198 \r
199                 xTaskCreate( vSuicidalTask, ( signed portCHAR * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );\r
200                 xTaskCreate( vSuicidalTask, ( signed portCHAR * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );\r
201 \r
202                 ++usCreationCount;\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 /* This is called to check that the creator task is still running and that there\r
208 are not any more than four extra tasks. */\r
209 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
210 {\r
211 static unsigned portSHORT usLastCreationCount = 0xfff;\r
212 portBASE_TYPE xReturn = pdTRUE;\r
213 static unsigned portBASE_TYPE uxTasksRunningNow;\r
214 \r
215         if( usLastCreationCount == usCreationCount )\r
216         {\r
217                 xReturn = pdFALSE;\r
218         }\r
219         else\r
220         {\r
221                 usLastCreationCount = usCreationCount;\r
222         }\r
223         \r
224         uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
225 \r
226         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
227         {\r
228                 xReturn = pdFALSE;\r
229         }\r
230         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
231         {\r
232                 xReturn = pdFALSE;\r
233         }\r
234         else\r
235         {\r
236                 /* Everything is okay. */\r
237         }\r
238 \r
239         return xReturn;\r
240 }\r
241 \r
242 \r