root/webserver/example/freeRTOS/Demo/Common/Full/death.c

Revision 14, 7.6 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  * four tasks.  The original task is called the creator task, the four tasks it
51  * creates are called suicidal tasks.
52  *
53  * Two of the created suicidal tasks kill one other suicidal task before killing
54  * themselves - 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 V2.0.0
69
70         + Delay periods are now specified using variables and constants of
71           portTickType rather than unsigned portLONG.
72 */
73
74 #include <stdlib.h>
75
76 /* Scheduler include files. */
77 #include "FreeRTOS.h"
78 #include "task.h"
79
80 /* Demo program include files. */
81 #include "death.h"
82 #include "print.h"
83
84 #define deathSTACK_SIZE         ( ( unsigned portSHORT ) 512 )
85
86 /* The task originally created which is responsible for periodically dynamically
87 creating another four tasks. */
88 static void vCreateTasks( void *pvParameters );
89
90 /* The task function of the dynamically created tasks. */
91 static void vSuicidalTask( void *pvParameters );
92
93 /* A variable which is incremented every time the dynamic tasks are created.  This
94 is used to check that the task is still running. */
95 static volatile portSHORT sCreationCount = 0;
96
97 /* Used to store the number of tasks that were originally running so the creator
98 task can tell if any of the suicidal tasks have failed to die. */
99 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
100 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
101
102 /* Used to store a handle to the tasks that should be killed by a suicidal task,
103 before it kills itself. */
104 xTaskHandle xCreatedTask1, xCreatedTask2;
105
106 /*-----------------------------------------------------------*/
107
108 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
109 {
110 unsigned portBASE_TYPE *puxPriority;
111
112         /* Create the Creator tasks - passing in as a parameter the priority at which
113         the suicidal tasks should be created. */
114         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
115         *puxPriority = uxPriority;
116
117         xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
118
119         /* Record the number of tasks that are running now so we know if any of the
120         suicidal tasks have failed to be killed. */
121         uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
122 }
123 /*-----------------------------------------------------------*/
124
125 static void vSuicidalTask( void *pvParameters )
126 {
127 portDOUBLE d1, d2;
128 xTaskHandle xTaskToKill;
129 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;
130
131         if( pvParameters != NULL )
132         {
133                 /* This task is periodically created four times.  Tow created tasks are
134                 passed a handle to the other task so it can kill it before killing itself. 
135                 The other task is passed in null. */
136                 xTaskToKill = *( xTaskHandle* )pvParameters;
137         }
138         else
139         {
140                 xTaskToKill = NULL;
141         }
142
143         for( ;; )
144         {
145                 /* Do something random just to use some stack and registers. */
146                 d1 = 2.4;
147                 d2 = 89.2;
148                 d2 *= d1;
149                 vTaskDelay( xDelay );
150
151                 if( xTaskToKill != NULL )
152                 {
153                         /* Make sure the other task has a go before we delete it. */
154                         vTaskDelay( ( portTickType ) 0 );
155                         /* Kill the other task that was created by vCreateTasks(). */
156                         vTaskDelete( xTaskToKill );
157                         /* Kill ourselves. */
158                         vTaskDelete( NULL );
159                 }
160         }
161 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
162 /*-----------------------------------------------------------*/
163
164 static void vCreateTasks( void *pvParameters )
165 {
166 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
167 unsigned portBASE_TYPE uxPriority;
168 const portCHAR * const pcTaskStartMsg = "Create task started.\r\n";
169
170         /* Queue a message for printing to say the task has started. */
171         vPrintDisplayMessage( &pcTaskStartMsg );
172
173         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
174         vPortFree( pvParameters );
175
176         for( ;; )
177         {
178                 /* Just loop round, delaying then creating the four suicidal tasks. */
179                 vTaskDelay( xDelay );
180
181                 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
182                 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
183
184                 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
185                 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );
186
187                 ++sCreationCount;
188         }
189 }
190 /*-----------------------------------------------------------*/
191
192 /* This is called to check that the creator task is still running and that there
193 are not any more than four extra tasks. */
194 portBASE_TYPE xIsCreateTaskStillRunning( void )
195 {
196 static portSHORT sLastCreationCount = 0;
197 portSHORT sReturn = pdTRUE;
198 unsigned portBASE_TYPE uxTasksRunningNow;
199
200         if( sLastCreationCount == sCreationCount )
201         {
202                 sReturn = pdFALSE;
203         }
204        
205         uxTasksRunningNow = uxTaskGetNumberOfTasks();
206
207         if( uxTasksRunningNow < uxTasksRunningAtStart )
208         {
209                 sReturn = pdFALSE;
210         }
211         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
212         {
213                 sReturn = pdFALSE;
214         }
215         else
216         {
217                 /* Everything is okay. */
218         }
219
220         return sReturn;
221 }
222
223
Note: See TracBrowser for help on using the browser.