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 #include "tx_timer.h"
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _tx_thread_sleep PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* William E. Lamie, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function handles application thread sleep requests. If the */
46 /* sleep request was called from a non-thread, an error is returned. */
47 /* */
48 /* INPUT */
49 /* */
50 /* timer_ticks Number of timer ticks to sleep*/
51 /* */
52 /* OUTPUT */
53 /* */
54 /* status Return completion status */
55 /* */
56 /* CALLS */
57 /* */
58 /* _tx_thread_system_suspend Actual thread suspension */
59 /* _tx_thread_system_ni_suspend Non-interruptable suspend thread */
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_sleep(ULONG timer_ticks)74 UINT _tx_thread_sleep(ULONG timer_ticks)
75 {
76
77 TX_INTERRUPT_SAVE_AREA
78
79 UINT status;
80 TX_THREAD *thread_ptr;
81
82
83 /* Lockout interrupts while the thread is being resumed. */
84 TX_DISABLE
85
86 /* Pickup thread pointer. */
87 TX_THREAD_GET_CURRENT(thread_ptr)
88
89 /* Determine if this is a legal request. */
90
91 /* Is there a current thread? */
92 if (thread_ptr == TX_NULL)
93 {
94
95 /* Restore interrupts. */
96 TX_RESTORE
97
98 /* Illegal caller of this service. */
99 status = TX_CALLER_ERROR;
100 }
101
102 /* Is the caller an ISR or Initialization? */
103 else if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
104 {
105
106 /* Restore interrupts. */
107 TX_RESTORE
108
109 /* Illegal caller of this service. */
110 status = TX_CALLER_ERROR;
111 }
112
113 #ifndef TX_TIMER_PROCESS_IN_ISR
114
115 /* Is the caller the system timer thread? */
116 else if (thread_ptr == &_tx_timer_thread)
117 {
118
119 /* Restore interrupts. */
120 TX_RESTORE
121
122 /* Illegal caller of this service. */
123 status = TX_CALLER_ERROR;
124 }
125 #endif
126
127 /* Determine if the requested number of ticks is zero. */
128 else if (timer_ticks == ((ULONG) 0))
129 {
130
131 /* Restore interrupts. */
132 TX_RESTORE
133
134 /* Just return with a successful status. */
135 status = TX_SUCCESS;
136 }
137 else
138 {
139
140 /* Determine if the preempt disable flag is non-zero. */
141 if (_tx_thread_preempt_disable != ((UINT) 0))
142 {
143
144 /* Restore interrupts. */
145 TX_RESTORE
146
147 /* Suspension is not allowed if the preempt disable flag is non-zero at this point - return error completion. */
148 status = TX_CALLER_ERROR;
149 }
150 else
151 {
152
153 /* If trace is enabled, insert this event into the trace buffer. */
154 TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_SLEEP, TX_ULONG_TO_POINTER_CONVERT(timer_ticks), thread_ptr -> tx_thread_state, TX_POINTER_TO_ULONG_CONVERT(&status), 0, TX_TRACE_THREAD_EVENTS)
155
156 /* Log this kernel call. */
157 TX_EL_THREAD_SLEEP_INSERT
158
159 /* Suspend the current thread. */
160
161 /* Set the state to suspended. */
162 thread_ptr -> tx_thread_state = TX_SLEEP;
163
164 #ifdef TX_NOT_INTERRUPTABLE
165
166 /* Call actual non-interruptable thread suspension routine. */
167 _tx_thread_system_ni_suspend(thread_ptr, timer_ticks);
168
169 /* Restore interrupts. */
170 TX_RESTORE
171 #else
172
173 /* Set the suspending flag. */
174 thread_ptr -> tx_thread_suspending = TX_TRUE;
175
176 /* Initialize the status to successful. */
177 thread_ptr -> tx_thread_suspend_status = TX_SUCCESS;
178
179 /* Setup the timeout period. */
180 thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks = timer_ticks;
181
182 /* Temporarily disable preemption. */
183 _tx_thread_preempt_disable++;
184
185 /* Restore interrupts. */
186 TX_RESTORE
187
188 /* Call actual thread suspension routine. */
189 _tx_thread_system_suspend(thread_ptr);
190 #endif
191
192 /* Return status to the caller. */
193 status = thread_ptr -> tx_thread_suspend_status;
194 }
195 }
196
197 /* Return completion status. */
198 return(status);
199 }
200
201