root/webserver/example/freeRTOSexample/Demo/Common/Minimal/death.c

Revision 14, 8.3 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

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