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