2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 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 * four tasks. The original task is called the creator task, the four tasks it
\r
69 * creates are called suicidal tasks.
\r
71 * Two of the created suicidal tasks kill one other suicidal task before killing
\r
72 * themselves - 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
88 + Delay periods are now specified using variables and constants of
\r
89 TickType_t rather than unsigned long.
\r
94 /* Scheduler include files. */
\r
95 #include "FreeRTOS.h"
\r
98 /* Demo program include files. */
\r
102 #define deathSTACK_SIZE ( ( unsigned short ) 512 )
\r
104 /* The task originally created which is responsible for periodically dynamically
\r
105 creating another four tasks. */
\r
106 static void vCreateTasks( void *pvParameters );
\r
108 /* The task function of the dynamically created tasks. */
\r
109 static void vSuicidalTask( void *pvParameters );
\r
111 /* A variable which is incremented every time the dynamic tasks are created. This
\r
112 is used to check that the task is still running. */
\r
113 static volatile short sCreationCount = 0;
\r
115 /* Used to store the number of tasks that were originally running so the creator
\r
116 task can tell if any of the suicidal tasks have failed to die. */
\r
117 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
118 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
\r
120 /* Used to store a handle to the tasks that should be killed by a suicidal task,
\r
121 before it kills itself. */
\r
122 TaskHandle_t xCreatedTask1, xCreatedTask2;
\r
124 /*-----------------------------------------------------------*/
\r
126 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
128 unsigned portBASE_TYPE *puxPriority;
\r
130 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
131 the suicidal tasks should be created. */
\r
132 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
133 *puxPriority = uxPriority;
\r
135 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
137 /* Record the number of tasks that are running now so we know if any of the
\r
138 suicidal tasks have failed to be killed. */
\r
139 uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
\r
141 /*-----------------------------------------------------------*/
\r
143 static void vSuicidalTask( void *pvParameters )
\r
146 TaskHandle_t xTaskToKill;
\r
147 const TickType_t xDelay = ( TickType_t ) 500 / portTICK_PERIOD_MS;
\r
149 if( pvParameters != NULL )
\r
151 /* This task is periodically created four times. Tow created tasks are
\r
152 passed a handle to the other task so it can kill it before killing itself.
\r
153 The other task is passed in null. */
\r
154 xTaskToKill = *( TaskHandle_t* )pvParameters;
\r
158 xTaskToKill = NULL;
\r
163 /* Do something random just to use some stack and registers. */
\r
167 vTaskDelay( xDelay );
\r
169 if( xTaskToKill != NULL )
\r
171 /* Make sure the other task has a go before we delete it. */
\r
172 vTaskDelay( ( TickType_t ) 0 );
\r
173 /* Kill the other task that was created by vCreateTasks(). */
\r
174 vTaskDelete( xTaskToKill );
\r
175 /* Kill ourselves. */
\r
176 vTaskDelete( NULL );
\r
179 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
180 /*-----------------------------------------------------------*/
\r
182 static void vCreateTasks( void *pvParameters )
\r
184 const TickType_t xDelay = ( TickType_t ) 1000 / portTICK_PERIOD_MS;
\r
185 unsigned portBASE_TYPE uxPriority;
\r
186 const char * const pcTaskStartMsg = "Create task started.\r\n";
\r
188 /* Queue a message for printing to say the task has started. */
\r
189 vPrintDisplayMessage( &pcTaskStartMsg );
\r
191 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
192 vPortFree( pvParameters );
\r
196 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
197 vTaskDelay( xDelay );
\r
199 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
\r
200 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
\r
202 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
\r
203 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );
\r
208 /*-----------------------------------------------------------*/
\r
210 /* This is called to check that the creator task is still running and that there
\r
211 are not any more than four extra tasks. */
\r
212 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
214 static short sLastCreationCount = 0;
\r
215 short sReturn = pdTRUE;
\r
216 unsigned portBASE_TYPE uxTasksRunningNow;
\r
218 if( sLastCreationCount == sCreationCount )
\r
223 uxTasksRunningNow = uxTaskGetNumberOfTasks();
\r
225 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
229 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
235 /* Everything is okay. */
\r