2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Create a single persistent task which periodically dynamically creates another
\r
67 * two tasks. The original task is called the creator task, the two tasks it
\r
68 * creates are called suicidal tasks.
\r
70 * One of the created suicidal tasks kill one other suicidal task before killing
\r
71 * itself - leaving just the original task remaining.
\r
73 * The creator task must be spawned after all of the other demo application tasks
\r
74 * as it keeps a check on the number of tasks under the scheduler control. The
\r
75 * number of tasks it expects to see running should never be greater than the
\r
76 * number of tasks that were in existence when the creator task was spawned, plus
\r
77 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
79 * \page DeathC death.c
\r
80 * \ingroup DemoFiles
\r
86 + CreationCount sizes changed from unsigned portBASE_TYPE to
\r
87 unsigned short to minimize the risk of overflowing.
\r
89 + Reset of usLastCreationCount added
\r
92 + Changed the dummy calculation to use variables of type long, rather than
\r
93 float. This allows the file to be used with ports that do not support
\r
100 /* Scheduler include files. */
\r
101 #include "FreeRTOS.h"
\r
104 /* Demo program include files. */
\r
107 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
109 /* The task originally created which is responsible for periodically dynamically
\r
110 creating another four tasks. */
\r
111 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
113 /* The task function of the dynamically created tasks. */
\r
114 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
116 /* A variable which is incremented every time the dynamic tasks are created. This
\r
117 is used to check that the task is still running. */
\r
118 static volatile unsigned short usCreationCount = 0;
\r
120 /* Used to store the number of tasks that were originally running so the creator
\r
121 task can tell if any of the suicidal tasks have failed to die.
\r
123 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
125 /* Tasks are deleted by the idle task. Under heavy load the idle task might
\r
126 not get much processing time, so it would be legitimate for several tasks to
\r
127 remain undeleted for a short period. */
\r
128 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 3;
\r
130 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
131 before it kills itself. */
\r
132 xTaskHandle xCreatedTask;
\r
134 /*-----------------------------------------------------------*/
\r
136 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
138 unsigned portBASE_TYPE *puxPriority;
\r
140 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
141 the suicidal tasks should be created. */
\r
142 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
143 *puxPriority = uxPriority;
\r
145 xTaskCreate( vCreateTasks, ( signed char * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
147 /* Record the number of tasks that are running now so we know if any of the
\r
148 suicidal tasks have failed to be killed. */
\r
149 uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
151 /* FreeRTOS.org versions before V3.0 started the idle-task as the very
\r
152 first task. The idle task was then already included in uxTasksRunningAtStart.
\r
153 From FreeRTOS V3.0 on, the idle task is started when the scheduler is
\r
154 started. Therefore the idle task is not yet accounted for. We correct
\r
155 this by increasing uxTasksRunningAtStart by 1. */
\r
156 uxTasksRunningAtStart++;
\r
158 /* From FreeRTOS version 7.0.0 can optionally create a timer service task.
\r
159 If this is done, then uxTasksRunningAtStart needs incrementing again as that
\r
160 too is created when the scheduler is started. */
\r
161 #if configUSE_TIMERS == 1
\r
162 uxTasksRunningAtStart++;
\r
165 /*-----------------------------------------------------------*/
\r
167 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
169 volatile long l1, l2;
\r
170 xTaskHandle xTaskToKill;
\r
171 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
173 if( pvParameters != NULL )
\r
175 /* This task is periodically created four times. Two created tasks are
\r
176 passed a handle to the other task so it can kill it before killing itself.
\r
177 The other task is passed in null. */
\r
178 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
182 xTaskToKill = NULL;
\r
187 /* Do something random just to use some stack and registers. */
\r
191 vTaskDelay( xDelay );
\r
193 if( xTaskToKill != NULL )
\r
195 /* Make sure the other task has a go before we delete it. */
\r
196 vTaskDelay( ( portTickType ) 0 );
\r
198 /* Kill the other task that was created by vCreateTasks(). */
\r
199 vTaskDelete( xTaskToKill );
\r
201 /* Kill ourselves. */
\r
202 vTaskDelete( NULL );
\r
205 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
206 /*-----------------------------------------------------------*/
\r
208 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
210 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
211 unsigned portBASE_TYPE uxPriority;
\r
213 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
214 vPortFree( pvParameters );
\r
218 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
219 vTaskDelay( xDelay );
\r
221 xCreatedTask = NULL;
\r
223 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
224 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
229 /*-----------------------------------------------------------*/
\r
231 /* This is called to check that the creator task is still running and that there
\r
232 are not any more than four extra tasks. */
\r
233 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
235 static unsigned short usLastCreationCount = 0xfff;
\r
236 portBASE_TYPE xReturn = pdTRUE;
\r
237 static unsigned portBASE_TYPE uxTasksRunningNow;
\r
239 if( usLastCreationCount == usCreationCount )
\r
245 usLastCreationCount = usCreationCount;
\r
248 uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
250 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
254 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
260 /* Everything is okay. */
\r