2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Create a single persistent task which periodically dynamically creates another
\r
31 * two tasks. The original task is called the creator task, the two tasks it
\r
32 * creates are called suicidal tasks.
\r
34 * One of the created suicidal tasks kill one other suicidal task before killing
\r
35 * itself - leaving just the original task remaining.
\r
37 * The creator task must be spawned after all of the other demo application tasks
\r
38 * as it keeps a check on the number of tasks under the scheduler control. The
\r
39 * number of tasks it expects to see running should never be greater than the
\r
40 * number of tasks that were in existence when the creator task was spawned, plus
\r
41 * one set of four suicidal tasks. If this number is exceeded an error is flagged.
\r
43 * \page DeathC death.c
\r
44 * \ingroup DemoFiles
\r
51 /* Scheduler include files. */
\r
52 #include "FreeRTOS.h"
\r
55 /* Demo program include files. */
\r
58 #define deathSTACK_SIZE ( configMINIMAL_STACK_SIZE + 60 )
\r
60 /* The task originally created which is responsible for periodically dynamically
\r
61 creating another four tasks. */
\r
62 static portTASK_FUNCTION_PROTO( vCreateTasks, pvParameters );
\r
64 /* The task function of the dynamically created tasks. */
\r
65 static portTASK_FUNCTION_PROTO( vSuicidalTask, pvParameters );
\r
67 /* A variable which is incremented every time the dynamic tasks are created. This
\r
68 is used to check that the task is still running. */
\r
69 static volatile uint16_t usCreationCount = 0;
\r
71 /* Used to store the number of tasks that were originally running so the creator
\r
72 task can tell if any of the suicidal tasks have failed to die.
\r
74 static volatile UBaseType_t uxTasksRunningAtStart = 0;
\r
76 /* When a task deletes itself, it stack and TCB are cleaned up by the Idle task.
\r
77 Under heavy load the idle task might not get much processing time, so it would
\r
78 be legitimate for several tasks to remain undeleted for a short period. There
\r
79 may also be a few other unexpected tasks if, for example, the tasks that test
\r
80 static allocation are also being used. */
\r
81 static const UBaseType_t uxMaxNumberOfExtraTasksRunning = 3;
\r
83 /* Used to store a handle to the task that should be killed by a suicidal task,
\r
84 before it kills itself. */
\r
85 TaskHandle_t xCreatedTask;
\r
87 /*-----------------------------------------------------------*/
\r
89 void vCreateSuicidalTasks( UBaseType_t uxPriority )
\r
91 xTaskCreate( vCreateTasks, "CREATOR", deathSTACK_SIZE, ( void * ) NULL, uxPriority, NULL );
\r
93 /*-----------------------------------------------------------*/
\r
95 static portTASK_FUNCTION( vSuicidalTask, pvParameters )
\r
97 volatile long l1, l2;
\r
98 TaskHandle_t xTaskToKill;
\r
99 const TickType_t xDelay = pdMS_TO_TICKS( ( TickType_t ) 200 );
\r
101 /* Test deletion of a task's secure context, if any. */
\r
102 portTASK_CALLS_SECURE_FUNCTIONS();
\r
104 if( pvParameters != NULL )
\r
106 /* This task is periodically created four times. Two created tasks are
\r
107 passed a handle to the other task so it can kill it before killing itself.
\r
108 The other task is passed in null. */
\r
109 xTaskToKill = *( TaskHandle_t* )pvParameters;
\r
113 xTaskToKill = NULL;
\r
118 /* Do something random just to use some stack and registers. */
\r
122 vTaskDelay( xDelay );
\r
124 if( xTaskToKill != NULL )
\r
126 /* Make sure the other task has a go before we delete it. */
\r
127 vTaskDelay( ( TickType_t ) 0 );
\r
129 /* Kill the other task that was created by vCreateTasks(). */
\r
130 vTaskDelete( xTaskToKill );
\r
132 /* Kill ourselves. */
\r
133 vTaskDelete( NULL );
\r
136 }/*lint !e818 !e550 Function prototype must be as per standard for task functions. */
\r
137 /*-----------------------------------------------------------*/
\r
139 static portTASK_FUNCTION( vCreateTasks, pvParameters )
\r
141 const TickType_t xDelay = pdMS_TO_TICKS( ( TickType_t ) 1000 );
\r
142 UBaseType_t uxPriority;
\r
144 /* Remove compiler warning about unused parameter. */
\r
145 ( void ) pvParameters;
\r
147 /* Delay at the start to ensure tasks created by other demos have been
\r
148 created before storing the current number of tasks. */
\r
149 vTaskDelay( xDelay );
\r
150 uxTasksRunningAtStart = ( UBaseType_t ) uxTaskGetNumberOfTasks();
\r
152 uxPriority = uxTaskPriorityGet( NULL );
\r
156 /* Just loop round, delaying then creating the four suicidal tasks. */
\r
157 vTaskDelay( xDelay );
\r
159 xCreatedTask = NULL;
\r
161 xTaskCreate( vSuicidalTask, "SUICID1", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xCreatedTask );
\r
162 xTaskCreate( vSuicidalTask, "SUICID2", configMINIMAL_STACK_SIZE, &xCreatedTask, uxPriority, NULL );
\r
167 /*-----------------------------------------------------------*/
\r
169 /* This is called to check that the creator task is still running and that there
\r
170 are not any more than four extra tasks. */
\r
171 BaseType_t xIsCreateTaskStillRunning( void )
\r
173 static uint16_t usLastCreationCount = 0xfff;
\r
174 BaseType_t xReturn = pdTRUE;
\r
175 static UBaseType_t uxTasksRunningNow;
\r
177 if( usLastCreationCount == usCreationCount )
\r
183 usLastCreationCount = usCreationCount;
\r
186 uxTasksRunningNow = ( UBaseType_t ) uxTaskGetNumberOfTasks();
\r
188 if( uxTasksRunningNow < uxTasksRunningAtStart )
\r
192 else if( ( uxTasksRunningNow - uxTasksRunningAtStart ) > uxMaxNumberOfExtraTasksRunning )
\r
198 /* Everything is okay. */
\r