Debian Codegraph @

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

raw
1 /*
2 *
3 * Copyright 2017 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_manager.h"
22
23 #include <memory>
24 #include <utility>
25
26 #include "absl/time/time.h"
27 #include "absl/types/optional.h"
28
29 #include <grpc/impl/codegen/gpr_types.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/time.h>
32
33 #include "src/core/lib/debug/trace.h"
34 #include "src/core/lib/gprpp/thd.h"
35
36 static thread_local bool g_timer_thread;
37
38 namespace grpc_event_engine {
39 namespace posix_engine {
40
41 grpc_core::DebugOnlyTraceFlag grpc_event_engine_timer_trace(false, "timer");
42
43 void TimerManager::RunSomeTimers(
44 std::vector<experimental::EventEngine::Closure*> timers) {
45 for (auto* timer : timers) {
46 thread_pool_->Run(timer);
47 }
48 }
49
50 // wait until 'next' (or forever if there is already a timed waiter in the pool)
51 // returns true if the thread should continue executing (false if it should
52 // shutdown)
53 bool TimerManager::WaitUntil(grpc_core::Timestamp next) {
54 grpc_core::MutexLock lock(&mu_);
55 if (shutdown_) return false;
56 // If kicked_ is true at this point, it means there was a kick from the timer
57 // system that the timer-manager threads here missed. We cannot trust 'next'
58 // here any longer (since there might be an earlier deadline). So if kicked_
59 // is true at this point, we should quickly exit this and get the next
60 // deadline from the timer system
61 if (!kicked_) {
62 cv_wait_.WaitWithTimeout(&mu_,
63 absl::Milliseconds((next - host_.Now()).millis()));
64 ++wakeups_;
65 }
66 kicked_ = false;
67 return true;
68 }
69
70 void TimerManager::MainLoop() {
71 for (;;) {
72 grpc_core::Timestamp next = grpc_core::Timestamp::InfFuture();
73 absl::optional<std::vector<experimental::EventEngine::Closure*>>
74 check_result = timer_list_->TimerCheck(&next);
75 GPR_ASSERT(check_result.has_value() &&
76 "ERROR: More than one MainLoop is running.");
77 if (!check_result->empty()) {
78 RunSomeTimers(std::move(*check_result));
79 continue;
80 }
81 if (!WaitUntil(next)) break;
82 }
83 main_loop_exit_signal_->Notify();
84 }
85
86 bool TimerManager::IsTimerManagerThread() { return g_timer_thread; }
87
88 void TimerManager::StartMainLoopThread() {
89 main_thread_ = grpc_core::Thread(
90 "timer_manager",
91 [](void* arg) {
92 auto self = static_cast<TimerManager*>(arg);
93 self->MainLoop();
94 },
95 this, nullptr,
96 grpc_core::Thread::Options().set_tracked(false).set_joinable(false));
97 main_thread_.Start();
98 }
99
100 TimerManager::TimerManager(
101 std::shared_ptr<grpc_event_engine::experimental::ThreadPool> thread_pool)
102 : host_(this), thread_pool_(std::move(thread_pool)) {
103 timer_list_ = std::make_unique<TimerList>(&host_);
104 main_loop_exit_signal_.emplace();
105 StartMainLoopThread();
106 }
107
108 grpc_core::Timestamp TimerManager::Host::Now() {
109 return grpc_core::Timestamp::FromTimespecRoundDown(
110 gpr_now(GPR_CLOCK_MONOTONIC));
111 }
112
113 void TimerManager::TimerInit(Timer* timer, grpc_core::Timestamp deadline,
114 experimental::EventEngine::Closure* closure) {
115 if (grpc_event_engine_timer_trace.enabled()) {
116 grpc_core::MutexLock lock(&mu_);
117 if (shutdown_) {
118 gpr_log(GPR_ERROR,
119 "WARNING: TimerManager::%p: scheduling Closure::%p after "
120 "TimerManager has been shut down.",
121 this, closure);
122 }
123 }
124 timer_list_->TimerInit(timer, deadline, closure);
125 }
126
127 bool TimerManager::TimerCancel(Timer* timer) {
128 return timer_list_->TimerCancel(timer);
129 }
130
131 void TimerManager::Shutdown() {
132 {
133 grpc_core::MutexLock lock(&mu_);
134 if (shutdown_) return;
135 if (grpc_event_engine_timer_trace.enabled()) {
136 gpr_log(GPR_DEBUG, "TimerManager::%p shutting down", this);
137 }
138 shutdown_ = true;
139 // Wait on the main loop to exit.
140 cv_wait_.Signal();
141 }
142 main_loop_exit_signal_->WaitForNotification();
143 if (grpc_event_engine_timer_trace.enabled()) {
144 gpr_log(GPR_DEBUG, "TimerManager::%p shutdown complete", this);
145 }
146 }
147
148 TimerManager::~TimerManager() { Shutdown(); }
149
150 void TimerManager::Host::Kick() { timer_manager_->Kick(); }
151
152 void TimerManager::Kick() {
153 grpc_core::MutexLock lock(&mu_);
154 kicked_ = true;
155 cv_wait_.Signal();
156 }
157
158 void TimerManager::RestartPostFork() {
159 grpc_core::MutexLock lock(&mu_);
160 GPR_ASSERT(GPR_LIKELY(shutdown_));
161 if (grpc_event_engine_timer_trace.enabled()) {
162 gpr_log(GPR_DEBUG, "TimerManager::%p restarting after shutdown", this);
163 }
164 shutdown_ = false;
165 main_loop_exit_signal_.emplace();
166 StartMainLoopThread();
167 }
168
169 void TimerManager::PrepareFork() { Shutdown(); }
170 void TimerManager::PostforkParent() { RestartPostFork(); }
171 void TimerManager::PostforkChild() { RestartPostFork(); }
172
173 } // namespace posix_engine
174 } // namespace grpc_event_engine