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 #include "tx_timer.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_thread_terminate                                PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function handles application thread terminate requests.  Once  */
46 /*    a thread is terminated, it cannot be executed again unless it is    */
47 /*    deleted and recreated.                                              */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    thread_ptr                            Pointer to thread to suspend  */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Return completion status      */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _tx_timer_system_deactivate           Timer deactivate function     */
60 /*    _tx_thread_system_suspend             Actual thread suspension      */
61 /*    _tx_thread_system_ni_suspend          Non-interruptable suspend     */
62 /*                                            thread                      */
63 /*    _tx_thread_system_preempt_check       Check for preemption          */
64 /*    Suspend Cleanup Routine               Suspension cleanup function   */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_tx_thread_terminate(TX_THREAD * thread_ptr)79 UINT  _tx_thread_terminate(TX_THREAD *thread_ptr)
80 {
81 
82 TX_INTERRUPT_SAVE_AREA
83 
84 VOID        (*suspend_cleanup)(struct TX_THREAD_STRUCT *suspend_thread_ptr, ULONG suspension_sequence);
85 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
86 VOID        (*entry_exit_notify)(TX_THREAD *notify_thread_ptr, UINT id);
87 #endif
88 UINT        status;
89 ULONG       suspension_sequence;
90 
91 
92     /* Default to successful completion.  */
93     status =  TX_SUCCESS;
94 
95     /* Lockout interrupts while the thread is being terminated.  */
96     TX_DISABLE
97 
98     /* Deactivate thread timer, if active.  */
99     _tx_timer_system_deactivate(&thread_ptr -> tx_thread_timer);
100 
101     /* If trace is enabled, insert this event into the trace buffer.  */
102     TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_TERMINATE, thread_ptr, thread_ptr -> tx_thread_state, TX_POINTER_TO_ULONG_CONVERT(&suspend_cleanup), 0, TX_TRACE_THREAD_EVENTS)
103 
104     /* Log this kernel call.  */
105     TX_EL_THREAD_TERMINATE_INSERT
106 
107     /* Is the thread already terminated?  */
108     if (thread_ptr -> tx_thread_state == TX_TERMINATED)
109     {
110 
111         /* Restore interrupts.  */
112         TX_RESTORE
113 
114         /* Return success since thread is already terminated.  */
115         status =  TX_SUCCESS;
116     }
117 
118     /* Check the specified thread's current status.  */
119     else if (thread_ptr -> tx_thread_state != TX_COMPLETED)
120     {
121 
122         /* Disable preemption.  */
123         _tx_thread_preempt_disable++;
124 
125 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
126 
127         /* Pickup the entry/exit application callback routine.  */
128         entry_exit_notify =  thread_ptr -> tx_thread_entry_exit_notify;
129 #endif
130 
131         /* Check to see if the thread is currently ready.  */
132         if (thread_ptr -> tx_thread_state == TX_READY)
133         {
134 
135             /* Set the state to terminated.  */
136             thread_ptr -> tx_thread_state =  TX_TERMINATED;
137 
138             /* Thread state change.  */
139             TX_THREAD_STATE_CHANGE(thread_ptr, TX_TERMINATED)
140 
141 #ifdef TX_NOT_INTERRUPTABLE
142 
143 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
144 
145             /* Determine if an application callback routine is specified.  */
146             if (entry_exit_notify != TX_NULL)
147             {
148 
149                 /* Yes, notify application that this thread has exited!  */
150                 (entry_exit_notify)(thread_ptr, TX_THREAD_EXIT);
151             }
152 #endif
153 
154             /* Call actual non-interruptable thread suspension routine.  */
155             _tx_thread_system_ni_suspend(thread_ptr, ((ULONG) 0));
156 #else
157 
158             /* Set the suspending flag.  */
159             thread_ptr -> tx_thread_suspending =  TX_TRUE;
160 
161             /* Setup for no timeout period.  */
162             thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks =  ((ULONG) 0);
163 
164             /* Disable preemption.  */
165             _tx_thread_preempt_disable++;
166 
167             /* Since the thread is currently ready, we don't need to
168                worry about calling the suspend cleanup routine!  */
169 
170             /* Restore interrupts.  */
171             TX_RESTORE
172 
173             /* Perform any additional activities for tool or user purpose.  */
174             TX_THREAD_TERMINATED_EXTENSION(thread_ptr)
175 
176 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
177 
178             /* Determine if an application callback routine is specified.  */
179             if (entry_exit_notify != TX_NULL)
180             {
181 
182                 /* Yes, notify application that this thread has exited!  */
183                 (entry_exit_notify)(thread_ptr, TX_THREAD_EXIT);
184             }
185 #endif
186 
187             /* Call actual thread suspension routine.  */
188             _tx_thread_system_suspend(thread_ptr);
189 
190             /* Disable interrupts.  */
191             TX_DISABLE
192 #endif
193         }
194         else
195         {
196 
197             /* Change the state to terminated.  */
198             thread_ptr -> tx_thread_state =    TX_TERMINATED;
199 
200             /* Thread state change.  */
201             TX_THREAD_STATE_CHANGE(thread_ptr, TX_TERMINATED)
202 
203             /* Set the suspending flag.  This prevents the thread from being
204                resumed before the cleanup routine is executed.  */
205             thread_ptr -> tx_thread_suspending =  TX_TRUE;
206 
207             /* Pickup the cleanup routine address.  */
208             suspend_cleanup =  thread_ptr -> tx_thread_suspend_cleanup;
209 
210 #ifndef TX_NOT_INTERRUPTABLE
211 
212             /* Pickup the suspension sequence number that is used later to verify that the
213                cleanup is still necessary.  */
214             suspension_sequence =  thread_ptr -> tx_thread_suspension_sequence;
215 #else
216 
217             /* When not interruptable is selected, the suspension sequence is not used - just set to 0.  */
218             suspension_sequence =  ((ULONG) 0);
219 #endif
220 
221 #ifndef TX_NOT_INTERRUPTABLE
222 
223             /* Restore interrupts.  */
224             TX_RESTORE
225 #endif
226 
227             /* Call any cleanup routines.  */
228             if (suspend_cleanup != TX_NULL)
229             {
230 
231                 /* Yes, there is a function to call.  */
232                 (suspend_cleanup)(thread_ptr, suspension_sequence);
233             }
234 
235 #ifndef TX_NOT_INTERRUPTABLE
236 
237             /* Disable interrupts.  */
238             TX_DISABLE
239 #endif
240 
241             /* Clear the suspending flag.  */
242             thread_ptr -> tx_thread_suspending =  TX_FALSE;
243 
244 #ifndef TX_NOT_INTERRUPTABLE
245 
246             /* Restore interrupts.  */
247             TX_RESTORE
248 #endif
249 
250             /* Perform any additional activities for tool or user purpose.  */
251             TX_THREAD_TERMINATED_EXTENSION(thread_ptr)
252 
253 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
254 
255             /* Determine if an application callback routine is specified.  */
256             if (entry_exit_notify != TX_NULL)
257             {
258 
259                 /* Yes, notify application that this thread has exited!  */
260                 (entry_exit_notify)(thread_ptr, TX_THREAD_EXIT);
261             }
262 #endif
263 
264 #ifndef TX_NOT_INTERRUPTABLE
265 
266             /* Disable interrupts.  */
267             TX_DISABLE
268 #endif
269         }
270 
271 #ifndef TX_NOT_INTERRUPTABLE
272 
273         /* Restore interrupts.  */
274         TX_RESTORE
275 #endif
276 
277         /* Determine if the application is using mutexes.  */
278         if (_tx_thread_mutex_release != TX_NULL)
279         {
280 
281             /* Yes, call the mutex release function via a function pointer that
282                is setup during initialization.  */
283             (_tx_thread_mutex_release)(thread_ptr);
284         }
285 
286 #ifndef TX_NOT_INTERRUPTABLE
287 
288         /* Disable interrupts.  */
289         TX_DISABLE
290 #endif
291 
292         /* Enable preemption.  */
293         _tx_thread_preempt_disable--;
294 
295         /* Restore interrupts.  */
296         TX_RESTORE
297     }
298     else
299     {
300 
301         /* Restore interrupts.  */
302         TX_RESTORE
303     }
304 
305     /* Check for preemption.  */
306     _tx_thread_system_preempt_check();
307 
308     /* Return completion status.  */
309     return(status);
310 }
311 
312