]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/death.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Demo / Common / Full / death.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /**\r
68  * Create a single persistent task which periodically dynamically creates another \r
69  * four tasks.  The original task is called the creator task, the four tasks it \r
70  * creates are called suicidal tasks.\r
71  *\r
72  * Two of the created suicidal tasks kill one other suicidal task before killing \r
73  * themselves - leaving just the original task remaining.  \r
74  *\r
75  * The creator task must be spawned after all of the other demo application tasks \r
76  * as it keeps a check on the number of tasks under the scheduler control.  The \r
77  * number of tasks it expects to see running should never be greater than the \r
78  * number of tasks that were in existence when the creator task was spawned, plus \r
79  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
80  *\r
81  * \page DeathC death.c\r
82  * \ingroup DemoFiles\r
83  * <HR>\r
84  */\r
85 \r
86 /*\r
87 Changes from V2.0.0\r
88 \r
89         + Delay periods are now specified using variables and constants of\r
90           portTickType rather than unsigned long.\r
91 */\r
92 \r
93 #include <stdlib.h>\r
94 \r
95 /* Scheduler include files. */\r
96 #include "FreeRTOS.h"\r
97 #include "task.h"\r
98 \r
99 /* Demo program include files. */\r
100 #include "death.h"\r
101 #include "print.h"\r
102 \r
103 #define deathSTACK_SIZE         ( ( unsigned short ) 512 )\r
104 \r
105 /* The task originally created which is responsible for periodically dynamically \r
106 creating another four tasks. */\r
107 static void vCreateTasks( void *pvParameters );\r
108 \r
109 /* The task function of the dynamically created tasks. */\r
110 static void vSuicidalTask( void *pvParameters );\r
111 \r
112 /* A variable which is incremented every time the dynamic tasks are created.  This \r
113 is used to check that the task is still running. */\r
114 static volatile short sCreationCount = 0;\r
115 \r
116 /* Used to store the number of tasks that were originally running so the creator \r
117 task can tell if any of the suicidal tasks have failed to die. */\r
118 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;\r
119 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;\r
120 \r
121 /* Used to store a handle to the tasks that should be killed by a suicidal task, \r
122 before it kills itself. */\r
123 xTaskHandle xCreatedTask1, xCreatedTask2;\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )\r
128 {\r
129 unsigned portBASE_TYPE *puxPriority;\r
130 \r
131         /* Create the Creator tasks - passing in as a parameter the priority at which \r
132         the suicidal tasks should be created. */\r
133         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );\r
134         *puxPriority = uxPriority;\r
135 \r
136         xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
137 \r
138         /* Record the number of tasks that are running now so we know if any of the \r
139         suicidal tasks have failed to be killed. */\r
140         uxTasksRunningAtStart = uxTaskGetNumberOfTasks();\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 static void vSuicidalTask( void *pvParameters )\r
145 {\r
146 portDOUBLE d1, d2;\r
147 xTaskHandle xTaskToKill;\r
148 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;\r
149 \r
150         if( pvParameters != NULL )\r
151         {\r
152                 /* This task is periodically created four times.  Tow created tasks are \r
153                 passed a handle to the other task so it can kill it before killing itself.  \r
154                 The other task is passed in null. */\r
155                 xTaskToKill = *( xTaskHandle* )pvParameters;\r
156         }\r
157         else\r
158         {\r
159                 xTaskToKill = NULL;\r
160         }\r
161 \r
162         for( ;; )\r
163         {\r
164                 /* Do something random just to use some stack and registers. */\r
165                 d1 = 2.4;\r
166                 d2 = 89.2;\r
167                 d2 *= d1;\r
168                 vTaskDelay( xDelay );\r
169 \r
170                 if( xTaskToKill != NULL )\r
171                 {\r
172                         /* Make sure the other task has a go before we delete it. */\r
173                         vTaskDelay( ( portTickType ) 0 );\r
174                         /* Kill the other task that was created by vCreateTasks(). */\r
175                         vTaskDelete( xTaskToKill );\r
176                         /* Kill ourselves. */\r
177                         vTaskDelete( NULL );\r
178                 }\r
179         }\r
180 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static void vCreateTasks( void *pvParameters )\r
184 {\r
185 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;\r
186 unsigned portBASE_TYPE uxPriority;\r
187 const char * const pcTaskStartMsg = "Create task started.\r\n";\r
188 \r
189         /* Queue a message for printing to say the task has started. */\r
190         vPrintDisplayMessage( &pcTaskStartMsg );\r
191 \r
192         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
193         vPortFree( pvParameters );\r
194 \r
195         for( ;; )\r
196         {\r
197                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
198                 vTaskDelay( xDelay );\r
199 \r
200                 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );\r
201                 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );\r
202 \r
203                 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );\r
204                 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );\r
205 \r
206                 ++sCreationCount;\r
207         }\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 /* This is called to check that the creator task is still running and that there \r
212 are not any more than four extra tasks. */\r
213 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
214 {\r
215 static short sLastCreationCount = 0;\r
216 short sReturn = pdTRUE;\r
217 unsigned portBASE_TYPE uxTasksRunningNow;\r
218 \r
219         if( sLastCreationCount == sCreationCount )\r
220         {\r
221                 sReturn = pdFALSE;\r
222         }\r
223         \r
224         uxTasksRunningNow = uxTaskGetNumberOfTasks();\r
225 \r
226         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
227         {\r
228                 sReturn = pdFALSE;\r
229         }\r
230         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
231         {\r
232                 sReturn = pdFALSE;\r
233         }\r
234         else\r
235         {\r
236                 /* Everything is okay. */\r
237         }\r
238 \r
239         return sReturn;\r
240 }\r
241 \r
242 \r