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 * four tasks. The original task is called the creator task, the four tasks it
\r
68 * creates are called suicidal tasks.
\r
70 * Two of the created suicidal tasks kill one other suicidal task before killing
\r
71 * themselves - 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
87 + Delay periods are now specified using variables and constants of
\r
88 portTickType rather than unsigned long.
\r
93 /* Scheduler include files. */
\r
94 #include "FreeRTOS.h"
\r
97 /* Demo program include files. */
\r
101 #define deathSTACK_SIZE ( ( unsigned short ) 512 )
\r
103 /* The task originally created which is responsible for periodically dynamically
\r
104 creating another four tasks. */
\r
105 static void vCreateTasks( void *pvParameters );
\r
107 /* The task function of the dynamically created tasks. */
\r
108 static void vSuicidalTask( void *pvParameters );
\r
110 /* A variable which is incremented every time the dynamic tasks are created. This
\r
111 is used to check that the task is still running. */
\r
112 static volatile short sCreationCount = 0;
\r
114 /* Used to store the number of tasks that were originally running so the creator
\r
115 task can tell if any of the suicidal tasks have failed to die. */
\r
116 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
\r
117 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
\r
119 /* Used to store a handle to the tasks that should be killed by a suicidal task,
\r
120 before it kills itself. */
\r
121 xTaskHandle xCreatedTask1, xCreatedTask2;
\r
123 /*-----------------------------------------------------------*/
\r
125 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
\r
127 unsigned portBASE_TYPE *puxPriority;
\r
129 /* Create the Creator tasks - passing in as a parameter the priority at which
\r
130 the suicidal tasks should be created. */
\r
131 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
\r
132 *puxPriority = uxPriority;
\r
134 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
\r
136 /* Record the number of tasks that are running now so we know if any of the
\r
137 suicidal tasks have failed to be killed. */
\r
138 uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
\r
140 /*-----------------------------------------------------------*/
\r
142 static void vSuicidalTask( void *pvParameters )
\r
145 xTaskHandle xTaskToKill;
\r
146 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;
\r
148 if( pvParameters != NULL )
\r
150 /* This task is periodically created four times. Tow created tasks are
\r
151 passed a handle to the other task so it can kill it before killing itself.
\r
152 The other task is passed in null. */
\r
153 xTaskToKill = *( xTaskHandle* )pvParameters;
\r
157 xTaskToKill = NULL;
\r
162 /* Do something random just to use some stack and registers. */
\r
166 vTaskDelay( xDelay );
\r
168 if( xTaskToKill != NULL )
\r
170 /* Make sure the other task has a go before we delete it. */
\r
171 vTaskDelay( ( portTickType ) 0 );
\r
172 /* Kill the other task that was created by vCreateTasks(). */
\r
173 vTaskDelete( xTaskToKill );
\r
174 /* Kill ourselves. */
\r
175 vTaskDelete( NULL );
\r
178 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
179 /*-----------------------------------------------------------*/
\r
181 static void vCreateTasks( void *pvParameters )
\r
183 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
\r
184 unsigned portBASE_TYPE uxPriority;
\r
185 const char * const pcTaskStartMsg = "Create task started.\r\n";
\r
187 /* Queue a message for printing to say the task has started. */
\r
188 vPrintDisplayMessage( &pcTaskStartMsg );
\r
190 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
\r
191 vPortFree( pvParameters );
\r
195 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
196 vTaskDelay( xDelay );
\r
198 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
\r
199 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
\r
201 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
\r
202 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );
\r
207 /*-----------------------------------------------------------*/
\r
209 /* This is called to check that the creator task is still running and that there
\r
210 are not any more than four extra tasks. */
\r
211 portBASE_TYPE xIsCreateTaskStillRunning( void )
\r
213 static short sLastCreationCount = 0;
\r
214 short sReturn = pdTRUE;
\r
215 unsigned portBASE_TYPE uxTasksRunningNow;
\r
217 if( sLastCreationCount == sCreationCount )
\r
222 uxTasksRunningNow = uxTaskGetNumberOfTasks();
\r
224 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
228 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
234 /* Everything is okay. */
\r