]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/death.c
e98e0fb9bb80d43462852555bcba0303dfd84cb2
[freertos] / FreeRTOS / Demo / Common / Minimal / death.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /**\r
67  * Create a single persistent task which periodically dynamically creates another\r
68  * two tasks.  The original task is called the creator task, the two tasks it\r
69  * creates are called suicidal tasks.\r
70  *\r
71  * One of the created suicidal tasks kill one other suicidal task before killing\r
72  * itself - leaving just the original task remaining.\r
73  *\r
74  * The creator task must be spawned after all of the other demo application tasks\r
75  * as it keeps a check on the number of tasks under the scheduler control.  The\r
76  * number of tasks it expects to see running should never be greater than the\r
77  * number of tasks that were in existence when the creator task was spawned, plus\r
78  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
79  *\r
80  * \page DeathC death.c\r
81  * \ingroup DemoFiles\r
82  * <HR>\r
83  */\r
84 \r
85 /*\r
86 Changes from V3.0.0\r
87         + CreationCount sizes changed from unsigned portBASE_TYPE to\r
88           unsigned short to minimize the risk of overflowing.\r
89         \r
90         + Reset of usLastCreationCount added\r
91         \r
92 Changes from V3.1.0\r
93         + Changed the dummy calculation to use variables of type long, rather than\r
94           float.  This allows the file to be used with ports that do not support\r
95           floating point.\r
96 \r
97 */\r
98 \r
99 #include <stdlib.h>\r
100 \r
101 /* Scheduler include files. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 \r
105 /* Demo program include files. */\r
106 #include "death.h"\r
107 \r
108 #define deathSTACK_SIZE         ( configMINIMAL_STACK_SIZE + 60 )\r
109 \r
110 /* The task originally created which is responsible for periodically dynamically\r
111 creating another four tasks. */\r
112 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );\r
113 \r
114 /* The task function of the dynamically created tasks. */\r
115 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );\r
116 \r
117 /* A variable which is incremented every time the dynamic tasks are created.  This\r
118 is used to check that the task is still running. */\r
119 static volatile unsigned short usCreationCount = 0;\r
120 \r
121 /* Used to store the number of tasks that were originally running so the creator\r
122 task can tell if any of the suicidal tasks have failed to die.\r
123 */\r
124 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;\r
125 \r
126 /* Tasks are deleted by the idle task.  Under heavy load the idle task might\r
127 not get much processing time, so it would be legitimate for several tasks to\r
128 remain undeleted for a short period. */\r
129 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 3;\r
130 \r
131 /* Used to store a handle to the task that should be killed by a suicidal task,\r
132 before it kills itself. */\r
133 TaskHandle_t xCreatedTask;\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )\r
138 {\r
139 unsigned portBASE_TYPE *puxPriority;\r
140 \r
141         /* Create the Creator tasks - passing in as a parameter the priority at which\r
142         the suicidal tasks should be created. */\r
143         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );\r
144         *puxPriority = uxPriority;\r
145 \r
146         xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
147 \r
148         /* Record the number of tasks that are running now so we know if any of the\r
149         suicidal tasks have failed to be killed. */\r
150         uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
151         \r
152         /* FreeRTOS.org versions before V3.0 started the idle-task as the very\r
153         first task. The idle task was then already included in uxTasksRunningAtStart.\r
154         From FreeRTOS V3.0 on, the idle task is started when the scheduler is\r
155         started. Therefore the idle task is not yet accounted for. We correct\r
156         this by increasing uxTasksRunningAtStart by 1. */\r
157         uxTasksRunningAtStart++;\r
158         \r
159         /* From FreeRTOS version 7.0.0 can optionally create a timer service task.  \r
160         If this is done, then uxTasksRunningAtStart needs incrementing again as that\r
161         too is created when the scheduler is started. */\r
162         #if configUSE_TIMERS == 1\r
163                 uxTasksRunningAtStart++;\r
164         #endif\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167                                         \r
168 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
169 {\r
170 volatile long l1, l2;\r
171 TaskHandle_t xTaskToKill;\r
172 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;\r
173 \r
174         if( pvParameters != NULL )\r
175         {\r
176                 /* This task is periodically created four times.  Two created tasks are\r
177                 passed a handle to the other task so it can kill it before killing itself.\r
178                 The other task is passed in null. */\r
179                 xTaskToKill = *( TaskHandle_t* )pvParameters;\r
180         }\r
181         else\r
182         {\r
183                 xTaskToKill = NULL;\r
184         }\r
185 \r
186         for( ;; )\r
187         {\r
188                 /* Do something random just to use some stack and registers. */\r
189                 l1 = 2;\r
190                 l2 = 89;\r
191                 l2 *= l1;\r
192                 vTaskDelay( xDelay );\r
193 \r
194                 if( xTaskToKill != NULL )\r
195                 {\r
196                         /* Make sure the other task has a go before we delete it. */\r
197                         vTaskDelay( ( TickType_t ) 0 );\r
198 \r
199                         /* Kill the other task that was created by vCreateTasks(). */\r
200                         vTaskDelete( xTaskToKill );\r
201 \r
202                         /* Kill ourselves. */\r
203                         vTaskDelete( NULL );\r
204                 }\r
205         }\r
206 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
210 {\r
211 const TickType_t xDelay = ( TickType_t ) 1000 / portTICK_PERIOD_MS;\r
212 unsigned portBASE_TYPE uxPriority;\r
213 \r
214         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
215         vPortFree( pvParameters );\r
216 \r
217         for( ;; )\r
218         {\r
219                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
220                 vTaskDelay( xDelay );\r
221 \r
222                 xCreatedTask = NULL;\r
223 \r
224                 xTaskCreate( vSuicidalTask, "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );\r
225                 xTaskCreate( vSuicidalTask, "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );\r
226 \r
227                 ++usCreationCount;\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 /* This is called to check that the creator task is still running and that there\r
233 are not any more than four extra tasks. */\r
234 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
235 {\r
236 static unsigned short usLastCreationCount = 0xfff;\r
237 portBASE_TYPE xReturn = pdTRUE;\r
238 static unsigned portBASE_TYPE uxTasksRunningNow;\r
239 \r
240         if( usLastCreationCount == usCreationCount )\r
241         {\r
242                 xReturn = pdFALSE;\r
243         }\r
244         else\r
245         {\r
246                 usLastCreationCount = usCreationCount;\r
247         }\r
248         \r
249         uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
250 \r
251         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
252         {\r
253                 xReturn = pdFALSE;\r
254         }\r
255         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
256         {\r
257                 xReturn = pdFALSE;\r
258         }\r
259         else\r
260         {\r
261                 /* Everything is okay. */\r
262         }\r
263 \r
264         return xReturn;\r
265 }\r
266 \r
267 \r