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