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