Debian Codegraph @

src/core/lib/event_engine/posix_engine/timer_heap.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/event_engine/posix_engine/timer_heap.h"
22
23 #include <stdint.h>
24
25 #include <algorithm>
26
27 #include "src/core/lib/event_engine/posix_engine/timer.h"
28
29 namespace grpc_event_engine {
30 namespace posix_engine {
31
32 /* Adjusts a heap so as to move a hole at position i closer to the root,
33 until a suitable position is found for element t. Then, copies t into that
34 position. This functor is called each time immediately after modifying a
35 value in the underlying container, with the offset of the modified element as
36 its argument. */
37 void TimerHeap::AdjustUpwards(size_t i, Timer* t) {
38 while (i > 0) {
39 size_t parent = (i - 1) / 2;
40 if (timers_[parent]->deadline <= t->deadline) break;
41 timers_[i] = timers_[parent];
42 timers_[i]->heap_index = i;
43 i = parent;
44 }
45 timers_[i] = t;
46 t->heap_index = i;
47 }
48
49 /* Adjusts a heap so as to move a hole at position i farther away from the root,
50 until a suitable position is found for element t. Then, copies t into that
51 position. */
52 void TimerHeap::AdjustDownwards(size_t i, Timer* t) {
53 for (;;) {
54 size_t left_child = 1 + 2 * i;
55 if (left_child >= timers_.size()) break;
56 size_t right_child = left_child + 1;
57 size_t next_i =
58 right_child < timers_.size() &&
59 timers_[left_child]->deadline > timers_[right_child]->deadline
60 ? right_child
61 : left_child;
62 if (t->deadline <= timers_[next_i]->deadline) break;
63 timers_[i] = timers_[next_i];
64 timers_[i]->heap_index = i;
65 i = next_i;
66 }
67 timers_[i] = t;
68 t->heap_index = i;
69 }
70
71 void TimerHeap::NoteChangedPriority(Timer* timer) {
72 uint32_t i = timer->heap_index;
73 uint32_t parent = static_cast<uint32_t>((static_cast<int>(i) - 1) / 2);
74 if (timers_[parent]->deadline > timer->deadline) {
75 AdjustUpwards(i, timer);
76 } else {
77 AdjustDownwards(i, timer);
78 }
79 }
80
81 bool TimerHeap::Add(Timer* timer) {
82 timer->heap_index = timers_.size();
83 timers_.push_back(timer);
84 AdjustUpwards(timer->heap_index, timer);
85 return timer->heap_index == 0;
86 }
87
88 void TimerHeap::Remove(Timer* timer) {
89 uint32_t i = timer->heap_index;
90 if (i == timers_.size() - 1) {
91 timers_.pop_back();
92 return;
93 }
94 timers_[i] = timers_[timers_.size() - 1];
95 timers_[i]->heap_index = i;
96 timers_.pop_back();
97 NoteChangedPriority(timers_[i]);
98 }
99
100 bool TimerHeap::is_empty() { return timers_.empty(); }
101
102 Timer* TimerHeap::Top() { return timers_[0]; }
103
104 void TimerHeap::Pop() { Remove(Top()); }
105
106 } // namespace posix_engine
107 } // namespace grpc_event_engine