]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/death.c
Change Demo/Common/Minimal/death.c so it takes into account the timer daemon task...
[freertos] / Demo / Common / Minimal / death.c
1 /*\r
2     FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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         /* FreeRTOS version 7.0.0 can optionally create a timer service task.  If\r
148         this is done, then uxTasksRunningAtStart needs incrementing again as that\r
149         too is created when the scheduler is started. */\r
150         #if configUSE_TIMERS == 1\r
151                 uxTasksRunningAtStart++;\r
152         #endif\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155                                         \r
156 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
157 {\r
158 volatile long l1, l2;\r
159 xTaskHandle xTaskToKill;\r
160 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
161 \r
162         if( pvParameters != NULL )\r
163         {\r
164                 /* This task is periodically created four times.  Two created tasks are\r
165                 passed a handle to the other task so it can kill it before killing itself.\r
166                 The other task is passed in null. */\r
167                 xTaskToKill = *( xTaskHandle* )pvParameters;\r
168         }\r
169         else\r
170         {\r
171                 xTaskToKill = NULL;\r
172         }\r
173 \r
174         for( ;; )\r
175         {\r
176                 /* Do something random just to use some stack and registers. */\r
177                 l1 = 2;\r
178                 l2 = 89;\r
179                 l2 *= l1;\r
180                 vTaskDelay( xDelay );\r
181 \r
182                 if( xTaskToKill != NULL )\r
183                 {\r
184                         /* Make sure the other task has a go before we delete it. */\r
185                         vTaskDelay( ( portTickType ) 0 );\r
186 \r
187                         /* Kill the other task that was created by vCreateTasks(). */\r
188                         vTaskDelete( xTaskToKill );\r
189 \r
190                         /* Kill ourselves. */\r
191                         vTaskDelete( NULL );\r
192                 }\r
193         }\r
194 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
198 {\r
199 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;\r
200 unsigned portBASE_TYPE uxPriority;\r
201 \r
202         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
203         vPortFree( pvParameters );\r
204 \r
205         for( ;; )\r
206         {\r
207                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
208                 vTaskDelay( xDelay );\r
209 \r
210                 xCreatedTask = NULL;\r
211 \r
212                 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );\r
213                 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );\r
214 \r
215                 ++usCreationCount;\r
216         }\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 /* This is called to check that the creator task is still running and that there\r
221 are not any more than four extra tasks. */\r
222 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
223 {\r
224 static unsigned short usLastCreationCount = 0xfff;\r
225 portBASE_TYPE xReturn = pdTRUE;\r
226 static unsigned portBASE_TYPE uxTasksRunningNow;\r
227 \r
228         if( usLastCreationCount == usCreationCount )\r
229         {\r
230                 xReturn = pdFALSE;\r
231         }\r
232         else\r
233         {\r
234                 usLastCreationCount = usCreationCount;\r
235         }\r
236         \r
237         uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
238 \r
239         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
240         {\r
241                 xReturn = pdFALSE;\r
242         }\r
243         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
244         {\r
245                 xReturn = pdFALSE;\r
246         }\r
247         else\r
248         {\r
249                 /* Everything is okay. */\r
250         }\r
251 \r
252         return xReturn;\r
253 }\r
254 \r
255 \r