2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * Create a single persistent task which periodically dynamically creates another
\r
68 * two tasks. The original task is called the creator task, the two tasks it
\r
69 * creates are called suicidal tasks.
\r
71 * One of the created suicidal tasks kill one other suicidal task before killing
\r
72 * itself - leaving just the original task remaining.
\r
74 * The creator task must be spawned after all of the other demo application tasks
\r
75 * as it keeps a check on the number of tasks under the scheduler control. The
\r
76 * number of tasks it expects to see running should never be greater than the
\r
77 * number of tasks that were in existence when the creator task was spawned, plus
\r
78 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
80 * \page DeathC death.c
\r
81 * \ingroup DemoFiles
\r
87 + CreationCount sizes changed from unsigned portBASE_TYPE to
\r
88 unsigned short to minimize the risk of overflowing.
\r
90 + Reset of usLastCreationCount added
\r
93 + Changed the dummy calculation to use variables of type long, rather than
\r
94 float. This allows the file to be used with ports that do not support
\r
101 /* Scheduler include files. */
\r
102 #include "FreeRTOS.h"
\r
105 /* Demo program include files. */
\r
108 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
110 /* The task originally created which is responsible for periodically dynamically
\r
111 creating another four tasks. */
\r
112 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
114 /* The task function of the dynamically created tasks. */
\r
115 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
117 /* A variable which is incremented every time the dynamic tasks are created. This
\r
118 is used to check that the task is still running. */
\r
119 static volatile unsigned short usCreationCount = 0;
\r
121 /* Used to store the number of tasks that were originally running so the creator
\r
122 task can tell if any of the suicidal tasks have failed to die.
\r
124 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
126 /* Tasks are deleted by the idle task. Under heavy load the idle task might
\r
127 not get much processing time, so it would be legitimate for several tasks to
\r
128 remain undeleted for a short period. */
\r
129 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 3;
\r
131 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
132 before it kills itself. */
\r
133 xTaskHandle xCreatedTask;
\r
135 /*-----------------------------------------------------------*/
\r
137 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
139 unsigned portBASE_TYPE *puxPriority;
\r
141 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
142 the suicidal tasks should be created. */
\r
143 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
144 *puxPriority = uxPriority;
\r
146 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
148 /* Record the number of tasks that are running now so we know if any of the
\r
149 suicidal tasks have failed to be killed. */
\r
150 uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
152 /* FreeRTOS.org versions before V3.0 started the idle-task as the very
\r
153 first task. The idle task was then already included in uxTasksRunningAtStart.
\r
154 From FreeRTOS V3.0 on, the idle task is started when the scheduler is
\r
155 started. Therefore the idle task is not yet accounted for. We correct
\r
156 this by increasing uxTasksRunningAtStart by 1. */
\r
157 uxTasksRunningAtStart++;
\r
159 /* From FreeRTOS version 7.0.0 can optionally create a timer service task.
\r
160 If this is done, then uxTasksRunningAtStart needs incrementing again as that
\r
161 too is created when the scheduler is started. */
\r
162 #if configUSE_TIMERS == 1
\r
163 uxTasksRunningAtStart++;
\r
166 /*-----------------------------------------------------------*/
\r
168 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
170 volatile long l1, l2;
\r
171 xTaskHandle xTaskToKill;
\r
172 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
174 if( pvParameters != NULL )
\r
176 /* This task is periodically created four times. Two created tasks are
\r
177 passed a handle to the other task so it can kill it before killing itself.
\r
178 The other task is passed in null. */
\r
179 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
183 xTaskToKill = NULL;
\r
188 /* Do something random just to use some stack and registers. */
\r
192 vTaskDelay( xDelay );
\r
194 if( xTaskToKill != NULL )
\r
196 /* Make sure the other task has a go before we delete it. */
\r
197 vTaskDelay( ( portTickType ) 0 );
\r
199 /* Kill the other task that was created by vCreateTasks(). */
\r
200 vTaskDelete( xTaskToKill );
\r
202 /* Kill ourselves. */
\r
203 vTaskDelete( NULL );
\r
206 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
207 /*-----------------------------------------------------------*/
\r
209 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
211 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
212 unsigned portBASE_TYPE uxPriority;
\r
214 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
215 vPortFree( pvParameters );
\r
219 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
220 vTaskDelay( xDelay );
\r
222 xCreatedTask = NULL;
\r
224 xTaskCreate( vSuicidalTask, "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
225 xTaskCreate( vSuicidalTask, "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
230 /*-----------------------------------------------------------*/
\r
232 /* This is called to check that the creator task is still running and that there
\r
233 are not any more than four extra tasks. */
\r
234 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
236 static unsigned short usLastCreationCount = 0xfff;
\r
237 portBASE_TYPE xReturn = pdTRUE;
\r
238 static unsigned portBASE_TYPE uxTasksRunningNow;
\r
240 if( usLastCreationCount == usCreationCount )
\r
246 usLastCreationCount = usCreationCount;
\r
249 uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
\r
251 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
255 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
261 /* Everything is okay. */
\r