grpc
src
core
lib
event_engine
iomgr_engine
event_engine/iomgr_engine/timer_heap.cc
Go to the documentation of this file.
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/iomgr_engine/timer_heap.h
"
22
23
#include <
stdint.h
>
24
25
#include <algorithm>
26
27
#include "
src/core/lib/event_engine/iomgr_engine/timer.h
"
28
29
namespace
grpc_event_engine
{
30
namespace
iomgr_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 iomgr_engine
107
}
// namespace grpc_event_engine
grpc_event_engine::iomgr_engine::TimerHeap::Top
Timer * Top()
Definition:
event_engine/iomgr_engine/timer_heap.cc:102
grpc_event_engine::iomgr_engine::TimerHeap::Add
bool Add(Timer *timer)
Definition:
event_engine/iomgr_engine/timer_heap.cc:81
grpc_event_engine::iomgr_engine::TimerHeap::Pop
void Pop()
Definition:
event_engine/iomgr_engine/timer_heap.cc:104
grpc_event_engine::iomgr_engine::TimerHeap::is_empty
bool is_empty()
Definition:
event_engine/iomgr_engine/timer_heap.cc:100
grpc_event_engine::iomgr_engine::TimerHeap::NoteChangedPriority
void NoteChangedPriority(Timer *timer)
Definition:
event_engine/iomgr_engine/timer_heap.cc:71
grpc_event_engine::iomgr_engine::TimerHeap::timers_
std::vector< Timer * > timers_
Definition:
event_engine/iomgr_engine/timer_heap.h:50
grpc_event_engine::iomgr_engine::TimerHeap::Remove
void Remove(Timer *timer)
Definition:
event_engine/iomgr_engine/timer_heap.cc:88
uint32_t
unsigned int uint32_t
Definition:
stdint-msvc2008.h:80
stdint.h
grpc_event_engine::iomgr_engine::Timer
Definition:
event_engine/iomgr_engine/timer.h:44
grpc_event_engine::iomgr_engine::TimerHeap::AdjustUpwards
void AdjustUpwards(size_t i, Timer *t)
Definition:
event_engine/iomgr_engine/timer_heap.cc:37
grpc_event_engine::iomgr_engine::TimerHeap::AdjustDownwards
void AdjustDownwards(size_t i, Timer *t)
Definition:
event_engine/iomgr_engine/timer_heap.cc:52
grpc_event_engine
Definition:
endpoint_config.h:24
timer_heap.h
timer.h
i
uint64_t i
Definition:
abseil-cpp/absl/container/btree_benchmark.cc:230
timer
static uv_timer_t timer
Definition:
test-callback-stack.c:34
port_platform.h
grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:38