2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * Create a single persistent task which periodically dynamically creates another
\r
71 * four tasks. The original task is called the creator task, the four tasks it
\r
72 * creates are called suicidal tasks.
\r
74 * Two of the created suicidal tasks kill one other suicidal task before killing
\r
75 * themselves - leaving just the original task remaining.
\r
77 * The creator task must be spawned after all of the other demo application tasks
\r
78 * as it keeps a check on the number of tasks under the scheduler control. The
\r
79 * number of tasks it expects to see running should never be greater than the
\r
80 * number of tasks that were in existence when the creator task was spawned, plus
\r
81 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
83 * \page DeathC death.c
\r
84 * \ingroup DemoFiles
\r
91 + Delay periods are now specified using variables and constants of
\r
92 portTickType rather than unsigned long.
\r
97 /* Scheduler include files. */
\r
98 #include "FreeRTOS.h"
\r
101 /* Demo program include files. */
\r
105 #define deathSTACK_SIZE ( ( unsigned short ) 512 )
\r
107 /* The task originally created which is responsible for periodically dynamically
\r
108 creating another four tasks. */
\r
109 static void vCreateTasks( void *pvParameters );
\r
111 /* The task function of the dynamically created tasks. */
\r
112 static void vSuicidalTask( void *pvParameters );
\r
114 /* A variable which is incremented every time the dynamic tasks are created. This
\r
115 is used to check that the task is still running. */
\r
116 static volatile short sCreationCount = 0;
\r
118 /* Used to store the number of tasks that were originally running so the creator
\r
119 task can tell if any of the suicidal tasks have failed to die. */
\r
120 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
121 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
\r
123 /* Used to store a handle to the tasks that should be killed by a suicidal task,
\r
124 before it kills itself. */
\r
125 xTaskHandle xCreatedTask1, xCreatedTask2;
\r
127 /*-----------------------------------------------------------*/
\r
129 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
131 unsigned portBASE_TYPE *puxPriority;
\r
133 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
134 the suicidal tasks should be created. */
\r
135 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
136 *puxPriority = uxPriority;
\r
138 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
140 /* Record the number of tasks that are running now so we know if any of the
\r
141 suicidal tasks have failed to be killed. */
\r
142 uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
\r
144 /*-----------------------------------------------------------*/
\r
146 static void vSuicidalTask( void *pvParameters )
\r
149 xTaskHandle xTaskToKill;
\r
150 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;
\r
152 if( pvParameters != NULL )
\r
154 /* This task is periodically created four times. Tow created tasks are
\r
155 passed a handle to the other task so it can kill it before killing itself.
\r
156 The other task is passed in null. */
\r
157 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
161 xTaskToKill = NULL;
\r
166 /* Do something random just to use some stack and registers. */
\r
170 vTaskDelay( xDelay );
\r
172 if( xTaskToKill != NULL )
\r
174 /* Make sure the other task has a go before we delete it. */
\r
175 vTaskDelay( ( portTickType ) 0 );
\r
176 /* Kill the other task that was created by vCreateTasks(). */
\r
177 vTaskDelete( xTaskToKill );
\r
178 /* Kill ourselves. */
\r
179 vTaskDelete( NULL );
\r
182 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
183 /*-----------------------------------------------------------*/
\r
185 static void vCreateTasks( void *pvParameters )
\r
187 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
188 unsigned portBASE_TYPE uxPriority;
\r
189 const char * const pcTaskStartMsg = "Create task started.\r\n";
\r
191 /* Queue a message for printing to say the task has started. */
\r
192 vPrintDisplayMessage( &pcTaskStartMsg );
\r
194 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
195 vPortFree( pvParameters );
\r
199 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
200 vTaskDelay( xDelay );
\r
202 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
\r
203 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
\r
205 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
\r
206 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, 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 short sLastCreationCount = 0;
\r
218 short sReturn = pdTRUE;
\r
219 unsigned portBASE_TYPE uxTasksRunningNow;
\r
221 if( sLastCreationCount == sCreationCount )
\r
226 uxTasksRunningNow = uxTaskGetNumberOfTasks();
\r
228 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
232 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
238 /* Everything is okay. */
\r