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 #include "tx_timer.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _tx_thread_timeout                                  PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    William E. Lamie, Microsoft Corporation                             */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function handles thread timeout processing.  Timeouts occur in */
45 /*    two flavors, namely the thread sleep timeout and all other service  */
46 /*    call timeouts.  Thread sleep timeouts are processed locally, while  */
47 /*    the others are processed by the appropriate suspension clean-up     */
48 /*    service.                                                            */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    timeout_input                         Contains the thread pointer   */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    Suspension Cleanup Functions                                        */
61 /*    _tx_thread_system_resume          Resume thread                     */
62 /*    _tx_thread_system_ni_resume       Non-interruptable resume thread   */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _tx_timer_expiration_process          Timer expiration function     */
67 /*    _tx_timer_thread_entry                Timer thread function         */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_tx_thread_timeout(ULONG timeout_input)78 VOID  _tx_thread_timeout(ULONG timeout_input)
79 {
80 
81 TX_INTERRUPT_SAVE_AREA
82 
83 TX_THREAD       *thread_ptr;
84 VOID            (*suspend_cleanup)(struct TX_THREAD_STRUCT *suspend_thread_ptr, ULONG suspension_sequence);
85 ULONG           suspension_sequence;
86 
87 
88     /* Pickup the thread pointer.  */
89     TX_THREAD_TIMEOUT_POINTER_SETUP(thread_ptr)
90 
91     /* Disable interrupts.  */
92     TX_DISABLE
93 
94     /* Determine how the thread is currently suspended.  */
95     if (thread_ptr -> tx_thread_state == TX_SLEEP)
96     {
97 
98 #ifdef TX_NOT_INTERRUPTABLE
99 
100         /* Resume the thread!  */
101         _tx_thread_system_ni_resume(thread_ptr);
102 
103         /* Restore interrupts.  */
104         TX_RESTORE
105 #else
106 
107         /* Increment the disable preemption flag.  */
108         _tx_thread_preempt_disable++;
109 
110         /* Restore interrupts.  */
111         TX_RESTORE
112 
113         /* Lift the suspension on the sleeping thread.  */
114         _tx_thread_system_resume(thread_ptr);
115 #endif
116     }
117     else
118     {
119 
120         /* Process all other suspension timeouts.  */
121 
122 #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO
123 
124         /* Increment the total number of thread timeouts.  */
125         _tx_thread_performance_timeout_count++;
126 
127         /* Increment the number of timeouts for this thread.  */
128         thread_ptr -> tx_thread_performance_timeout_count++;
129 #endif
130 
131         /* Pickup the cleanup routine address.  */
132         suspend_cleanup =  thread_ptr -> tx_thread_suspend_cleanup;
133 
134 #ifndef TX_NOT_INTERRUPTABLE
135 
136         /* Pickup the suspension sequence number that is used later to verify that the
137            cleanup is still necessary.  */
138         suspension_sequence =  thread_ptr -> tx_thread_suspension_sequence;
139 #else
140 
141         /* When not interruptable is selected, the suspension sequence is not used - just set to 0.  */
142         suspension_sequence =  ((ULONG) 0);
143 #endif
144 
145 #ifndef TX_NOT_INTERRUPTABLE
146 
147         /* Restore interrupts.  */
148         TX_RESTORE
149 #endif
150 
151         /* Call any cleanup routines.  */
152         if (suspend_cleanup != TX_NULL)
153         {
154 
155             /* Yes, there is a function to call.  */
156             (suspend_cleanup)(thread_ptr, suspension_sequence);
157         }
158 
159 #ifdef TX_NOT_INTERRUPTABLE
160 
161         /* Restore interrupts.  */
162         TX_RESTORE
163 #endif
164     }
165 }
166 
167