Debian Codegraph @

src/core/lib/gprpp/fork.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/gprpp/fork.h"
22
23 #include <grpc/impl/codegen/gpr_types.h>
24 #include <grpc/support/atm.h>
25 #include <grpc/support/sync.h>
26 #include <grpc/support/time.h>
27
28 #include "src/core/lib/gprpp/global_config_env.h"
29 #include "src/core/lib/gprpp/no_destruct.h"
30
31 /*
32 * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
33 * AROUND VERY SPECIFIC USE CASES.
34 */
35
36 #ifdef GRPC_ENABLE_FORK_SUPPORT
37 #define GRPC_ENABLE_FORK_SUPPORT_DEFAULT true
38 #else
39 #define GRPC_ENABLE_FORK_SUPPORT_DEFAULT false
40 #endif // GRPC_ENABLE_FORK_SUPPORT
41
42 GPR_GLOBAL_CONFIG_DEFINE_BOOL(grpc_enable_fork_support,
43 GRPC_ENABLE_FORK_SUPPORT_DEFAULT,
44 "Enable fork support");
45
46 namespace grpc_core {
47 namespace {
48 // The exec_ctx_count has 2 modes, blocked and unblocked.
49 // When unblocked, the count is 2-indexed; exec_ctx_count=2 indicates
50 // 0 active ExecCtxs, exex_ctx_count=3 indicates 1 active ExecCtxs...
51
52 // When blocked, the exec_ctx_count is 0-indexed. Note that ExecCtx
53 // creation can only be blocked if there is exactly 1 outstanding ExecCtx,
54 // meaning that BLOCKED and UNBLOCKED counts partition the integers
55 #define UNBLOCKED(n) ((n) + 2)
56 #define BLOCKED(n) (n)
57
58 class ExecCtxState {
59 public:
60 ExecCtxState() : fork_complete_(true) {
61 gpr_mu_init(&mu_);
62 gpr_cv_init(&cv_);
63 gpr_atm_no_barrier_store(&count_, UNBLOCKED(0));
64 }
65
66 void IncExecCtxCount() {
67 gpr_atm count = gpr_atm_no_barrier_load(&count_);
68 while (true) {
69 if (count <= BLOCKED(1)) {
70 // This only occurs if we are trying to fork. Wait until the fork()
71 // operation completes before allowing new ExecCtxs.
72 gpr_mu_lock(&mu_);
73 if (gpr_atm_no_barrier_load(&count_) <= BLOCKED(1)) {
74 while (!fork_complete_) {
75 gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
76 }
77 }
78 gpr_mu_unlock(&mu_);
79 } else if (gpr_atm_no_barrier_cas(&count_, count, count + 1)) {
80 break;
81 }
82 count = gpr_atm_no_barrier_load(&count_);
83 }
84 }
85
86 void DecExecCtxCount() { gpr_atm_no_barrier_fetch_add(&count_, -1); }
87
88 bool BlockExecCtx() {
89 // Assumes there is an active ExecCtx when this function is called
90 if (gpr_atm_no_barrier_cas(&count_, UNBLOCKED(1), BLOCKED(1))) {
91 gpr_mu_lock(&mu_);
92 fork_complete_ = false;
93 gpr_mu_unlock(&mu_);
94 return true;
95 }
96 return false;
97 }
98
99 void AllowExecCtx() {
100 gpr_mu_lock(&mu_);
101 gpr_atm_no_barrier_store(&count_, UNBLOCKED(0));
102 fork_complete_ = true;
103 gpr_cv_broadcast(&cv_);
104 gpr_mu_unlock(&mu_);
105 }
106
107 ~ExecCtxState() {
108 gpr_mu_destroy(&mu_);
109 gpr_cv_destroy(&cv_);
110 }
111
112 private:
113 bool fork_complete_;
114 gpr_mu mu_;
115 gpr_cv cv_;
116 gpr_atm count_;
117 };
118
119 class ThreadState {
120 public:
121 ThreadState() : awaiting_threads_(false), threads_done_(false), count_(0) {
122 gpr_mu_init(&mu_);
123 gpr_cv_init(&cv_);
124 }
125
126 void IncThreadCount() {
127 gpr_mu_lock(&mu_);
128 count_++;
129 gpr_mu_unlock(&mu_);
130 }
131
132 void DecThreadCount() {
133 gpr_mu_lock(&mu_);
134 count_--;
135 if (awaiting_threads_ && count_ == 0) {
136 threads_done_ = true;
137 gpr_cv_signal(&cv_);
138 }
139 gpr_mu_unlock(&mu_);
140 }
141 void AwaitThreads() {
142 gpr_mu_lock(&mu_);
143 awaiting_threads_ = true;
144 threads_done_ = (count_ == 0);
145 while (!threads_done_) {
146 gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
147 }
148 awaiting_threads_ = true;
149 gpr_mu_unlock(&mu_);
150 }
151
152 ~ThreadState() {
153 gpr_mu_destroy(&mu_);
154 gpr_cv_destroy(&cv_);
155 }
156
157 private:
158 bool awaiting_threads_;
159 bool threads_done_;
160 gpr_mu mu_;
161 gpr_cv cv_;
162 int count_;
163 };
164
165 } // namespace
166
167 void Fork::GlobalInit() {
168 if (!override_enabled_) {
169 support_enabled_.store(GPR_GLOBAL_CONFIG_GET(grpc_enable_fork_support),
170 std::memory_order_relaxed);
171 }
172 }
173
174 bool Fork::Enabled() {
175 return support_enabled_.load(std::memory_order_relaxed);
176 }
177
178 // Testing Only
179 void Fork::Enable(bool enable) {
180 override_enabled_ = true;
181 support_enabled_.store(enable, std::memory_order_relaxed);
182 }
183
184 void Fork::DoIncExecCtxCount() {
185 NoDestructSingleton<ExecCtxState>::Get()->IncExecCtxCount();
186 }
187
188 void Fork::DoDecExecCtxCount() {
189 NoDestructSingleton<ExecCtxState>::Get()->DecExecCtxCount();
190 }
191
192 void Fork::SetResetChildPollingEngineFunc(
193 Fork::child_postfork_func reset_child_polling_engine) {
194 reset_child_polling_engine_ = reset_child_polling_engine;
195 }
196 Fork::child_postfork_func Fork::GetResetChildPollingEngineFunc() {
197 return reset_child_polling_engine_;
198 }
199
200 bool Fork::BlockExecCtx() {
201 if (support_enabled_.load(std::memory_order_relaxed)) {
202 return NoDestructSingleton<ExecCtxState>::Get()->BlockExecCtx();
203 }
204 return false;
205 }
206
207 void Fork::AllowExecCtx() {
208 if (support_enabled_.load(std::memory_order_relaxed)) {
209 NoDestructSingleton<ExecCtxState>::Get()->AllowExecCtx();
210 }
211 }
212
213 void Fork::IncThreadCount() {
214 if (support_enabled_.load(std::memory_order_relaxed)) {
215 NoDestructSingleton<ThreadState>::Get()->IncThreadCount();
216 }
217 }
218
219 void Fork::DecThreadCount() {
220 if (support_enabled_.load(std::memory_order_relaxed)) {
221 NoDestructSingleton<ThreadState>::Get()->DecThreadCount();
222 }
223 }
224 void Fork::AwaitThreads() {
225 if (support_enabled_.load(std::memory_order_relaxed)) {
226 NoDestructSingleton<ThreadState>::Get()->AwaitThreads();
227 }
228 }
229
230 std::atomic<bool> Fork::support_enabled_(false);
231 bool Fork::override_enabled_ = false;
232 Fork::child_postfork_func Fork::reset_child_polling_engine_ = nullptr;
233 } // namespace grpc_core