@@ -154,25 +154,30 @@ std::chrono::nanoseconds TimersManager::get_head_timeout_unsafe()
154154 if (weak_timers_heap_.empty ()) {
155155 return MAX_TIME;
156156 }
157-
158- // Weak heap is not empty, so try to lock the first element
159- TimerPtr head_timer = weak_timers_heap_.front ().lock ();
157+ // Weak heap is not empty, so try to lock the first element.
160158 // If it is still a valid pointer, it is guaranteed to be the correct head
161- if (head_timer != nullptr ) {
162- return head_timer->time_until_trigger ();
163- }
159+ TimerPtr head_timer = weak_timers_heap_.front ().lock ();
164160
165- // If the first elements has expired, we can't make other assumptions on the heap
166- // and we need to entirely validate it.
167- TimersHeap locked_heap = weak_timers_heap_.validate_and_lock ();
161+ if (head_timer == nullptr ) {
162+ // The first element has expired, we can't make other assumptions on the heap
163+ // and we need to entirely validate it.
164+ TimersHeap locked_heap = weak_timers_heap_.validate_and_lock ();
165+ // NOTE: the following operations will not modify any element in the heap, so we
166+ // don't have to call `weak_timers_heap_.store(locked_heap)` at the end.
167+
168+ if (locked_heap.empty ()) {
169+ return MAX_TIME;
170+ }
171+ head_timer = locked_heap.front ();
172+ }
168173
169- // NOTE: the following operations will not modify any element in the heap, so we
170- // don't have to call `weak_timers_heap_.store(locked_heap)` at the end.
174+ auto time_until_trigger = head_timer->time_until_trigger ();
171175
172- if (locked_heap.empty ()) {
176+ // A canceled timer will return a nanoseconds::max duration
177+ if (time_until_trigger == std::chrono::nanoseconds::max ()) {
173178 return MAX_TIME;
174179 }
175- return locked_heap. front ()-> time_until_trigger () ;
180+ return time_until_trigger;
176181}
177182
178183void TimersManager::execute_ready_timers_unsafe ()
0 commit comments