2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Create a single persistent task which periodically dynamically creates another
\r
30 * two tasks. The original task is called the creator task, the two tasks it
\r
31 * creates are called suicidal tasks.
\r
33 * One of the created suicidal tasks kill one other suicidal task before killing
\r
34 * itself - leaving just the original task remaining.
\r
36 * The creator task must be spawned after all of the other demo application tasks
\r
37 * as it keeps a check on the number of tasks under the scheduler control. The
\r
38 * number of tasks it expects to see running should never be greater than the
\r
39 * number of tasks that were in existence when the creator task was spawned, plus
\r
40 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
42 * \page DeathC death.c
\r
43 * \ingroup DemoFiles
\r
50 /* Scheduler include files. */
\r
51 #include "FreeRTOS.h"
\r
54 /* Demo program include files. */
\r
57 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
59 /* The task originally created which is responsible for periodically dynamically
\r
60 creating another four tasks. */
\r
61 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
63 /* The task function of the dynamically created tasks. */
\r
64 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
66 /* A variable which is incremented every time the dynamic tasks are created. This
\r
67 is used to check that the task is still running. */
\r
68 static volatile uint16_t usCreationCount = 0;
\r
70 /* Used to store the number of tasks that were originally running so the creator
\r
71 task can tell if any of the suicidal tasks have failed to die.
\r
73 static volatile UBaseType_t uxTasksRunningAtStart = 0;
\r
75 /* When a task deletes itself, it stack and TCB are cleaned up by the Idle task.
\r
76 Under heavy load the idle task might not get much processing time, so it would
\r
77 be legitimate for several tasks to remain undeleted for a short period. There
\r
78 may also be a few other unexpected tasks if, for example, the tasks that test
\r
79 static allocation are also being used. */
\r
80 static const UBaseType_t uxMaxNumberOfExtraTasksRunning = 3;
\r
82 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
83 before it kills itself. */
\r
84 TaskHandle_t xCreatedTask;
\r
86 /*-----------------------------------------------------------*/
\r
88 void vCreateSuicidalTasks( UBaseType_t uxPriority )
\r
90 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) NULL, uxPriority, NULL );
\r
92 /*-----------------------------------------------------------*/
\r
94 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
96 volatile long l1, l2;
\r
97 TaskHandle_t xTaskToKill;
\r
98 const TickType_t xDelay = pdMS_TO_TICKS( ( TickType_t ) 200 );
\r
100 /* Test deletion of a task's secure context, if any. */
\r
101 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
\r
103 if( pvParameters != NULL )
\r
105 /* This task is periodically created four times. Two created tasks are
\r
106 passed a handle to the other task so it can kill it before killing itself.
\r
107 The other task is passed in null. */
\r
108 xTaskToKill = *( TaskHandle_t* )pvParameters;
\r
112 xTaskToKill = NULL;
\r
117 /* Do something random just to use some stack and registers. */
\r
121 vTaskDelay( xDelay );
\r
123 if( xTaskToKill != NULL )
\r
125 /* Make sure the other task has a go before we delete it. */
\r
126 vTaskDelay( ( TickType_t ) 0 );
\r
128 /* Kill the other task that was created by vCreateTasks(). */
\r
129 vTaskDelete( xTaskToKill );
\r
131 /* Kill ourselves. */
\r
132 vTaskDelete( NULL );
\r
135 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
136 /*-----------------------------------------------------------*/
\r
138 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
140 const TickType_t xDelay = pdMS_TO_TICKS( ( TickType_t ) 1000 );
\r
141 UBaseType_t uxPriority;
\r
143 /* Remove compiler warning about unused parameter. */
\r
144 ( void ) pvParameters;
\r
146 /* Delay at the start to ensure tasks created by other demos have been
\r
147 created before storing the current number of tasks. */
\r
148 vTaskDelay( xDelay );
\r
149 uxTasksRunningAtStart = ( UBaseType_t ) uxTaskGetNumberOfTasks();
\r
151 uxPriority = uxTaskPriorityGet( NULL );
\r
155 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
156 vTaskDelay( xDelay );
\r
158 xCreatedTask = NULL;
\r
160 xTaskCreate( vSuicidalTask, "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
161 xTaskCreate( vSuicidalTask, "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
166 /*-----------------------------------------------------------*/
\r
168 /* This is called to check that the creator task is still running and that there
\r
169 are not any more than four extra tasks. */
\r
170 BaseType_t xIsCreateTaskStillRunning( void )
\r
172 static uint16_t usLastCreationCount = 0xfff;
\r
173 BaseType_t xReturn = pdTRUE;
\r
174 static UBaseType_t uxTasksRunningNow;
\r
176 if( usLastCreationCount == usCreationCount )
\r
182 usLastCreationCount = usCreationCount;
\r
185 uxTasksRunningNow = ( UBaseType_t ) uxTaskGetNumberOfTasks();
\r
187 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
191 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
197 /* Everything is okay. */
\r