2 FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
37 * Create a single persistent task which periodically dynamically creates another
\r
38 * four tasks. The original task is called the creator task, the four tasks it
\r
39 * creates are called suicidal tasks.
\r
41 * Two of the created suicidal tasks kill one other suicidal task before killing
\r
42 * themselves - leaving just the original task remaining.
\r
44 * The creator task must be spawned after all of the other demo application tasks
\r
45 * as it keeps a check on the number of tasks under the scheduler control. The
\r
46 * number of tasks it expects to see running should never be greater than the
\r
47 * number of tasks that were in existence when the creator task was spawned, plus
\r
48 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
50 * \page DeathC death.c
\r
51 * \ingroup DemoFiles
\r
58 + Delay periods are now specified using variables and constants of
\r
59 portTickType rather than unsigned portLONG.
\r
64 /* Scheduler include files. */
\r
65 #include "FreeRTOS.h"
\r
68 /* Demo program include files. */
\r
72 #define deathSTACK_SIZE ( ( unsigned portSHORT ) 512 )
\r
74 /* The task originally created which is responsible for periodically dynamically
\r
75 creating another four tasks. */
\r
76 static void vCreateTasks( void *pvParameters );
\r
78 /* The task function of the dynamically created tasks. */
\r
79 static void vSuicidalTask( void *pvParameters );
\r
81 /* A variable which is incremented every time the dynamic tasks are created. This
\r
82 is used to check that the task is still running. */
\r
83 static volatile portSHORT sCreationCount = 0;
\r
85 /* Used to store the number of tasks that were originally running so the creator
\r
86 task can tell if any of the suicidal tasks have failed to die. */
\r
87 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
88 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
\r
90 /* Used to store a handle to the tasks that should be killed by a suicidal task,
\r
91 before it kills itself. */
\r
92 xTaskHandle xCreatedTask1, xCreatedTask2;
\r
94 /*-----------------------------------------------------------*/
\r
96 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
98 unsigned portBASE_TYPE *puxPriority;
\r
100 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
101 the suicidal tasks should be created. */
\r
102 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
103 *puxPriority = uxPriority;
\r
105 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
107 /* Record the number of tasks that are running now so we know if any of the
\r
108 suicidal tasks have failed to be killed. */
\r
109 uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
\r
111 /*-----------------------------------------------------------*/
\r
113 static void vSuicidalTask( void *pvParameters )
\r
116 xTaskHandle xTaskToKill;
\r
117 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;
\r
119 if( pvParameters != NULL )
\r
121 /* This task is periodically created four times. Tow created tasks are
\r
122 passed a handle to the other task so it can kill it before killing itself.
\r
123 The other task is passed in null. */
\r
124 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
128 xTaskToKill = NULL;
\r
133 /* Do something random just to use some stack and registers. */
\r
137 vTaskDelay( xDelay );
\r
139 if( xTaskToKill != NULL )
\r
141 /* Make sure the other task has a go before we delete it. */
\r
142 vTaskDelay( ( portTickType ) 0 );
\r
143 /* Kill the other task that was created by vCreateTasks(). */
\r
144 vTaskDelete( xTaskToKill );
\r
145 /* Kill ourselves. */
\r
146 vTaskDelete( NULL );
\r
149 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
150 /*-----------------------------------------------------------*/
\r
152 static void vCreateTasks( void *pvParameters )
\r
154 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
155 unsigned portBASE_TYPE uxPriority;
\r
156 const portCHAR * const pcTaskStartMsg = "Create task started.\r\n";
\r
158 /* Queue a message for printing to say the task has started. */
\r
159 vPrintDisplayMessage( &pcTaskStartMsg );
\r
161 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
162 vPortFree( pvParameters );
\r
166 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
167 vTaskDelay( xDelay );
\r
169 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
\r
170 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
\r
172 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
\r
173 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );
\r
178 /*-----------------------------------------------------------*/
\r
180 /* This is called to check that the creator task is still running and that there
\r
181 are not any more than four extra tasks. */
\r
182 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
184 static portSHORT sLastCreationCount = 0;
\r
185 portSHORT sReturn = pdTRUE;
\r
186 unsigned portBASE_TYPE uxTasksRunningNow;
\r
188 if( sLastCreationCount == sCreationCount )
\r
193 uxTasksRunningNow = uxTaskGetNumberOfTasks();
\r
195 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
199 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
205 /* Everything is okay. */
\r