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_thread.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _tx_thread_shell_entry                              PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    William E. Lamie, Microsoft Corporation                             */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function calls the specified entry function of the thread.  It */
44 /*    also provides a place for the thread's entry function to return.    */
45 /*    If the thread returns, this function places the thread in a         */
46 /*    "COMPLETED" state.                                                  */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    None                                                                */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    thread_entry                      Thread's entry function           */
59 /*    _tx_thread_system_suspend         Thread suspension routine         */
60 /*    _tx_thread_system_ni_suspend      Non-interruptable suspend thread  */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Initial thread stack frame                                          */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_tx_thread_shell_entry(VOID)75 VOID  _tx_thread_shell_entry(VOID)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 
80 TX_THREAD       *thread_ptr;
81 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
82 VOID            (*entry_exit_notify)(TX_THREAD *notify_thread_ptr, UINT type);
83 #endif
84 
85 
86     /* Pickup thread pointer.  */
87     TX_THREAD_GET_CURRENT(thread_ptr)
88 
89     /* Perform any additional activities for tool or user purpose.  */
90     TX_THREAD_STARTED_EXTENSION(thread_ptr)
91 
92 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
93 
94     /* Disable interrupts.  */
95     TX_DISABLE
96 
97     /* Pickup the entry/exit application callback routine.  */
98     entry_exit_notify =  thread_ptr -> tx_thread_entry_exit_notify;
99 
100     /* Restore interrupts.  */
101     TX_RESTORE
102 
103     /* Determine if an application callback routine is specified.  */
104     if (entry_exit_notify != TX_NULL)
105     {
106 
107         /* Yes, notify application that this thread has been entered!  */
108         (entry_exit_notify)(thread_ptr, TX_THREAD_ENTRY);
109     }
110 #endif
111 
112     /* Call current thread's entry function.  */
113     (thread_ptr -> tx_thread_entry) (thread_ptr -> tx_thread_entry_parameter);
114 
115     /* Suspend thread with a "completed" state.  */
116 
117     /* Determine if the application is using mutexes.  */
118     if (_tx_thread_mutex_release != TX_NULL)
119     {
120 
121         /* Yes, call the mutex release function via a function pointer that
122            is setup during mutex initialization.  */
123         (_tx_thread_mutex_release)(thread_ptr);
124     }
125 
126     /* Lockout interrupts while the thread state is setup.  */
127     TX_DISABLE
128 
129 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
130 
131     /* Pickup the entry/exit application callback routine again.  */
132     entry_exit_notify =  thread_ptr -> tx_thread_entry_exit_notify;
133 #endif
134 
135     /* Set the status to suspending, in order to indicate the suspension
136        is in progress.  */
137     thread_ptr -> tx_thread_state =  TX_COMPLETED;
138 
139     /* Thread state change.  */
140     TX_THREAD_STATE_CHANGE(thread_ptr, TX_COMPLETED)
141 
142 #ifdef TX_NOT_INTERRUPTABLE
143 
144 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
145 
146     /* Determine if an application callback routine is specified.  */
147     if (entry_exit_notify != TX_NULL)
148     {
149 
150         /* Yes, notify application that this thread has exited!  */
151         (entry_exit_notify)(thread_ptr, TX_THREAD_EXIT);
152     }
153 #endif
154 
155     /* Perform any additional activities for tool or user purpose.  */
156     TX_THREAD_COMPLETED_EXTENSION(thread_ptr)
157 
158     /* Call actual non-interruptable thread suspension routine.  */
159     _tx_thread_system_ni_suspend(thread_ptr, ((ULONG) 0));
160 
161     /* Restore interrupts.  */
162     TX_RESTORE
163 #else
164 
165     /* Set the suspending flag. */
166     thread_ptr -> tx_thread_suspending =  TX_TRUE;
167 
168     /* Setup for no timeout period.  */
169     thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks =  ((ULONG) 0);
170 
171     /* Temporarily disable preemption.  */
172     _tx_thread_preempt_disable++;
173 
174     /* Restore interrupts.  */
175     TX_RESTORE
176 
177     /* Perform any additional activities for tool or user purpose.  */
178     TX_THREAD_COMPLETED_EXTENSION(thread_ptr)
179 
180 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
181 
182     /* Determine if an application callback routine is specified.  */
183     if (entry_exit_notify != TX_NULL)
184     {
185 
186         /* Yes, notify application that this thread has exited!  */
187         (entry_exit_notify)(thread_ptr, TX_THREAD_EXIT);
188     }
189 #endif
190 
191     /* Call actual thread suspension routine.  */
192     _tx_thread_system_suspend(thread_ptr);
193 #endif
194 
195 
196 #ifdef TX_SAFETY_CRITICAL
197 
198     /* If we ever get here, raise safety critical exception.  */
199     TX_SAFETY_CRITICAL_EXCEPTION(__FILE__, __LINE__, 0);
200 #endif
201 }
202 
203