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 /**   Timer                                                               */
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_timer.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _tx_timer_info_get                                  PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    William E. Lamie, Microsoft Corporation                             */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function retrieves information from the specified timer.       */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    timer_ptr                         Pointer to timer control block    */
49 /*    name                              Destination for the timer name    */
50 /*    active                            Destination for active flag       */
51 /*    remaining_ticks                   Destination for remaining ticks   */
52 /*                                        before expiration               */
53 /*    reschedule_ticks                  Destination for reschedule ticks  */
54 /*    next_timer                        Destination for next timer on the */
55 /*                                        created list                    */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    status                            Completion status                 */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    None                                                                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application Code                                                    */
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_timer_info_get(TX_TIMER * timer_ptr,CHAR ** name,UINT * active,ULONG * remaining_ticks,ULONG * reschedule_ticks,TX_TIMER ** next_timer)78 UINT  _tx_timer_info_get(TX_TIMER *timer_ptr, CHAR **name, UINT *active, ULONG *remaining_ticks,
79                 ULONG *reschedule_ticks, TX_TIMER **next_timer)
80 {
81 
82 TX_INTERRUPT_SAVE_AREA
83 
84 TX_TIMER_INTERNAL   *internal_ptr;
85 TX_TIMER_INTERNAL   **list_head;
86 ULONG               ticks_left;
87 UINT                timer_active;
88 UINT                active_timer_list;
89 
90 
91     /* Disable interrupts.  */
92     TX_DISABLE
93 
94     /* If trace is enabled, insert this event into the trace buffer.  */
95     TX_TRACE_IN_LINE_INSERT(TX_TRACE_TIMER_INFO_GET, timer_ptr, TX_POINTER_TO_ULONG_CONVERT(&ticks_left), 0, 0, TX_TRACE_TIMER_EVENTS)
96 
97     /* Log this kernel call.  */
98     TX_EL_TIMER_INFO_GET_INSERT
99 
100     /* Retrieve the name of the timer.  */
101     if (name != TX_NULL)
102     {
103 
104         *name =  timer_ptr -> tx_timer_name;
105     }
106 
107     /* Pickup address of internal timer structure.  */
108     internal_ptr =  &(timer_ptr -> tx_timer_internal);
109 
110     /* Retrieve all the pertinent information and return it in the supplied
111        destinations.  */
112 
113     /* Default active to false.  */
114     timer_active =  TX_FALSE;
115 
116     /* Default the ticks left to the remaining ticks.  */
117     ticks_left =  internal_ptr -> tx_timer_internal_remaining_ticks;
118 
119     /* Determine if the timer is still active.  */
120     if (internal_ptr -> tx_timer_internal_list_head != TX_NULL)
121     {
122 
123         /* Indicate this timer is active.  */
124         timer_active =  TX_TRUE;
125 
126         /* Default the active timer list flag to false.  */
127         active_timer_list =  TX_FALSE;
128 
129         /* Determine if the timer is still active.  */
130         if (internal_ptr -> tx_timer_internal_list_head >= _tx_timer_list_start)
131         {
132 
133             /* Determine if the list head is before the end of the list.  */
134             if (internal_ptr -> tx_timer_internal_list_head < _tx_timer_list_end)
135             {
136 
137                 /* This timer is active and has not yet expired.  */
138                 active_timer_list =  TX_TRUE;
139             }
140         }
141 
142         /* Determine if the timer is on the active timer list.  */
143         if (active_timer_list == TX_TRUE)
144         {
145 
146             /* Calculate the amount of time that has elapsed since the timer
147                was activated.  */
148 
149             /* Setup the list head pointer.  */
150             list_head =  internal_ptr -> tx_timer_internal_list_head;
151 
152             /* Is this timer's entry after the current timer pointer?  */
153             if (internal_ptr -> tx_timer_internal_list_head >= _tx_timer_current_ptr)
154             {
155 
156                 /* Calculate ticks left to expiration - just the difference between this
157                    timer's entry and the current timer pointer.  */
158                 ticks_left =  ((TX_TIMER_POINTER_DIF(list_head, _tx_timer_current_ptr)) + ((ULONG) 1));
159             }
160             else
161             {
162 
163                 /* Calculate the ticks left with a wrapped list condition.  */
164                 ticks_left =  ((TX_TIMER_POINTER_DIF(list_head, _tx_timer_list_start)));
165 
166                 ticks_left =  ticks_left + ((TX_TIMER_POINTER_DIF(_tx_timer_list_end, _tx_timer_current_ptr)) + ((ULONG) 1));
167             }
168 
169             /* Adjust the remaining ticks accordingly.  */
170             if (internal_ptr -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES)
171             {
172 
173                 /* Subtract off the last full pass through the timer list and add the
174                    time left.  */
175                 ticks_left =  (internal_ptr -> tx_timer_internal_remaining_ticks - TX_TIMER_ENTRIES) + ticks_left;
176             }
177 
178         }
179         else
180         {
181 
182             /* The timer is not on the actual timer list so it must either be being processed
183                or on a temporary list to be processed.   */
184 
185             /* Check to see if this timer is the timer currently being processed.  */
186             if (_tx_timer_expired_timer_ptr == internal_ptr)
187             {
188 
189                 /* Timer dispatch routine is executing, waiting to execute, or just finishing. No more remaining ticks for this expiration.  */
190                 ticks_left =  ((ULONG) 0);
191             }
192             else
193             {
194 
195                 /* Timer is not the one being processed, which means it must be on the temporary expiration list
196                    waiting to be processed.  */
197 
198                 /* Calculate the remaining ticks for a timer in the process of expiring.  */
199                 if (ticks_left > TX_TIMER_ENTRIES)
200                 {
201 
202                     /* Calculate the number of ticks remaining.  */
203                     ticks_left =  internal_ptr -> tx_timer_internal_remaining_ticks - TX_TIMER_ENTRIES;
204                 }
205                 else
206                 {
207 
208                     /* Timer dispatch routine is waiting to execute, no more remaining ticks for this expiration.  */
209                     ticks_left =  ((ULONG) 0);
210                 }
211             }
212         }
213     }
214 
215     /* Setup return values for an inactive timer.  */
216     if (active != TX_NULL)
217     {
218 
219        /* Setup the timer active indication.  */
220        *active =  timer_active;
221     }
222     if (remaining_ticks != TX_NULL)
223     {
224 
225         /* Setup the default remaining ticks value.  */
226         *remaining_ticks =  ticks_left;
227     }
228 
229     /* Pickup the reschedule ticks value.  */
230     if (reschedule_ticks != TX_NULL)
231     {
232 
233         *reschedule_ticks =  internal_ptr -> tx_timer_internal_re_initialize_ticks;
234     }
235 
236     /* Pickup the next created application timer.  */
237     if (next_timer != TX_NULL)
238     {
239 
240         *next_timer =  timer_ptr -> tx_timer_created_next;
241     }
242 
243     /* Restore interrupts.  */
244     TX_RESTORE
245 
246     /* Return completion status.  */
247     return(TX_SUCCESS);
248 }
249 
250