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