]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/death.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / Common / Minimal / death.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\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
12      *                                                                       *\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
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \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
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \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
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \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
54 \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
58 \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
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /**\r
67  * Create a single persistent task which periodically dynamically creates another\r
68  * two tasks.  The original task is called the creator task, the two tasks it\r
69  * creates are called suicidal tasks.\r
70  *\r
71  * One of the created suicidal tasks kill one other suicidal task before killing\r
72  * itself - leaving just the original task remaining.\r
73  *\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
79  *\r
80  * \page DeathC death.c\r
81  * \ingroup DemoFiles\r
82  * <HR>\r
83  */\r
84 \r
85 \r
86 #include <stdlib.h>\r
87 \r
88 /* Scheduler include files. */\r
89 #include "FreeRTOS.h"\r
90 #include "task.h"\r
91 \r
92 /* Demo program include files. */\r
93 #include "death.h"\r
94 \r
95 #define deathSTACK_SIZE         ( configMINIMAL_STACK_SIZE + 60 )\r
96 \r
97 /* The task originally created which is responsible for periodically dynamically\r
98 creating another four tasks. */\r
99 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );\r
100 \r
101 /* The task function of the dynamically created tasks. */\r
102 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );\r
103 \r
104 /* A variable which is incremented every time the dynamic tasks are created.  This\r
105 is used to check that the task is still running. */\r
106 static volatile uint16_t usCreationCount = 0;\r
107 \r
108 /* Used to store the number of tasks that were originally running so the creator\r
109 task can tell if any of the suicidal tasks have failed to die.\r
110 */\r
111 static volatile UBaseType_t uxTasksRunningAtStart = 0;\r
112 \r
113 /* Tasks are deleted by the idle task.  Under heavy load the idle task might\r
114 not get much processing time, so it would be legitimate for several tasks to\r
115 remain undeleted for a short period. */\r
116 static const UBaseType_t uxMaxNumberOfExtraTasksRunning = 3;\r
117 \r
118 /* Used to store a handle to the task that should be killed by a suicidal task,\r
119 before it kills itself. */\r
120 TaskHandle_t xCreatedTask;\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 void vCreateSuicidalTasks( UBaseType_t uxPriority )\r
125 {\r
126 UBaseType_t *puxPriority;\r
127 \r
128         /* Create the Creator tasks - passing in as a parameter the priority at which\r
129         the suicidal tasks should be created. */\r
130         puxPriority = ( UBaseType_t * ) pvPortMalloc( sizeof( UBaseType_t ) );\r
131         *puxPriority = uxPriority;\r
132 \r
133         xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
134 \r
135         /* Record the number of tasks that are running now so we know if any of the\r
136         suicidal tasks have failed to be killed. */\r
137         uxTasksRunningAtStart = ( UBaseType_t ) uxTaskGetNumberOfTasks();\r
138         \r
139         /* FreeRTOS.org versions before V3.0 started the idle-task as the very\r
140         first task. The idle task was then already included in uxTasksRunningAtStart.\r
141         From FreeRTOS V3.0 on, the idle task is started when the scheduler is\r
142         started. Therefore the idle task is not yet accounted for. We correct\r
143         this by increasing uxTasksRunningAtStart by 1. */\r
144         uxTasksRunningAtStart++;\r
145         \r
146         /* From FreeRTOS version 7.0.0 can optionally create a timer service task.  \r
147         If this is done, then uxTasksRunningAtStart needs incrementing again as that\r
148         too is created when the scheduler is started. */\r
149         #if configUSE_TIMERS == 1\r
150                 uxTasksRunningAtStart++;\r
151         #endif\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154                                         \r
155 static portTASK_FUNCTION( vSuicidalTask, pvParameters )\r
156 {\r
157 volatile long l1, l2;\r
158 TaskHandle_t xTaskToKill;\r
159 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;\r
160 \r
161         if( pvParameters != NULL )\r
162         {\r
163                 /* This task is periodically created four times.  Two created tasks are\r
164                 passed a handle to the other task so it can kill it before killing itself.\r
165                 The other task is passed in null. */\r
166                 xTaskToKill = *( TaskHandle_t* )pvParameters;\r
167         }\r
168         else\r
169         {\r
170                 xTaskToKill = NULL;\r
171         }\r
172 \r
173         for( ;; )\r
174         {\r
175                 /* Do something random just to use some stack and registers. */\r
176                 l1 = 2;\r
177                 l2 = 89;\r
178                 l2 *= l1;\r
179                 vTaskDelay( xDelay );\r
180 \r
181                 if( xTaskToKill != NULL )\r
182                 {\r
183                         /* Make sure the other task has a go before we delete it. */\r
184                         vTaskDelay( ( TickType_t ) 0 );\r
185 \r
186                         /* Kill the other task that was created by vCreateTasks(). */\r
187                         vTaskDelete( xTaskToKill );\r
188 \r
189                         /* Kill ourselves. */\r
190                         vTaskDelete( NULL );\r
191                 }\r
192         }\r
193 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static portTASK_FUNCTION( vCreateTasks, pvParameters )\r
197 {\r
198 const TickType_t xDelay = ( TickType_t ) 1000 / portTICK_PERIOD_MS;\r
199 UBaseType_t uxPriority;\r
200 \r
201         uxPriority = *( UBaseType_t * ) pvParameters;\r
202         vPortFree( pvParameters );\r
203 \r
204         for( ;; )\r
205         {\r
206                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
207                 vTaskDelay( xDelay );\r
208 \r
209                 xCreatedTask = NULL;\r
210 \r
211                 xTaskCreate( vSuicidalTask, "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );\r
212                 xTaskCreate( vSuicidalTask, "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );\r
213 \r
214                 ++usCreationCount;\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 /* This is called to check that the creator task is still running and that there\r
220 are not any more than four extra tasks. */\r
221 BaseType_t xIsCreateTaskStillRunning( void )\r
222 {\r
223 static uint16_t usLastCreationCount = 0xfff;\r
224 BaseType_t xReturn = pdTRUE;\r
225 static UBaseType_t uxTasksRunningNow;\r
226 \r
227         if( usLastCreationCount == usCreationCount )\r
228         {\r
229                 xReturn = pdFALSE;\r
230         }\r
231         else\r
232         {\r
233                 usLastCreationCount = usCreationCount;\r
234         }\r
235         \r
236         uxTasksRunningNow = ( UBaseType_t ) uxTaskGetNumberOfTasks();\r
237 \r
238         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
239         {\r
240                 xReturn = pdFALSE;\r
241         }\r
242         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
243         {\r
244                 xReturn = pdFALSE;\r
245         }\r
246         else\r
247         {\r
248                 /* Everything is okay. */\r
249         }\r
250 \r
251         return xReturn;\r
252 }\r
253 \r
254 \r