2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Create a single persistent task which periodically dynamically creates another
\r
98 * two tasks. The original task is called the creator task, the two tasks it
\r
99 * creates are called suicidal tasks.
\r
101 * One of the created suicidal tasks kill one other suicidal task before killing
\r
102 * itself - leaving just the original task remaining.
\r
104 * The creator task must be spawned after all of the other demo application tasks
\r
105 * as it keeps a check on the number of tasks under the scheduler control. The
\r
106 * number of tasks it expects to see running should never be greater than the
\r
107 * number of tasks that were in existence when the creator task was spawned, plus
\r
108 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
110 * \page DeathC death.c
\r
111 * \ingroup DemoFiles
\r
116 #include <stdlib.h>
\r
118 /* Scheduler include files. */
\r
119 #include "FreeRTOS.h"
\r
122 /* Demo program include files. */
\r
125 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
127 /* The task originally created which is responsible for periodically dynamically
\r
128 creating another four tasks. */
\r
129 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
131 /* The task function of the dynamically created tasks. */
\r
132 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
134 /* A variable which is incremented every time the dynamic tasks are created. This
\r
135 is used to check that the task is still running. */
\r
136 static volatile uint16_t usCreationCount = 0;
\r
138 /* Used to store the number of tasks that were originally running so the creator
\r
139 task can tell if any of the suicidal tasks have failed to die.
\r
141 static volatile UBaseType_t uxTasksRunningAtStart = 0;
\r
143 /* Tasks are deleted by the idle task. Under heavy load the idle task might
\r
144 not get much processing time, so it would be legitimate for several tasks to
\r
145 remain undeleted for a short period. */
\r
146 static const UBaseType_t uxMaxNumberOfExtraTasksRunning = 3;
\r
148 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
149 before it kills itself. */
\r
150 TaskHandle_t xCreatedTask;
\r
152 /*-----------------------------------------------------------*/
\r
154 void vCreateSuicidalTasks( UBaseType_t uxPriority )
\r
156 UBaseType_t *puxPriority;
\r
158 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
159 the suicidal tasks should be created. */
\r
160 puxPriority = ( UBaseType_t * ) pvPortMalloc( sizeof( UBaseType_t ) );
\r
161 *puxPriority = uxPriority;
\r
163 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
165 /* Record the number of tasks that are running now so we know if any of the
\r
166 suicidal tasks have failed to be killed. */
\r
167 uxTasksRunningAtStart = ( UBaseType_t ) uxTaskGetNumberOfTasks();
\r
169 /* FreeRTOS.org versions before V3.0 started the idle-task as the very
\r
170 first task. The idle task was then already included in uxTasksRunningAtStart.
\r
171 From FreeRTOS V3.0 on, the idle task is started when the scheduler is
\r
172 started. Therefore the idle task is not yet accounted for. We correct
\r
173 this by increasing uxTasksRunningAtStart by 1. */
\r
174 uxTasksRunningAtStart++;
\r
176 /* From FreeRTOS version 7.0.0 can optionally create a timer service task.
\r
177 If this is done, then uxTasksRunningAtStart needs incrementing again as that
\r
178 too is created when the scheduler is started. */
\r
179 #if configUSE_TIMERS == 1
\r
180 uxTasksRunningAtStart++;
\r
183 /*-----------------------------------------------------------*/
\r
185 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
187 volatile long l1, l2;
\r
188 TaskHandle_t xTaskToKill;
\r
189 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;
\r
191 if( pvParameters != NULL )
\r
193 /* This task is periodically created four times. Two created tasks are
\r
194 passed a handle to the other task so it can kill it before killing itself.
\r
195 The other task is passed in null. */
\r
196 xTaskToKill = *( TaskHandle_t* )pvParameters;
\r
200 xTaskToKill = NULL;
\r
205 /* Do something random just to use some stack and registers. */
\r
209 vTaskDelay( xDelay );
\r
211 if( xTaskToKill != NULL )
\r
213 /* Make sure the other task has a go before we delete it. */
\r
214 vTaskDelay( ( TickType_t ) 0 );
\r
216 /* Kill the other task that was created by vCreateTasks(). */
\r
217 vTaskDelete( xTaskToKill );
\r
219 /* Kill ourselves. */
\r
220 vTaskDelete( NULL );
\r
223 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
224 /*-----------------------------------------------------------*/
\r
226 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
228 const TickType_t xDelay = ( TickType_t ) 1000 / portTICK_PERIOD_MS;
\r
229 UBaseType_t uxPriority;
\r
231 uxPriority = *( UBaseType_t * ) pvParameters;
\r
232 vPortFree( pvParameters );
\r
236 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
237 vTaskDelay( xDelay );
\r
239 xCreatedTask = NULL;
\r
241 xTaskCreate( vSuicidalTask, "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
242 xTaskCreate( vSuicidalTask, "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
247 /*-----------------------------------------------------------*/
\r
249 /* This is called to check that the creator task is still running and that there
\r
250 are not any more than four extra tasks. */
\r
251 BaseType_t xIsCreateTaskStillRunning( void )
\r
253 static uint16_t usLastCreationCount = 0xfff;
\r
254 BaseType_t xReturn = pdTRUE;
\r
255 static UBaseType_t uxTasksRunningNow;
\r
257 if( usLastCreationCount == usCreationCount )
\r
263 usLastCreationCount = usCreationCount;
\r
266 uxTasksRunningNow = ( UBaseType_t ) uxTaskGetNumberOfTasks();
\r
268 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
272 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
278 /* Everything is okay. */
\r