]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/death.c
Comment the command line interpreter and lwIP sockets based server code.
[freertos] / Demo / Common / Full / death.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /**\r
55  * Create a single persistent task which periodically dynamically creates another \r
56  * four tasks.  The original task is called the creator task, the four tasks it \r
57  * creates are called suicidal tasks.\r
58  *\r
59  * Two of the created suicidal tasks kill one other suicidal task before killing \r
60  * themselves - leaving just the original task remaining.  \r
61  *\r
62  * The creator task must be spawned after all of the other demo application tasks \r
63  * as it keeps a check on the number of tasks under the scheduler control.  The \r
64  * number of tasks it expects to see running should never be greater than the \r
65  * number of tasks that were in existence when the creator task was spawned, plus \r
66  * one set of four suicidal tasks.  If this number is exceeded an error is flagged.\r
67  *\r
68  * \page DeathC death.c\r
69  * \ingroup DemoFiles\r
70  * <HR>\r
71  */\r
72 \r
73 /*\r
74 Changes from V2.0.0\r
75 \r
76         + Delay periods are now specified using variables and constants of\r
77           portTickType rather than unsigned long.\r
78 */\r
79 \r
80 #include <stdlib.h>\r
81 \r
82 /* Scheduler include files. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 \r
86 /* Demo program include files. */\r
87 #include "death.h"\r
88 #include "print.h"\r
89 \r
90 #define deathSTACK_SIZE         ( ( unsigned short ) 512 )\r
91 \r
92 /* The task originally created which is responsible for periodically dynamically \r
93 creating another four tasks. */\r
94 static void vCreateTasks( void *pvParameters );\r
95 \r
96 /* The task function of the dynamically created tasks. */\r
97 static void vSuicidalTask( void *pvParameters );\r
98 \r
99 /* A variable which is incremented every time the dynamic tasks are created.  This \r
100 is used to check that the task is still running. */\r
101 static volatile short sCreationCount = 0;\r
102 \r
103 /* Used to store the number of tasks that were originally running so the creator \r
104 task can tell if any of the suicidal tasks have failed to die. */\r
105 static volatile unsigned portBASE_TYPE uxTasksRunningAtStart = 0;\r
106 static const unsigned portBASE_TYPE uxMaxNumberOfExtraTasksRunning = 5;\r
107 \r
108 /* Used to store a handle to the tasks that should be killed by a suicidal task, \r
109 before it kills itself. */\r
110 xTaskHandle xCreatedTask1, xCreatedTask2;\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 void vCreateSuicidalTasks( unsigned portBASE_TYPE uxPriority )\r
115 {\r
116 unsigned portBASE_TYPE *puxPriority;\r
117 \r
118         /* Create the Creator tasks - passing in as a parameter the priority at which \r
119         the suicidal tasks should be created. */\r
120         puxPriority = ( unsigned portBASE_TYPE * ) pvPortMalloc( sizeof( unsigned portBASE_TYPE ) );\r
121         *puxPriority = uxPriority;\r
122 \r
123         xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) puxPriority, uxPriority, NULL );\r
124 \r
125         /* Record the number of tasks that are running now so we know if any of the \r
126         suicidal tasks have failed to be killed. */\r
127         uxTasksRunningAtStart = uxTaskGetNumberOfTasks();\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 static void vSuicidalTask( void *pvParameters )\r
132 {\r
133 portDOUBLE d1, d2;\r
134 xTaskHandle xTaskToKill;\r
135 const portTickType xDelay = ( portTickType ) 500 / portTICK_RATE_MS;\r
136 \r
137         if( pvParameters != NULL )\r
138         {\r
139                 /* This task is periodically created four times.  Tow created tasks are \r
140                 passed a handle to the other task so it can kill it before killing itself.  \r
141                 The other task is passed in null. */\r
142                 xTaskToKill = *( xTaskHandle* )pvParameters;\r
143         }\r
144         else\r
145         {\r
146                 xTaskToKill = NULL;\r
147         }\r
148 \r
149         for( ;; )\r
150         {\r
151                 /* Do something random just to use some stack and registers. */\r
152                 d1 = 2.4;\r
153                 d2 = 89.2;\r
154                 d2 *= d1;\r
155                 vTaskDelay( xDelay );\r
156 \r
157                 if( xTaskToKill != NULL )\r
158                 {\r
159                         /* Make sure the other task has a go before we delete it. */\r
160                         vTaskDelay( ( portTickType ) 0 );\r
161                         /* Kill the other task that was created by vCreateTasks(). */\r
162                         vTaskDelete( xTaskToKill );\r
163                         /* Kill ourselves. */\r
164                         vTaskDelete( NULL );\r
165                 }\r
166         }\r
167 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void vCreateTasks( void *pvParameters )\r
171 {\r
172 const portTickType xDelay = ( portTickType ) 1000 / portTICK_RATE_MS;\r
173 unsigned portBASE_TYPE uxPriority;\r
174 const char * const pcTaskStartMsg = "Create task started.\r\n";\r
175 \r
176         /* Queue a message for printing to say the task has started. */\r
177         vPrintDisplayMessage( &pcTaskStartMsg );\r
178 \r
179         uxPriority = *( unsigned portBASE_TYPE * ) pvParameters;\r
180         vPortFree( pvParameters );\r
181 \r
182         for( ;; )\r
183         {\r
184                 /* Just loop round, delaying then creating the four suicidal tasks. */\r
185                 vTaskDelay( xDelay );\r
186 \r
187                 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask1 );\r
188                 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask1, uxPriority, NULL );\r
189 \r
190                 xTaskCreate( vSuicidalTask, "SUICIDE1", deathSTACK_SIZE, NULL, uxPriority, &xCreatedTask2 );\r
191                 xTaskCreate( vSuicidalTask, "SUICIDE2", deathSTACK_SIZE, &xCreatedTask2, uxPriority, NULL );\r
192 \r
193                 ++sCreationCount;\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 /* This is called to check that the creator task is still running and that there \r
199 are not any more than four extra tasks. */\r
200 portBASE_TYPE xIsCreateTaskStillRunning( void )\r
201 {\r
202 static short sLastCreationCount = 0;\r
203 short sReturn = pdTRUE;\r
204 unsigned portBASE_TYPE uxTasksRunningNow;\r
205 \r
206         if( sLastCreationCount == sCreationCount )\r
207         {\r
208                 sReturn = pdFALSE;\r
209         }\r
210         \r
211         uxTasksRunningNow = uxTaskGetNumberOfTasks();\r
212 \r
213         if( uxTasksRunningNow < uxTasksRunningAtStart )\r
214         {\r
215                 sReturn = pdFALSE;\r
216         }\r
217         else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )\r
218         {\r
219                 sReturn = pdFALSE;\r
220         }\r
221         else\r
222         {\r
223                 /* Everything is okay. */\r
224         }\r
225 \r
226         return sReturn;\r
227 }\r
228 \r
229 \r