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