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_reset                                    PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function prepares the thread to run again from the entry       */
46 /*    point specified during thread creation. The application must        */
47 /*    call tx_thread_resume after this call completes for the thread      */
48 /*    to actually run.                                                    */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    thread_ptr                            Pointer to thread to reset    */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    status                                Service return status         */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _tx_thread_stack_build                Build initial thread stack    */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
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_reset(TX_THREAD * thread_ptr)75 UINT  _tx_thread_reset(TX_THREAD *thread_ptr)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 
80 TX_THREAD       *current_thread;
81 UINT            status;
82 
83 
84     /* Default a successful completion status.  */
85     status =  TX_SUCCESS;
86 
87     /* Disable interrupts.  */
88     TX_DISABLE
89 
90     /* Pickup thread pointer.  */
91     TX_THREAD_GET_CURRENT(current_thread)
92 
93     /* Check for a call from the current thread, which is not allowed!  */
94     if (current_thread == thread_ptr)
95     {
96 
97         /* Thread not completed or terminated - return an error!  */
98         status =  TX_NOT_DONE;
99     }
100     else
101     {
102 
103         /* Check for proper status of this thread to reset.  */
104         if (thread_ptr -> tx_thread_state != TX_COMPLETED)
105         {
106 
107             /* Now check for terminated state.  */
108             if (thread_ptr -> tx_thread_state != TX_TERMINATED)
109             {
110 
111                 /* Thread not completed or terminated - return an error!  */
112                 status =  TX_NOT_DONE;
113             }
114         }
115     }
116 
117     /* Is the request valid?  */
118     if (status == TX_SUCCESS)
119     {
120 
121         /* Modify the thread status to prevent additional reset calls.  */
122         thread_ptr -> tx_thread_state =  TX_NOT_DONE;
123 
124         /* Execute Port-Specific completion processing. If needed, it is typically defined in tx_port.h.  */
125         TX_THREAD_RESET_PORT_COMPLETION(thread_ptr)
126 
127         /* Restore interrupts.  */
128         TX_RESTORE
129 
130 #ifndef TX_DISABLE_STACK_FILLING
131 
132         /* Set the thread stack to a pattern prior to creating the initial
133            stack frame.  This pattern is used by the stack checking routines
134            to see how much has been used.  */
135         TX_MEMSET(thread_ptr -> tx_thread_stack_start, ((UCHAR) TX_STACK_FILL), thread_ptr -> tx_thread_stack_size);
136 #endif
137 
138         /* Call the target specific stack frame building routine to build the
139            thread's initial stack and to setup the actual stack pointer in the
140            control block.  */
141         _tx_thread_stack_build(thread_ptr, _tx_thread_shell_entry);
142 
143         /* Disable interrupts.  */
144         TX_DISABLE
145 
146         /* Finally, move into a suspended state to allow for the thread to be resumed.  */
147         thread_ptr -> tx_thread_state =  TX_SUSPENDED;
148 
149         /* If trace is enabled, insert this event into the trace buffer.  */
150         TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_RESET, thread_ptr, thread_ptr -> tx_thread_state, 0, 0, TX_TRACE_THREAD_EVENTS)
151 
152         /* Log this kernel call.  */
153         TX_EL_THREAD_RESET_INSERT
154 
155         /* Log the thread status change.  */
156         TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, TX_SUSPENDED)
157     }
158 
159     /* Restore interrupts.  */
160     TX_RESTORE
161 
162     /* Return completion status to caller.  */
163     return(status);
164 }
165 
166