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