FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Full / death.c
blob6cd5dd2a46fddf3f3ac1f7e6dd12d5d541acf26c
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
54 /**
55 * Create a single persistent task which periodically dynamically creates another
56 * four tasks. The original task is called the creator task, the four tasks it
57 * creates are called suicidal tasks.
59 * Two of the created suicidal tasks kill one other suicidal task before killing
60 * themselves - leaving just the original task remaining.
62 * The creator task must be spawned after all of the other demo application tasks
63 * as it keeps a check on the number of tasks under the scheduler control. The
64 * number of tasks it expects to see running should never be greater than the
65 * number of tasks that were in existence when the creator task was spawned, plus
66 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
68 * \page DeathC death.c
69 * \ingroup DemoFiles
70 * <HR>
74 Changes from V2.0.0
76 + Delay periods are now specified using variables and constants of
77 portTickType rather than unsigned long.
80 #include <stdlib.h>
82 /* Scheduler include files. */
83 #include "FreeRTOS.h"
84 #include "task.h"
86 /* Demo program include files. */
87 #include "death.h"
88 #include "print.h"
90 #define deathSTACK_SIZE ( ( unsigned short ) 512 )
92 /* The task originally created which is responsible for periodically dynamically
93 creating another four tasks. */
94 static void vCreateTasks( void *pvParameters );
96 /* The task function of the dynamically created tasks. */
97 static void vSuicidalTask( void *pvParameters );
99 /* A variable which is incremented every time the dynamic tasks are created. This
100 is used to check that the task is still running. */
101 static volatile short sCreationCount = 0;
103 /* Used to store the number of tasks that were originally running so the creator
104 task can tell if any of the suicidal tasks have failed to die. */
105 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
106 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;
108 /* Used to store a handle to the tasks that should be killed by a suicidal task,
109 before it kills itself. */
110 xTaskHandle xCreatedTask1, xCreatedTask2;
112 /*-----------------------------------------------------------*/
114 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
116 unsigned portBASE_TYPE *puxPriority;
118 /* Create the Creator tasks - passing in as a parameter the priority at which
119 the suicidal tasks should be created. */
120 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
121 *puxPriority = uxPriority;
123 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
125 /* Record the number of tasks that are running now so we know if any of the
126 suicidal tasks have failed to be killed. */
127 uxTasksRunningAtStart = uxTaskGetNumberOfTasks();
129 /*-----------------------------------------------------------*/
131 static void vSuicidalTask( void *pvParameters )
133 portDOUBLE d1, d2;
134 xTaskHandle xTaskToKill;
135 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;
137 if( pvParameters != NULL )
139 /* This task is periodically created four times. Tow created tasks are
140 passed a handle to the other task so it can kill it before killing itself.
141 The other task is passed in null. */
142 xTaskToKill = *( xTaskHandle* )pvParameters;
144 else
146 xTaskToKill = NULL;
149 for( ;; )
151 /* Do something random just to use some stack and registers. */
152 d1 = 2.4;
153 d2 = 89.2;
154 d2 *= d1;
155 vTaskDelay( xDelay );
157 if( xTaskToKill != NULL )
159 /* Make sure the other task has a go before we delete it. */
160 vTaskDelay( ( portTickType ) 0 );
161 /* Kill the other task that was created by vCreateTasks(). */
162 vTaskDelete( xTaskToKill );
163 /* Kill ourselves. */
164 vTaskDelete( NULL );
167 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
168 /*-----------------------------------------------------------*/
170 static void vCreateTasks( void *pvParameters )
172 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
173 unsigned portBASE_TYPE uxPriority;
174 const char * const pcTaskStartMsg = "Create task started.\r\n";
176 /* Queue a message for printing to say the task has started. */
177 vPrintDisplayMessage( &pcTaskStartMsg );
179 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
180 vPortFree( pvParameters );
182 for( ;; )
184 /* Just loop round, delaying then creating the four suicidal tasks. */
185 vTaskDelay( xDelay );
187 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );
188 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );
190 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );
191 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );
193 ++sCreationCount;
196 /*-----------------------------------------------------------*/
198 /* This is called to check that the creator task is still running and that there
199 are not any more than four extra tasks. */
200 portBASE_TYPE xIsCreateTaskStillRunning( void )
202 static short sLastCreationCount = 0;
203 short sReturn = pdTRUE;
204 unsigned portBASE_TYPE uxTasksRunningNow;
206 if( sLastCreationCount == sCreationCount )
208 sReturn = pdFALSE;
211 uxTasksRunningNow = uxTaskGetNumberOfTasks();
213 if( uxTasksRunningNow < uxTasksRunningAtStart )
215 sReturn = pdFALSE;
217 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
219 sReturn = pdFALSE;
221 else
223 /* Everything is okay. */
226 return sReturn;