2 FreeRTOS V7.0.1 - 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 * four tasks. The original task is called the creator task, the four tasks it
\r
57 * creates are called suicidal tasks.
\r
59 * Two of the created suicidal tasks kill one other suicidal task before killing
\r
60 * themselves - 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
76 + Delay periods are now specified using variables and constants of
\r
77 portTickType rather than unsigned long.
\r
82 /* Scheduler include files. */
\r
83 #include "FreeRTOS.h"
\r
86 /* Demo program include files. */
\r
90 #define deathSTACK_SIZE ( ( unsigned short ) 512 )
\r
92 /* The task originally created which is responsible for periodically dynamically
\r
93 creating another four tasks. */
\r
94 static void vCreateTasks( void *pvParameters );
\r
96 /* The task function of the dynamically created tasks. */
\r
97 static void vSuicidalTask( void *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 short sCreationCount = 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
105 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
106 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
\r
108 /* Used to store a handle to the tasks that should be killed by a suicidal task,
\r
109 before it kills itself. */
\r
110 xTaskHandle xCreatedTask1, xCreatedTask2;
\r
112 /*-----------------------------------------------------------*/
\r
114 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
116 unsigned portBASE_TYPE *puxPriority;
\r
118 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
119 the suicidal tasks should be created. */
\r
120 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
121 *puxPriority = uxPriority;
\r
123 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
125 /* Record the number of tasks that are running now so we know if any of the
\r
126 suicidal tasks have failed to be killed. */
\r
127 uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
\r
129 /*-----------------------------------------------------------*/
\r
131 static void vSuicidalTask( void *pvParameters )
\r
134 xTaskHandle xTaskToKill;
\r
135 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;
\r
137 if( pvParameters != NULL )
\r
139 /* This task is periodically created four times. Tow created tasks are
\r
140 passed a handle to the other task so it can kill it before killing itself.
\r
141 The other task is passed in null. */
\r
142 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
146 xTaskToKill = NULL;
\r
151 /* Do something random just to use some stack and registers. */
\r
155 vTaskDelay( xDelay );
\r
157 if( xTaskToKill != NULL )
\r
159 /* Make sure the other task has a go before we delete it. */
\r
160 vTaskDelay( ( portTickType ) 0 );
\r
161 /* Kill the other task that was created by vCreateTasks(). */
\r
162 vTaskDelete( xTaskToKill );
\r
163 /* Kill ourselves. */
\r
164 vTaskDelete( NULL );
\r
167 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
168 /*-----------------------------------------------------------*/
\r
170 static void vCreateTasks( void *pvParameters )
\r
172 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
173 unsigned portBASE_TYPE uxPriority;
\r
174 const char * const pcTaskStartMsg = "Create task started.\r\n";
\r
176 /* Queue a message for printing to say the task has started. */
\r
177 vPrintDisplayMessage( &pcTaskStartMsg );
\r
179 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
180 vPortFree( pvParameters );
\r
184 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
185 vTaskDelay( xDelay );
\r
187 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
\r
188 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
\r
190 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
\r
191 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );
\r
196 /*-----------------------------------------------------------*/
\r
198 /* This is called to check that the creator task is still running and that there
\r
199 are not any more than four extra tasks. */
\r
200 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
202 static short sLastCreationCount = 0;
\r
203 short sReturn = pdTRUE;
\r
204 unsigned portBASE_TYPE uxTasksRunningNow;
\r
206 if( sLastCreationCount == sCreationCount )
\r
211 uxTasksRunningNow = uxTaskGetNumberOfTasks();
\r
213 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
217 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
223 /* Everything is okay. */
\r