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_deactivate PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* William E. Lamie, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function deactivates the specified application timer. */
45 /* */
46 /* INPUT */
47 /* */
48 /* timer_ptr Pointer to timer control block */
49 /* */
50 /* OUTPUT */
51 /* */
52 /* TX_SUCCESS Always returns success */
53 /* */
54 /* CALLS */
55 /* */
56 /* None */
57 /* */
58 /* CALLED BY */
59 /* */
60 /* Application Code */
61 /* */
62 /* RELEASE HISTORY */
63 /* */
64 /* DATE NAME DESCRIPTION */
65 /* */
66 /* 05-19-2020 William E. Lamie Initial Version 6.0 */
67 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
68 /* resulting in version 6.1 */
69 /* */
70 /**************************************************************************/
_tx_timer_deactivate(TX_TIMER * timer_ptr)71 UINT _tx_timer_deactivate(TX_TIMER *timer_ptr)
72 {
73 TX_INTERRUPT_SAVE_AREA
74
75 TX_TIMER_INTERNAL *internal_ptr;
76 TX_TIMER_INTERNAL **list_head;
77 TX_TIMER_INTERNAL *next_timer;
78 TX_TIMER_INTERNAL *previous_timer;
79 ULONG ticks_left;
80 UINT active_timer_list;
81
82
83 /* Setup internal timer pointer. */
84 internal_ptr = &(timer_ptr -> tx_timer_internal);
85
86 /* Disable interrupts while the remaining time before expiration is
87 calculated. */
88 TX_DISABLE
89
90 #ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO
91
92 /* Increment the total deactivations counter. */
93 _tx_timer_performance_deactivate_count++;
94
95 /* Increment the number of deactivations on this timer. */
96 timer_ptr -> tx_timer_performance_deactivate_count++;
97 #endif
98
99 /* If trace is enabled, insert this event into the trace buffer. */
100 TX_TRACE_IN_LINE_INSERT(TX_TRACE_TIMER_DEACTIVATE, timer_ptr, TX_POINTER_TO_ULONG_CONVERT(&ticks_left), 0, 0, TX_TRACE_TIMER_EVENTS)
101
102 /* Log this kernel call. */
103 TX_EL_TIMER_DEACTIVATE_INSERT
104
105 /* Pickup the list head. */
106 list_head = internal_ptr -> tx_timer_internal_list_head;
107
108 /* Is the timer active? */
109 if (list_head != TX_NULL)
110 {
111
112 /* Default the active timer list flag to false. */
113 active_timer_list = TX_FALSE;
114
115 /* Determine if the head pointer is within the timer expiration list. */
116 if (TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(list_head) >= TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(_tx_timer_list_start))
117 {
118
119 /* Now check to make sure the list head is before the end of the list. */
120 if (TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(list_head) < TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(_tx_timer_list_end))
121 {
122
123 /* Set the active timer list flag to true. */
124 active_timer_list = TX_TRUE;
125 }
126 }
127
128 /* Determine if the timer is on active timer list. */
129 if (active_timer_list == TX_TRUE)
130 {
131
132 /* This timer is active and has not yet expired. */
133
134 /* Calculate the amount of time that has elapsed since the timer
135 was activated. */
136
137 /* Is this timer's entry after the current timer pointer? */
138 if (TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(list_head) >= TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(_tx_timer_current_ptr))
139 {
140
141 /* Calculate ticks left to expiration - just the difference between this
142 timer's entry and the current timer pointer. */
143 ticks_left = (ULONG) (TX_TIMER_POINTER_DIF(list_head,_tx_timer_current_ptr)) + ((ULONG) 1);
144 }
145 else
146 {
147
148 /* Calculate the ticks left with a wrapped list condition. */
149 ticks_left = (ULONG) (TX_TIMER_POINTER_DIF(list_head,_tx_timer_list_start));
150
151 ticks_left = ticks_left + (ULONG) ((TX_TIMER_POINTER_DIF(_tx_timer_list_end, _tx_timer_current_ptr)) + ((ULONG) 1));
152 }
153
154 /* Adjust the remaining ticks accordingly. */
155 if (internal_ptr -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES)
156 {
157
158 /* Subtract off the last full pass through the timer list and add the
159 time left. */
160 internal_ptr -> tx_timer_internal_remaining_ticks =
161 (internal_ptr -> tx_timer_internal_remaining_ticks - TX_TIMER_ENTRIES) + ticks_left;
162 }
163 else
164 {
165
166 /* Just put the ticks left into the timer's remaining ticks. */
167 internal_ptr -> tx_timer_internal_remaining_ticks = ticks_left;
168 }
169 }
170 else
171 {
172
173 /* Determine if this is timer has just expired. */
174 if (_tx_timer_expired_timer_ptr != internal_ptr)
175 {
176
177 /* No, it hasn't expired. Now check for remaining time greater than the list
178 size. */
179 if (internal_ptr -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES)
180 {
181
182 /* Adjust the remaining ticks. */
183 internal_ptr -> tx_timer_internal_remaining_ticks =
184 internal_ptr -> tx_timer_internal_remaining_ticks - TX_TIMER_ENTRIES;
185 }
186 else
187 {
188
189 /* Set the remaining time to the reactivation time. */
190 internal_ptr -> tx_timer_internal_remaining_ticks = internal_ptr -> tx_timer_internal_re_initialize_ticks;
191 }
192 }
193 else
194 {
195
196 /* Set the remaining time to the reactivation time. */
197 internal_ptr -> tx_timer_internal_remaining_ticks = internal_ptr -> tx_timer_internal_re_initialize_ticks;
198 }
199 }
200
201 /* Pickup the next timer. */
202 next_timer = internal_ptr -> tx_timer_internal_active_next;
203
204 /* See if this is the only timer in the list. */
205 if (internal_ptr == next_timer)
206 {
207
208 /* Yes, the only timer on the list. */
209
210 /* Determine if the head pointer needs to be updated. */
211 if (*(list_head) == internal_ptr)
212 {
213
214 /* Update the head pointer. */
215 *(list_head) = TX_NULL;
216 }
217 }
218 else
219 {
220
221 /* At least one more timer is on the same expiration list. */
222
223 /* Update the links of the adjacent timers. */
224 previous_timer = internal_ptr -> tx_timer_internal_active_previous;
225 next_timer -> tx_timer_internal_active_previous = previous_timer;
226 previous_timer -> tx_timer_internal_active_next = next_timer;
227
228 /* Determine if the head pointer needs to be updated. */
229 if (*(list_head) == internal_ptr)
230 {
231
232 /* Update the next timer in the list with the list head
233 pointer. */
234 next_timer -> tx_timer_internal_list_head = list_head;
235
236 /* Update the head pointer. */
237 *(list_head) = next_timer;
238 }
239 }
240
241 /* Clear the timer's list head pointer. */
242 internal_ptr -> tx_timer_internal_list_head = TX_NULL;
243 }
244
245 /* Restore interrupts to previous posture. */
246 TX_RESTORE
247
248 /* Return TX_SUCCESS. */
249 return(TX_SUCCESS);
250 }
251
252