]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/death.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / Common / Minimal / death.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /**\r
68  * Create a single persistent task which periodically dynamically creates another\r
69  * two tasks.  The original task is called the creator task, the two tasks it\r
70  * creates are called suicidal tasks.\r
71  *\r
72  * One of the created suicidal tasks kill one other suicidal task before killing\r
73  * itself - leaving just the original task remaining.\r
74  *\r
75  * The creator task must be spawned after all of the other demo application tasks\r
76  * as it keeps a check on the number of tasks under the scheduler control.  The\r
77  * number of tasks it expects to see running should never be greater than the\r
78  * number of tasks that were in existence when the creator task was spawned, plus\r
79  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
80  *\r
81  * \page DeathC death.c\r
82  * \ingroup DemoFiles\r
83  * <HR>\r
84  */\r
85 \r
86 /*\r
87 Changes from V3.0.0\r
88         + CreationCount sizes changed from unsigned portBASE_TYPE to\r
89           unsigned short to minimize the risk of overflowing.\r
90         \r
91         + Reset of usLastCreationCount added\r
92         \r
93 Changes from V3.1.0\r
94         + Changed the dummy calculation to use variables of type long, rather than\r
95           float.  This allows the file to be used with ports that do not support\r
96           floating point.\r
97 \r
98 */\r
99 \r
100 #include <stdlib.h>\r
101 \r
102 /* Scheduler include files. */\r
103 #include "FreeRTOS.h"\r
104 #include "task.h"\r
105 \r
106 /* Demo program include files. */\r
107 #include "death.h"\r
108 \r
109 #define deathSTACK_SIZE         ( configMINIMAL_STACK_SIZE + 60 )\r
110 \r
111 /* The task originally created which is responsible for periodically dynamically\r
112 creating another four tasks. */\r
113 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );\r
114 \r
115 /* The task function of the dynamically created tasks. */\r
116 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );\r
117 \r
118 /* A variable which is incremented every time the dynamic tasks are created.  This\r
119 is used to check that the task is still running. */\r
120 static volatile unsigned short usCreationCount = 0;\r
121 \r
122 /* Used to store the number of tasks that were originally running so the creator\r
123 task can tell if any of the suicidal tasks have failed to die.\r
124 */\r
125 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;\r
126 \r
127 /* Tasks are deleted by the idle task.  Under heavy load the idle task might\r
128 not get much processing time, so it would be legitimate for several tasks to\r
129 remain undeleted for a short period. */\r
130 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;\r
131 \r
132 /* Used to store a handle to the task that should be killed by a suicidal task,\r
133 before it kills itself. */\r
134 xTaskHandle xCreatedTask;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )\r
139 {\r
140 unsigned portBASE_TYPE *puxPriority;\r
141 \r
142         /* Create the Creator tasks - passing in as a parameter the priority at which\r
143         the suicidal tasks should be created. */\r
144         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );\r
145         *puxPriority = uxPriority;\r
146 \r
147         xTaskCreate( vCreateTasks, ( signed char * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
148 \r
149         /* Record the number of tasks that are running now so we know if any of the\r
150         suicidal tasks have failed to be killed. */\r
151         uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
152         \r
153         /* FreeRTOS.org versions before V3.0 started the idle-task as the very\r
154         first task. The idle task was then already included in uxTasksRunningAtStart.\r
155         From FreeRTOS V3.0 on, the idle task is started when the scheduler is\r
156         started. Therefore the idle task is not yet accounted for. We correct\r
157         this by increasing uxTasksRunningAtStart by 1. */\r
158         uxTasksRunningAtStart++;\r
159         \r
160         /* From FreeRTOS version 7.0.0 can optionally create a timer service task.  \r
161         If this is done, then uxTasksRunningAtStart needs incrementing again as that\r
162         too is created when the scheduler is started. */\r
163         #if configUSE_TIMERS == 1\r
164                 uxTasksRunningAtStart++;\r
165         #endif\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168                                         \r
169 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
170 {\r
171 volatile long l1, l2;\r
172 xTaskHandle xTaskToKill;\r
173 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
174 \r
175         if( pvParameters != NULL )\r
176         {\r
177                 /* This task is periodically created four times.  Two created tasks are\r
178                 passed a handle to the other task so it can kill it before killing itself.\r
179                 The other task is passed in null. */\r
180                 xTaskToKill = *( xTaskHandle* )pvParameters;\r
181         }\r
182         else\r
183         {\r
184                 xTaskToKill = NULL;\r
185         }\r
186 \r
187         for( ;; )\r
188         {\r
189                 /* Do something random just to use some stack and registers. */\r
190                 l1 = 2;\r
191                 l2 = 89;\r
192                 l2 *= l1;\r
193                 vTaskDelay( xDelay );\r
194 \r
195                 if( xTaskToKill != NULL )\r
196                 {\r
197                         /* Make sure the other task has a go before we delete it. */\r
198                         vTaskDelay( ( portTickType ) 0 );\r
199 \r
200                         /* Kill the other task that was created by vCreateTasks(). */\r
201                         vTaskDelete( xTaskToKill );\r
202 \r
203                         /* Kill ourselves. */\r
204                         vTaskDelete( NULL );\r
205                 }\r
206         }\r
207 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
211 {\r
212 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;\r
213 unsigned portBASE_TYPE uxPriority;\r
214 \r
215         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
216         vPortFree( pvParameters );\r
217 \r
218         for( ;; )\r
219         {\r
220                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
221                 vTaskDelay( xDelay );\r
222 \r
223                 xCreatedTask = NULL;\r
224 \r
225                 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );\r
226                 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );\r
227 \r
228                 ++usCreationCount;\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /* This is called to check that the creator task is still running and that there\r
234 are not any more than four extra tasks. */\r
235 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
236 {\r
237 static unsigned short usLastCreationCount = 0xfff;\r
238 portBASE_TYPE xReturn = pdTRUE;\r
239 static unsigned portBASE_TYPE uxTasksRunningNow;\r
240 \r
241         if( usLastCreationCount == usCreationCount )\r
242         {\r
243                 xReturn = pdFALSE;\r
244         }\r
245         else\r
246         {\r
247                 usLastCreationCount = usCreationCount;\r
248         }\r
249         \r
250         uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();\r
251 \r
252         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
253         {\r
254                 xReturn = pdFALSE;\r
255         }\r
256         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
257         {\r
258                 xReturn = pdFALSE;\r
259         }\r
260         else\r
261         {\r
262                 /* Everything is okay. */\r
263         }\r
264 \r
265         return xReturn;\r
266 }\r
267 \r
268 \r