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