]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/death.c
Update to V4.1.0.
[freertos] / Demo / Common / Minimal / death.c
1 /*\r
2         FreeRTOS.org V4.1.0 - Copyright (C) 2003-2006 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         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 /**\r
34  * Create a single persistent task which periodically dynamically creates another\r
35  * four tasks.  The original task is called the creator task, the four tasks it\r
36  * creates are called suicidal tasks.\r
37  *\r
38  * Two of the created suicidal tasks kill one other suicidal task before killing\r
39  * themselves - leaving just the original task remaining.\r
40  *\r
41  * The creator task must be spawned after all of the other demo application tasks\r
42  * as it keeps a check on the number of tasks under the scheduler control.  The\r
43  * number of tasks it expects to see running should never be greater than the\r
44  * number of tasks that were in existence when the creator task was spawned, plus\r
45  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
46  *\r
47  * \page DeathC death.c\r
48  * \ingroup DemoFiles\r
49  * <HR>\r
50  */\r
51 \r
52 /*\r
53 Changes from V3.0.0\r
54         + CreationCount sizes changed from unsigned portBASE_TYPE to\r
55           unsigned portSHORT to minimize the risk of overflowing.\r
56         \r
57         + Reset of usLastCreationCount added\r
58         \r
59 Changes from V3.1.0\r
60         + Changed the dummy calculation to use variables of type long, rather than\r
61           float.  This allows the file to be used with ports that do not support\r
62           floating point.\r
63 \r
64 */\r
65 \r
66 #include <stdlib.h>\r
67 \r
68 /* Scheduler include files. */\r
69 #include "FreeRTOS.h"\r
70 #include "task.h"\r
71 \r
72 /* Demo program include files. */\r
73 #include "death.h"\r
74 \r
75 #define deathSTACK_SIZE         ( configMINIMAL_STACK_SIZE + 24 )\r
76 \r
77 /* The task originally created which is responsible for periodically dynamically\r
78 creating another four tasks. */\r
79 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );\r
80 \r
81 /* The task function of the dynamically created tasks. */\r
82 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );\r
83 \r
84 /* A variable which is incremented every time the dynamic tasks are created.  This\r
85 is used to check that the task is still running. */\r
86 static volatile unsigned portSHORT usCreationCount = 0;\r
87 \r
88 /* Used to store the number of tasks that were originally running so the creator\r
89 task can tell if any of the suicidal tasks have failed to die.\r
90 */\r
91 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;\r
92 \r
93 /* Tasks are deleted by the idle task.  Under heavy load the idle task might\r
94 not get much processing time, so it would be legitimate for several tasks to\r
95 remain undeleted for a short period. */\r
96 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 4;\r
97 \r
98 /* Used to store a handle to the tasks that should be killed by a suicidal task,\r
99 before it kills itself. */\r
100 xTaskHandle xCreatedTask1, xCreatedTask2;\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )\r
105 {\r
106 unsigned portBASE_TYPE *puxPriority;\r
107 \r
108         /* Create the Creator tasks - passing in as a parameter the priority at which\r
109         the suicidal tasks should be created. */\r
110         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );\r
111         *puxPriority = uxPriority;\r
112 \r
113         xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
114 \r
115         /* Record the number of tasks that are running now so we know if any of the\r
116         suicidal tasks have failed to be killed. */\r
117         uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
118         \r
119         /* FreeRTOS.org versions before V3.0 started the idle-task as the very\r
120         first task. The idle task was then already included in uxTasksRunningAtStart.\r
121         From FreeRTOS V3.0 on, the idle task is started when the scheduler is\r
122         started. Therefore the idle task is not yet accounted for. We correct\r
123         this by increasing uxTasksRunningAtStart by 1. */\r
124         uxTasksRunningAtStart++;\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127                                         \r
128 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
129 {\r
130 volatile portLONG l1, l2;\r
131 xTaskHandle xTaskToKill;\r
132 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
133 \r
134         if( pvParameters != NULL )\r
135         {\r
136                 /* This task is periodically created four times.  Two created tasks are\r
137                 passed a handle to the other task so it can kill it before killing itself.\r
138                 The other task is passed in null. */\r
139                 xTaskToKill = *( xTaskHandle* )pvParameters;\r
140         }\r
141         else\r
142         {\r
143                 xTaskToKill = NULL;\r
144         }\r
145 \r
146         for( ;; )\r
147         {\r
148                 /* Do something random just to use some stack and registers. */\r
149                 l1 = 2;\r
150                 l2 = 89;\r
151                 l2 *= l1;\r
152                 vTaskDelay( xDelay );\r
153 \r
154                 if( xTaskToKill != NULL )\r
155                 {\r
156                         /* Make sure the other task has a go before we delete it. */\r
157                         vTaskDelay( ( portTickType ) 0 );\r
158                         /* Kill the other task that was created by vCreateTasks(). */\r
159                         vTaskDelete( xTaskToKill );\r
160                         /* Kill ourselves. */\r
161                         vTaskDelete( NULL );\r
162                 }\r
163         }\r
164 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
168 {\r
169 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;\r
170 unsigned portBASE_TYPE uxPriority;\r
171 \r
172         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
173         vPortFree( pvParameters );\r
174 \r
175         for( ;; )\r
176         {\r
177                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
178                 vTaskDelay( xDelay );\r
179 \r
180                 xTaskCreate( vSuicidalTask, "SUICID1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );\r
181                 xTaskCreate( vSuicidalTask, "SUICID2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );\r
182 \r
183                 xTaskCreate( vSuicidalTask, "SUICID1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );\r
184                 xTaskCreate( vSuicidalTask, "SUICID2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );\r
185 \r
186                 ++usCreationCount;\r
187         }\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 /* This is called to check that the creator task is still running and that there\r
192 are not any more than four extra tasks. */\r
193 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
194 {\r
195 static portSHORT usLastCreationCount = -1;\r
196 portBASE_TYPE xReturn = pdTRUE;\r
197 static unsigned portBASE_TYPE uxTasksRunningNow;\r
198 \r
199         if( usLastCreationCount == usCreationCount )\r
200         {\r
201                 xReturn = pdFALSE;\r
202         }\r
203         else\r
204         {\r
205                 usLastCreationCount = usCreationCount;\r
206         }\r
207         \r
208         uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
209 \r
210         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
211         {\r
212                 xReturn = pdFALSE;\r
213         }\r
214         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
215         {\r
216                 xReturn = pdFALSE;\r
217         }\r
218         else\r
219         {\r
220                 /* Everything is okay. */\r
221         }\r
222 \r
223         return xReturn;\r
224 }\r
225 \r
226 \r