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