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