]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/death.c
Update to V6.1.1
[freertos] / Demo / Common / Minimal / death.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /**\r
55  * Create a single persistent task which periodically dynamically creates another\r
56  * two tasks.  The original task is called the creator task, the two tasks it\r
57  * creates are called suicidal tasks.\r
58  *\r
59  * One of the created suicidal tasks kill one other suicidal task before killing\r
60  * itself - leaving just the original task remaining.\r
61  *\r
62  * The creator task must be spawned after all of the other demo application tasks\r
63  * as it keeps a check on the number of tasks under the scheduler control.  The\r
64  * number of tasks it expects to see running should never be greater than the\r
65  * number of tasks that were in existence when the creator task was spawned, plus\r
66  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
67  *\r
68  * \page DeathC death.c\r
69  * \ingroup DemoFiles\r
70  * <HR>\r
71  */\r
72 \r
73 /*\r
74 Changes from V3.0.0\r
75         + CreationCount sizes changed from unsigned portBASE_TYPE to\r
76           unsigned short to minimize the risk of overflowing.\r
77         \r
78         + Reset of usLastCreationCount added\r
79         \r
80 Changes from V3.1.0\r
81         + Changed the dummy calculation to use variables of type long, rather than\r
82           float.  This allows the file to be used with ports that do not support\r
83           floating point.\r
84 \r
85 */\r
86 \r
87 #include <stdlib.h>\r
88 \r
89 /* Scheduler include files. */\r
90 #include "FreeRTOS.h"\r
91 #include "task.h"\r
92 \r
93 /* Demo program include files. */\r
94 #include "death.h"\r
95 \r
96 #define deathSTACK_SIZE         ( configMINIMAL_STACK_SIZE + 60 )\r
97 \r
98 /* The task originally created which is responsible for periodically dynamically\r
99 creating another four tasks. */\r
100 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );\r
101 \r
102 /* The task function of the dynamically created tasks. */\r
103 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );\r
104 \r
105 /* A variable which is incremented every time the dynamic tasks are created.  This\r
106 is used to check that the task is still running. */\r
107 static volatile unsigned short usCreationCount = 0;\r
108 \r
109 /* Used to store the number of tasks that were originally running so the creator\r
110 task can tell if any of the suicidal tasks have failed to die.\r
111 */\r
112 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;\r
113 \r
114 /* Tasks are deleted by the idle task.  Under heavy load the idle task might\r
115 not get much processing time, so it would be legitimate for several tasks to\r
116 remain undeleted for a short period. */\r
117 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;\r
118 \r
119 /* Used to store a handle to the task that should be killed by a suicidal task,\r
120 before it kills itself. */\r
121 xTaskHandle xCreatedTask;\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )\r
126 {\r
127 unsigned portBASE_TYPE *puxPriority;\r
128 \r
129         /* Create the Creator tasks - passing in as a parameter the priority at which\r
130         the suicidal tasks should be created. */\r
131         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );\r
132         *puxPriority = uxPriority;\r
133 \r
134         xTaskCreate( vCreateTasks, ( signed char * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
135 \r
136         /* Record the number of tasks that are running now so we know if any of the\r
137         suicidal tasks have failed to be killed. */\r
138         uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
139         \r
140         /* FreeRTOS.org versions before V3.0 started the idle-task as the very\r
141         first task. The idle task was then already included in uxTasksRunningAtStart.\r
142         From FreeRTOS V3.0 on, the idle task is started when the scheduler is\r
143         started. Therefore the idle task is not yet accounted for. We correct\r
144         this by increasing uxTasksRunningAtStart by 1. */\r
145         uxTasksRunningAtStart++;\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148                                         \r
149 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
150 {\r
151 volatile long l1, l2;\r
152 xTaskHandle xTaskToKill;\r
153 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
154 \r
155         if( pvParameters != NULL )\r
156         {\r
157                 /* This task is periodically created four times.  Two created tasks are\r
158                 passed a handle to the other task so it can kill it before killing itself.\r
159                 The other task is passed in null. */\r
160                 xTaskToKill = *( xTaskHandle* )pvParameters;\r
161         }\r
162         else\r
163         {\r
164                 xTaskToKill = NULL;\r
165         }\r
166 \r
167         for( ;; )\r
168         {\r
169                 /* Do something random just to use some stack and registers. */\r
170                 l1 = 2;\r
171                 l2 = 89;\r
172                 l2 *= l1;\r
173                 vTaskDelay( xDelay );\r
174 \r
175                 if( xTaskToKill != NULL )\r
176                 {\r
177                         /* Make sure the other task has a go before we delete it. */\r
178                         vTaskDelay( ( portTickType ) 0 );\r
179 \r
180                         /* Kill the other task that was created by vCreateTasks(). */\r
181                         vTaskDelete( xTaskToKill );\r
182 \r
183                         /* Kill ourselves. */\r
184                         vTaskDelete( NULL );\r
185                 }\r
186         }\r
187 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
191 {\r
192 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;\r
193 unsigned portBASE_TYPE uxPriority;\r
194 \r
195         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
196         vPortFree( pvParameters );\r
197 \r
198         for( ;; )\r
199         {\r
200                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
201                 vTaskDelay( xDelay );\r
202 \r
203                 xCreatedTask = NULL;\r
204 \r
205                 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );\r
206                 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );\r
207 \r
208                 ++usCreationCount;\r
209         }\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /* This is called to check that the creator task is still running and that there\r
214 are not any more than four extra tasks. */\r
215 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
216 {\r
217 static unsigned short usLastCreationCount = 0xfff;\r
218 portBASE_TYPE xReturn = pdTRUE;\r
219 static unsigned portBASE_TYPE uxTasksRunningNow;\r
220 \r
221         if( usLastCreationCount == usCreationCount )\r
222         {\r
223                 xReturn = pdFALSE;\r
224         }\r
225         else\r
226         {\r
227                 usLastCreationCount = usCreationCount;\r
228         }\r
229         \r
230         uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
231 \r
232         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
233         {\r
234                 xReturn = pdFALSE;\r
235         }\r
236         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
237         {\r
238                 xReturn = pdFALSE;\r
239         }\r
240         else\r
241         {\r
242                 /* Everything is okay. */\r
243         }\r
244 \r
245         return xReturn;\r
246 }\r
247 \r
248 \r