2 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\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
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Create a single persistent task which periodically dynamically creates another
\r
56 * two tasks. The original task is called the creator task, the two tasks it
\r
57 * creates are called suicidal tasks.
\r
59 * One of the created suicidal tasks kill one other suicidal task before killing
\r
60 * itself - leaving just the original task remaining.
\r
62 * The creator task must be spawned after all of the other demo application tasks
\r
63 * as it keeps a check on the number of tasks under the scheduler control. The
\r
64 * number of tasks it expects to see running should never be greater than the
\r
65 * number of tasks that were in existence when the creator task was spawned, plus
\r
66 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
68 * \page DeathC death.c
\r
69 * \ingroup DemoFiles
\r
75 + CreationCount sizes changed from unsigned portBASE_TYPE to
\r
76 unsigned short to minimize the risk of overflowing.
\r
78 + Reset of usLastCreationCount added
\r
81 + Changed the dummy calculation to use variables of type long, rather than
\r
82 float. This allows the file to be used with ports that do not support
\r
89 /* Scheduler include files. */
\r
90 #include "FreeRTOS.h"
\r
93 /* Demo program include files. */
\r
96 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
98 /* The task originally created which is responsible for periodically dynamically
\r
99 creating another four tasks. */
\r
100 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
102 /* The task function of the dynamically created tasks. */
\r
103 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
105 /* A variable which is incremented every time the dynamic tasks are created. This
\r
106 is used to check that the task is still running. */
\r
107 static volatile unsigned short usCreationCount = 0;
\r
109 /* Used to store the number of tasks that were originally running so the creator
\r
110 task can tell if any of the suicidal tasks have failed to die.
\r
112 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
114 /* Tasks are deleted by the idle task. Under heavy load the idle task might
\r
115 not get much processing time, so it would be legitimate for several tasks to
\r
116 remain undeleted for a short period. */
\r
117 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;
\r
119 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
120 before it kills itself. */
\r
121 xTaskHandle xCreatedTask;
\r
123 /*-----------------------------------------------------------*/
\r
125 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
127 unsigned portBASE_TYPE *puxPriority;
\r
129 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
130 the suicidal tasks should be created. */
\r
131 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
132 *puxPriority = uxPriority;
\r
134 xTaskCreate( vCreateTasks, ( signed char * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
136 /* Record the number of tasks that are running now so we know if any of the
\r
137 suicidal tasks have failed to be killed. */
\r
138 uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
140 /* FreeRTOS.org versions before V3.0 started the idle-task as the very
\r
141 first task. The idle task was then already included in uxTasksRunningAtStart.
\r
142 From FreeRTOS V3.0 on, the idle task is started when the scheduler is
\r
143 started. Therefore the idle task is not yet accounted for. We correct
\r
144 this by increasing uxTasksRunningAtStart by 1. */
\r
145 uxTasksRunningAtStart++;
\r
147 /*-----------------------------------------------------------*/
\r
149 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
151 volatile long l1, l2;
\r
152 xTaskHandle xTaskToKill;
\r
153 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
155 if( pvParameters != NULL )
\r
157 /* This task is periodically created four times. Two created tasks are
\r
158 passed a handle to the other task so it can kill it before killing itself.
\r
159 The other task is passed in null. */
\r
160 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
164 xTaskToKill = NULL;
\r
169 /* Do something random just to use some stack and registers. */
\r
173 vTaskDelay( xDelay );
\r
175 if( xTaskToKill != NULL )
\r
177 /* Make sure the other task has a go before we delete it. */
\r
178 vTaskDelay( ( portTickType ) 0 );
\r
180 /* Kill the other task that was created by vCreateTasks(). */
\r
181 vTaskDelete( xTaskToKill );
\r
183 /* Kill ourselves. */
\r
184 vTaskDelete( NULL );
\r
187 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
188 /*-----------------------------------------------------------*/
\r
190 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
192 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
193 unsigned portBASE_TYPE uxPriority;
\r
195 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
196 vPortFree( pvParameters );
\r
200 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
201 vTaskDelay( xDelay );
\r
203 xCreatedTask = NULL;
\r
205 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
206 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
211 /*-----------------------------------------------------------*/
\r
213 /* This is called to check that the creator task is still running and that there
\r
214 are not any more than four extra tasks. */
\r
215 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
217 static unsigned short usLastCreationCount = 0xfff;
\r
218 portBASE_TYPE xReturn = pdTRUE;
\r
219 static unsigned portBASE_TYPE uxTasksRunningNow;
\r
221 if( usLastCreationCount == usCreationCount )
\r
227 usLastCreationCount = usCreationCount;
\r
230 uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
232 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
236 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
242 /* Everything is okay. */
\r