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