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/env.h"
30 #include "src/core/lib/gpr/useful.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.
40 // The exec_ctx_count has 2 modes, blocked and unblocked.
41 // When unblocked, the count is 2-indexed; exec_ctx_count=2 indicates
42 // 0 active ExecCtxs, exex_ctx_count=3 indicates 1 active ExecCtxs...
44 // When blocked, the exec_ctx_count is 0-indexed. Note that ExecCtx
45 // creation can only be blocked if there is exactly 1 outstanding ExecCtx,
46 // meaning that BLOCKED and UNBLOCKED counts partition the integers
47 #define UNBLOCKED(n) (n + 2)
48 #define BLOCKED(n) (n)
52 ExecCtxState() : fork_complete_(true) {
55 gpr_atm_no_barrier_store(&count_, UNBLOCKED(0));
58 void IncExecCtxCount() {
59 gpr_atm count = gpr_atm_no_barrier_load(&count_);
61 if (count <= BLOCKED(1)) {
62 // This only occurs if we are trying to fork. Wait until the fork()
63 // operation completes before allowing new ExecCtxs.
65 if (gpr_atm_no_barrier_load(&count_) <= BLOCKED(1)) {
66 while (!fork_complete_) {
67 gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
71 } else if (gpr_atm_no_barrier_cas(&count_, count, count + 1)) {
74 count = gpr_atm_no_barrier_load(&count_);
78 void DecExecCtxCount() { gpr_atm_no_barrier_fetch_add(&count_, -1); }
81 // Assumes there is an active ExecCtx when this function is called
82 if (gpr_atm_no_barrier_cas(&count_, UNBLOCKED(1), BLOCKED(1))) {
84 fork_complete_ = false;
93 gpr_atm_no_barrier_store(&count_, UNBLOCKED(0));
94 fork_complete_ = true;
95 gpr_cv_broadcast(&cv_);
100 gpr_mu_destroy(&mu_);
101 gpr_cv_destroy(&cv_);
113 ThreadState() : awaiting_threads_(false), threads_done_(false), count_(0) {
118 void IncThreadCount() {
124 void DecThreadCount() {
127 if (awaiting_threads_ && count_ == 0) {
128 threads_done_ = true;
133 void AwaitThreads() {
135 awaiting_threads_ = true;
136 threads_done_ = (count_ == 0);
137 while (!threads_done_) {
138 gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME));
140 awaiting_threads_ = true;
145 gpr_mu_destroy(&mu_);
146 gpr_cv_destroy(&cv_);
150 bool awaiting_threads_;
159 void Fork::GlobalInit() {
160 if (!override_enabled_) {
161 #ifdef GRPC_ENABLE_FORK_SUPPORT
162 support_enabled_ = true;
164 bool env_var_set = false;
165 char* env = gpr_getenv("GRPC_ENABLE_FORK_SUPPORT");
166 if (env != nullptr) {
167 static const char* truthy[] = {"yes", "Yes", "YES", "true",
168 "True", "TRUE", "1"};
169 static const char* falsey[] = {"no", "No", "NO", "false",
170 "False", "FALSE", "0"};
171 for (size_t i = 0; i < GPR_ARRAY_SIZE(truthy); i++) {
172 if (0 == strcmp(env, truthy[i])) {
173 support_enabled_ = true;
179 for (size_t i = 0; i < GPR_ARRAY_SIZE(falsey); i++) {
180 if (0 == strcmp(env, falsey[i])) {
181 support_enabled_ = false;
190 if (support_enabled_) {
191 exec_ctx_state_ = grpc_core::New<internal::ExecCtxState>();
192 thread_state_ = grpc_core::New<internal::ThreadState>();
196 void Fork::GlobalShutdown() {
197 if (support_enabled_) {
198 grpc_core::Delete(exec_ctx_state_);
199 grpc_core::Delete(thread_state_);
203 bool Fork::Enabled() { return support_enabled_; }
206 void Fork::Enable(bool enable) {
207 override_enabled_ = true;
208 support_enabled_ = enable;
211 void Fork::IncExecCtxCount() {
212 if (support_enabled_) {
213 exec_ctx_state_->IncExecCtxCount();
217 void Fork::DecExecCtxCount() {
218 if (support_enabled_) {
219 exec_ctx_state_->DecExecCtxCount();
223 void Fork::SetResetChildPollingEngineFunc(
224 Fork::child_postfork_func reset_child_polling_engine) {
225 reset_child_polling_engine_ = reset_child_polling_engine;
227 Fork::child_postfork_func Fork::GetResetChildPollingEngineFunc() {
228 return reset_child_polling_engine_;
231 bool Fork::BlockExecCtx() {
232 if (support_enabled_) {
233 return exec_ctx_state_->BlockExecCtx();
238 void Fork::AllowExecCtx() {
239 if (support_enabled_) {
240 exec_ctx_state_->AllowExecCtx();
244 void Fork::IncThreadCount() {
245 if (support_enabled_) {
246 thread_state_->IncThreadCount();
250 void Fork::DecThreadCount() {
251 if (support_enabled_) {
252 thread_state_->DecThreadCount();
255 void Fork::AwaitThreads() {
256 if (support_enabled_) {
257 thread_state_->AwaitThreads();
261 internal::ExecCtxState* Fork::exec_ctx_state_ = nullptr;
262 internal::ThreadState* Fork::thread_state_ = nullptr;
263 bool Fork::support_enabled_ = false;
264 bool Fork::override_enabled_ = false;
265 Fork::child_postfork_func Fork::reset_child_polling_engine_ = nullptr;
266 } // namespace grpc_core