3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/gprpp/fork.h"
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/sync.h>
27 #include <grpc/support/time.h>
29 #include "src/core/lib/gpr/useful.h"
30 #include "src/core/lib/gprpp/global_config.h"
31 #include "src/core/lib/gprpp/memory.h"
34 * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
35 * AROUND VERY SPECIFIC USE CASES.
38 #ifdef GRPC_ENABLE_FORK_SUPPORT
39 #define GRPC_ENABLE_FORK_SUPPORT_DEFAULT true
41 #define GRPC_ENABLE_FORK_SUPPORT_DEFAULT false
42 #endif // GRPC_ENABLE_FORK_SUPPORT
44 GPR_GLOBAL_CONFIG_DEFINE_BOOL(grpc_enable_fork_support,
45 GRPC_ENABLE_FORK_SUPPORT_DEFAULT,
46 "Enable folk support");
50 // The exec_ctx_count has 2 modes, blocked and unblocked.
51 // When unblocked, the count is 2-indexed; exec_ctx_count=2 indicates
52 // 0 active ExecCtxs, exex_ctx_count=3 indicates 1 active ExecCtxs...
54 // When blocked, the exec_ctx_count is 0-indexed. Note that ExecCtx
55 // creation can only be blocked if there is exactly 1 outstanding ExecCtx,
56 // meaning that BLOCKED and UNBLOCKED counts partition the integers
57 #define UNBLOCKED(n) (n + 2)
58 #define BLOCKED(n) (n)
62 ExecCtxState() : fork_complete_(true) {
65 gpr_atm_no_barrier_store(&count_, UNBLOCKED(0));
68 void IncExecCtxCount() {
69 gpr_atm count = gpr_atm_no_barrier_load(&count_);
71 if (count <= BLOCKED(1)) {
72 // This only occurs if we are trying to fork. Wait until the fork()
73 // operation completes before allowing new ExecCtxs.
75 if (gpr_atm_no_barrier_load(&count_) <= BLOCKED(1)) {
76 while (!fork_complete_) {
77 gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
81 } else if (gpr_atm_no_barrier_cas(&count_, count, count + 1)) {
84 count = gpr_atm_no_barrier_load(&count_);
88 void DecExecCtxCount() { gpr_atm_no_barrier_fetch_add(&count_, -1); }
91 // Assumes there is an active ExecCtx when this function is called
92 if (gpr_atm_no_barrier_cas(&count_, UNBLOCKED(1), BLOCKED(1))) {
94 fork_complete_ = false;
101 void AllowExecCtx() {
103 gpr_atm_no_barrier_store(&count_, UNBLOCKED(0));
104 fork_complete_ = true;
105 gpr_cv_broadcast(&cv_);
110 gpr_mu_destroy(&mu_);
111 gpr_cv_destroy(&cv_);
123 ThreadState() : awaiting_threads_(false), threads_done_(false), count_(0) {
128 void IncThreadCount() {
134 void DecThreadCount() {
137 if (awaiting_threads_ && count_ == 0) {
138 threads_done_ = true;
143 void AwaitThreads() {
145 awaiting_threads_ = true;
146 threads_done_ = (count_ == 0);
147 while (!threads_done_) {
148 gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
150 awaiting_threads_ = true;
155 gpr_mu_destroy(&mu_);
156 gpr_cv_destroy(&cv_);
160 bool awaiting_threads_;
169 void Fork::GlobalInit() {
170 if (!override_enabled_) {
171 support_enabled_ = GPR_GLOBAL_CONFIG_GET(grpc_enable_fork_support);
173 if (support_enabled_) {
174 exec_ctx_state_ = grpc_core::New<internal::ExecCtxState>();
175 thread_state_ = grpc_core::New<internal::ThreadState>();
179 void Fork::GlobalShutdown() {
180 if (support_enabled_) {
181 grpc_core::Delete(exec_ctx_state_);
182 grpc_core::Delete(thread_state_);
186 bool Fork::Enabled() { return support_enabled_; }
189 void Fork::Enable(bool enable) {
190 override_enabled_ = true;
191 support_enabled_ = enable;
194 void Fork::IncExecCtxCount() {
195 if (support_enabled_) {
196 exec_ctx_state_->IncExecCtxCount();
200 void Fork::DecExecCtxCount() {
201 if (support_enabled_) {
202 exec_ctx_state_->DecExecCtxCount();
206 void Fork::SetResetChildPollingEngineFunc(
207 Fork::child_postfork_func reset_child_polling_engine) {
208 reset_child_polling_engine_ = reset_child_polling_engine;
210 Fork::child_postfork_func Fork::GetResetChildPollingEngineFunc() {
211 return reset_child_polling_engine_;
214 bool Fork::BlockExecCtx() {
215 if (support_enabled_) {
216 return exec_ctx_state_->BlockExecCtx();
221 void Fork::AllowExecCtx() {
222 if (support_enabled_) {
223 exec_ctx_state_->AllowExecCtx();
227 void Fork::IncThreadCount() {
228 if (support_enabled_) {
229 thread_state_->IncThreadCount();
233 void Fork::DecThreadCount() {
234 if (support_enabled_) {
235 thread_state_->DecThreadCount();
238 void Fork::AwaitThreads() {
239 if (support_enabled_) {
240 thread_state_->AwaitThreads();
244 internal::ExecCtxState* Fork::exec_ctx_state_ = nullptr;
245 internal::ThreadState* Fork::thread_state_ = nullptr;
246 bool Fork::support_enabled_ = false;
247 bool Fork::override_enabled_ = false;
248 Fork::child_postfork_func Fork::reset_child_polling_engine_ = nullptr;
249 } // namespace grpc_core