FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Minimal / death.c
blobd0954d681abce1bfb6b2bbd49eff5ec73da75741
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 * two tasks. The original task is called the creator task, the two tasks it
57 * creates are called suicidal tasks.
59 * One of the created suicidal tasks kill one other suicidal task before killing
60 * itself - 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 V3.0.0
75 + CreationCount sizes changed from unsigned portBASE_TYPE to
76 unsigned short to minimize the risk of overflowing.
78 + Reset of usLastCreationCount added
80 Changes from V3.1.0
81 + Changed the dummy calculation to use variables of type long, rather than
82 float. This allows the file to be used with ports that do not support
83 floating point.
87 #include <stdlib.h>
89 /* Scheduler include files. */
90 #include "FreeRTOS.h"
91 #include "task.h"
93 /* Demo program include files. */
94 #include "death.h"
96 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
98 /* The task originally created which is responsible for periodically dynamically
99 creating another four tasks. */
100 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
102 /* The task function of the dynamically created tasks. */
103 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
105 /* A variable which is incremented every time the dynamic tasks are created. This
106 is used to check that the task is still running. */
107 static volatile unsigned short usCreationCount = 0;
109 /* Used to store the number of tasks that were originally running so the creator
110 task can tell if any of the suicidal tasks have failed to die.
112 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;
114 /* Tasks are deleted by the idle task. Under heavy load the idle task might
115 not get much processing time, so it would be legitimate for several tasks to
116 remain undeleted for a short period. */
117 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 2;
119 /* Used to store a handle to the task that should be killed by a suicidal task,
120 before it kills itself. */
121 xTaskHandle xCreatedTask;
123 /*-----------------------------------------------------------*/
125 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )
127 unsigned portBASE_TYPE *puxPriority;
129 /* Create the Creator tasks - passing in as a parameter the priority at which
130 the suicidal tasks should be created. */
131 puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );
132 *puxPriority = uxPriority;
134 xTaskCreate( vCreateTasks, ( signed char * ) "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );
136 /* Record the number of tasks that are running now so we know if any of the
137 suicidal tasks have failed to be killed. */
138 uxTasksRunningAtStart = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
140 /* FreeRTOS.org versions before V3.0 started the idle-task as the very
141 first task. The idle task was then already included in uxTasksRunningAtStart.
142 From FreeRTOS V3.0 on, the idle task is started when the scheduler is
143 started. Therefore the idle task is not yet accounted for. We correct
144 this by increasing uxTasksRunningAtStart by 1. */
145 uxTasksRunningAtStart++;
147 /*-----------------------------------------------------------*/
149 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
151 volatile long l1, l2;
152 xTaskHandle xTaskToKill;
153 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
155 if( pvParameters != NULL )
157 /* This task is periodically created four times. Two created tasks are
158 passed a handle to the other task so it can kill it before killing itself.
159 The other task is passed in null. */
160 xTaskToKill = *( xTaskHandle* )pvParameters;
162 else
164 xTaskToKill = NULL;
167 for( ;; )
169 /* Do something random just to use some stack and registers. */
170 l1 = 2;
171 l2 = 89;
172 l2 *= l1;
173 vTaskDelay( xDelay );
175 if( xTaskToKill != NULL )
177 /* Make sure the other task has a go before we delete it. */
178 vTaskDelay( ( portTickType ) 0 );
180 /* Kill the other task that was created by vCreateTasks(). */
181 vTaskDelete( xTaskToKill );
183 /* Kill ourselves. */
184 vTaskDelete( NULL );
187 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
188 /*-----------------------------------------------------------*/
190 static portTASK_FUNCTION( vCreateTasks, pvParameters )
192 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;
193 unsigned portBASE_TYPE uxPriority;
195 uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;
196 vPortFree( pvParameters );
198 for( ;; )
200 /* Just loop round, delaying then creating the four suicidal tasks. */
201 vTaskDelay( xDelay );
203 xCreatedTask = NULL;
205 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
206 xTaskCreate( vSuicidalTask, ( signed char * ) "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
208 ++usCreationCount;
211 /*-----------------------------------------------------------*/
213 /* This is called to check that the creator task is still running and that there
214 are not any more than four extra tasks. */
215 portBASE_TYPE xIsCreateTaskStillRunning( void )
217 static unsigned short usLastCreationCount = 0xfff;
218 portBASE_TYPE xReturn = pdTRUE;
219 static unsigned portBASE_TYPE uxTasksRunningNow;
221 if( usLastCreationCount == usCreationCount )
223 xReturn = pdFALSE;
225 else
227 usLastCreationCount = usCreationCount;
230 uxTasksRunningNow = ( unsigned portBASE_TYPE ) uxTaskGetNumberOfTasks();
232 if( uxTasksRunningNow < uxTasksRunningAtStart )
234 xReturn = pdFALSE;
236 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
238 xReturn = pdFALSE;
240 else
242 /* Everything is okay. */
245 return xReturn;