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