2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 * Create a single persistent task which periodically dynamically creates another
\r
50 * two tasks. The original task is called the creator task, the two tasks it
\r
51 * creates are called suicidal tasks.
\r
53 * One of the created suicidal tasks kill one other suicidal task before killing
\r
54 * itself - leaving just the original task remaining.
\r
56 * The creator task must be spawned after all of the other demo application tasks
\r
57 * as it keeps a check on the number of tasks under the scheduler control. The
\r
58 * number of tasks it expects to see running should never be greater than the
\r
59 * number of tasks that were in existence when the creator task was spawned, plus
\r
60 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
62 * \page DeathC death.c
\r
63 * \ingroup DemoFiles
\r
69 + CreationCount sizes changed from unsigned portBASE_TYPE to
\r
70 unsigned portSHORT to minimize the risk of overflowing.
\r
72 + Reset of usLastCreationCount added
\r
75 + Changed the dummy calculation to use variables of type long, rather than
\r
76 float. This allows the file to be used with ports that do not support
\r
83 /* Scheduler include files. */
\r
84 #include "FreeRTOS.h"
\r
87 /* Demo program include files. */
\r
90 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
92 /* The task originally created which is responsible for periodically dynamically
\r
93 creating another four tasks. */
\r
94 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
96 /* The task function of the dynamically created tasks. */
\r
97 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
99 /* A variable which is incremented every time the dynamic tasks are created. This
\r
100 is used to check that the task is still running. */
\r
101 static volatile unsigned portSHORT usCreationCount = 0;
\r
103 /* Used to store the number of tasks that were originally running so the creator
\r
104 task can tell if any of the suicidal tasks have failed to die.
\r
106 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
108 /* Tasks are deleted by the idle task. Under heavy load the idle task might
\r
109 not get much processing time, so it would be legitimate for several tasks to
\r
110 remain undeleted for a short period. */
\r
111 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;
\r
113 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
114 before it kills itself. */
\r
115 xTaskHandle xCreatedTask;
\r
117 /*-----------------------------------------------------------*/
\r
119 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
121 unsigned portBASE_TYPE *puxPriority;
\r
123 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
124 the suicidal tasks should be created. */
\r
125 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
126 *puxPriority = uxPriority;
\r
128 xTaskCreate( vCreateTasks, ( signed portCHAR * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
130 /* Record the number of tasks that are running now so we know if any of the
\r
131 suicidal tasks have failed to be killed. */
\r
132 uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
134 /* FreeRTOS.org versions before V3.0 started the idle-task as the very
\r
135 first task. The idle task was then already included in uxTasksRunningAtStart.
\r
136 From FreeRTOS V3.0 on, the idle task is started when the scheduler is
\r
137 started. Therefore the idle task is not yet accounted for. We correct
\r
138 this by increasing uxTasksRunningAtStart by 1. */
\r
139 uxTasksRunningAtStart++;
\r
141 /*-----------------------------------------------------------*/
\r
143 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
145 volatile portLONG l1, l2;
\r
146 xTaskHandle xTaskToKill;
\r
147 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
149 if( pvParameters != NULL )
\r
151 /* This task is periodically created four times. Two created tasks are
\r
152 passed a handle to the other task so it can kill it before killing itself.
\r
153 The other task is passed in null. */
\r
154 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
158 xTaskToKill = NULL;
\r
163 /* Do something random just to use some stack and registers. */
\r
167 vTaskDelay( xDelay );
\r
169 if( xTaskToKill != NULL )
\r
171 /* Make sure the other task has a go before we delete it. */
\r
172 vTaskDelay( ( portTickType ) 0 );
\r
174 /* Kill the other task that was created by vCreateTasks(). */
\r
175 vTaskDelete( xTaskToKill );
\r
177 /* Kill ourselves. */
\r
178 vTaskDelete( NULL );
\r
181 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
182 /*-----------------------------------------------------------*/
\r
184 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
186 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
187 unsigned portBASE_TYPE uxPriority;
\r
189 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
190 vPortFree( pvParameters );
\r
194 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
195 vTaskDelay( xDelay );
\r
197 xCreatedTask = NULL;
\r
199 xTaskCreate( vSuicidalTask, ( signed portCHAR * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
200 xTaskCreate( vSuicidalTask, ( signed portCHAR * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
205 /*-----------------------------------------------------------*/
\r
207 /* This is called to check that the creator task is still running and that there
\r
208 are not any more than four extra tasks. */
\r
209 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
211 static unsigned portSHORT usLastCreationCount = 0xfff;
\r
212 portBASE_TYPE xReturn = pdTRUE;
\r
213 static unsigned portBASE_TYPE uxTasksRunningNow;
\r
215 if( usLastCreationCount == usCreationCount )
\r
221 usLastCreationCount = usCreationCount;
\r
224 uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
226 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
230 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
236 /* Everything is okay. */
\r