1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** ThreadX Component */
16 /** */
17 /** Thread */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define TX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "tx_api.h"
28 #include "tx_trace.h"
29 #include "tx_thread.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _tx_thread_delete PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* William E. Lamie, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function handles application delete thread requests. The */
45 /* thread to delete must be in a terminated or completed state, */
46 /* otherwise this function just returns an error code. */
47 /* */
48 /* INPUT */
49 /* */
50 /* thread_ptr Pointer to thread to suspend */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* status Return completion status */
55 /* */
56 /* CALLS */
57 /* */
58 /* None */
59 /* */
60 /* CALLED BY */
61 /* */
62 /* Application code */
63 /* */
64 /* RELEASE HISTORY */
65 /* */
66 /* DATE NAME DESCRIPTION */
67 /* */
68 /* 05-19-2020 William E. Lamie Initial Version 6.0 */
69 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
70 /* resulting in version 6.1 */
71 /* */
72 /**************************************************************************/
_tx_thread_delete(TX_THREAD * thread_ptr)73 UINT _tx_thread_delete(TX_THREAD *thread_ptr)
74 {
75
76 TX_INTERRUPT_SAVE_AREA
77
78 TX_THREAD *next_thread;
79 TX_THREAD *previous_thread;
80 UINT status;
81
82
83 /* Default status to success. */
84 status = TX_SUCCESS;
85
86 /* Lockout interrupts while the thread is being deleted. */
87 TX_DISABLE
88
89 /* Check for proper status of this thread to delete. */
90 if (thread_ptr -> tx_thread_state != TX_COMPLETED)
91 {
92
93 /* Now check for terminated state. */
94 if (thread_ptr -> tx_thread_state != TX_TERMINATED)
95 {
96
97 /* Restore interrupts. */
98 TX_RESTORE
99
100 /* Thread not completed or terminated - return an error! */
101 status = TX_DELETE_ERROR;
102 }
103 }
104
105 /* Determine if the delete operation is okay. */
106 if (status == TX_SUCCESS)
107 {
108
109 /* Yes, continue with deleting the thread. */
110
111 /* Perform any additional activities for tool or user purpose. */
112 TX_THREAD_DELETE_EXTENSION(thread_ptr)
113
114 /* If trace is enabled, insert this event into the trace buffer. */
115 TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_DELETE, thread_ptr, TX_POINTER_TO_ULONG_CONVERT(&next_thread), 0, 0, TX_TRACE_THREAD_EVENTS)
116
117 /* If trace is enabled, unregister this object. */
118 TX_TRACE_OBJECT_UNREGISTER(thread_ptr)
119
120 /* Log this kernel call. */
121 TX_EL_THREAD_DELETE_INSERT
122
123 /* Unregister thread in the thread array structure. */
124 TX_EL_THREAD_UNREGISTER(thread_ptr)
125
126 /* Clear the thread ID to make it invalid. */
127 thread_ptr -> tx_thread_id = TX_CLEAR_ID;
128
129 /* Decrement the number of created threads. */
130 _tx_thread_created_count--;
131
132 /* See if the thread is the only one on the list. */
133 if (_tx_thread_created_count == TX_EMPTY)
134 {
135
136 /* Only created thread, just set the created list to NULL. */
137 _tx_thread_created_ptr = TX_NULL;
138 }
139 else
140 {
141
142 /* Otherwise, not the only created thread, link-up the neighbors. */
143 next_thread = thread_ptr -> tx_thread_created_next;
144 previous_thread = thread_ptr -> tx_thread_created_previous;
145 next_thread -> tx_thread_created_previous = previous_thread;
146 previous_thread -> tx_thread_created_next = next_thread;
147
148 /* See if we have to update the created list head pointer. */
149 if (_tx_thread_created_ptr == thread_ptr)
150 {
151
152 /* Yes, move the head pointer to the next link. */
153 _tx_thread_created_ptr = next_thread;
154 }
155 }
156
157 /* Execute Port-Specific completion processing. If needed, it is typically defined in tx_port.h. */
158 TX_THREAD_DELETE_PORT_COMPLETION(thread_ptr)
159
160 /* Restore interrupts. */
161 TX_RESTORE
162 }
163
164 /* Return completion status. */
165 return(status);
166 }
167
168