diff --git a/BUILD b/BUILD index 9c99f95fcdf..45c8f406927 100644 --- a/BUILD +++ b/BUILD @@ -511,7 +511,6 @@ grpc_cc_library( "src/core/lib/gpr/env_linux.cc", "src/core/lib/gpr/env_posix.cc", "src/core/lib/gpr/env_windows.cc", - "src/core/lib/gpr/fork.cc", "src/core/lib/gpr/host_port.cc", "src/core/lib/gpr/log.cc", "src/core/lib/gpr/log_android.cc", @@ -536,6 +535,7 @@ grpc_cc_library( "src/core/lib/gpr/tmpfile_posix.cc", "src/core/lib/gpr/tmpfile_windows.cc", "src/core/lib/gpr/wrap_memcpy.cc", + "src/core/lib/gprpp/fork.cc", "src/core/lib/gprpp/thd_posix.cc", "src/core/lib/gprpp/thd_windows.cc", "src/core/lib/profiling/basic_timers.cc", @@ -544,7 +544,6 @@ grpc_cc_library( hdrs = [ "src/core/lib/gpr/arena.h", "src/core/lib/gpr/env.h", - "src/core/lib/gpr/fork.h", "src/core/lib/gpr/host_port.h", "src/core/lib/gpr/mpscq.h", "src/core/lib/gpr/murmur_hash.h", @@ -559,6 +558,7 @@ grpc_cc_library( "src/core/lib/gpr/tmpfile.h", "src/core/lib/gpr/useful.h", "src/core/lib/gprpp/abstract.h", + "src/core/lib/gprpp/fork.h", "src/core/lib/gprpp/manual_constructor.h", "src/core/lib/gprpp/memory.h", "src/core/lib/gprpp/thd.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index aa4dbb4f9b3..b58065b1119 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -703,7 +703,6 @@ add_library(gpr src/core/lib/gpr/env_linux.cc src/core/lib/gpr/env_posix.cc src/core/lib/gpr/env_windows.cc - src/core/lib/gpr/fork.cc src/core/lib/gpr/host_port.cc src/core/lib/gpr/log.cc src/core/lib/gpr/log_android.cc @@ -728,6 +727,7 @@ add_library(gpr src/core/lib/gpr/tmpfile_posix.cc src/core/lib/gpr/tmpfile_windows.cc src/core/lib/gpr/wrap_memcpy.cc + src/core/lib/gprpp/fork.cc src/core/lib/gprpp/thd_posix.cc src/core/lib/gprpp/thd_windows.cc src/core/lib/profiling/basic_timers.cc @@ -6161,7 +6161,7 @@ if (gRPC_BUILD_TESTS) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC) add_executable(fork_test - test/core/gpr/fork_test.cc + test/core/gprpp/fork_test.cc ) diff --git a/Makefile b/Makefile index 631ecf9f96e..83941022698 100644 --- a/Makefile +++ b/Makefile @@ -3035,7 +3035,6 @@ LIBGPR_SRC = \ src/core/lib/gpr/env_linux.cc \ src/core/lib/gpr/env_posix.cc \ src/core/lib/gpr/env_windows.cc \ - src/core/lib/gpr/fork.cc \ src/core/lib/gpr/host_port.cc \ src/core/lib/gpr/log.cc \ src/core/lib/gpr/log_android.cc \ @@ -3060,6 +3059,7 @@ LIBGPR_SRC = \ src/core/lib/gpr/tmpfile_posix.cc \ src/core/lib/gpr/tmpfile_windows.cc \ src/core/lib/gpr/wrap_memcpy.cc \ + src/core/lib/gprpp/fork.cc \ src/core/lib/gprpp/thd_posix.cc \ src/core/lib/gprpp/thd_windows.cc \ src/core/lib/profiling/basic_timers.cc \ @@ -10959,7 +10959,7 @@ endif FORK_TEST_SRC = \ - test/core/gpr/fork_test.cc \ + test/core/gprpp/fork_test.cc \ FORK_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(FORK_TEST_SRC)))) ifeq ($(NO_SECURE),true) @@ -10979,7 +10979,7 @@ $(BINDIR)/$(CONFIG)/fork_test: $(FORK_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgpr_test endif -$(OBJDIR)/$(CONFIG)/test/core/gpr/fork_test.o: $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a +$(OBJDIR)/$(CONFIG)/test/core/gprpp/fork_test.o: $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a deps_fork_test: $(FORK_TEST_OBJS:.o=.dep) diff --git a/build.yaml b/build.yaml index 0d8846bcd1c..890ebb06c48 100644 --- a/build.yaml +++ b/build.yaml @@ -121,7 +121,6 @@ filegroups: - src/core/lib/gpr/env_linux.cc - src/core/lib/gpr/env_posix.cc - src/core/lib/gpr/env_windows.cc - - src/core/lib/gpr/fork.cc - src/core/lib/gpr/host_port.cc - src/core/lib/gpr/log.cc - src/core/lib/gpr/log_android.cc @@ -146,6 +145,7 @@ filegroups: - src/core/lib/gpr/tmpfile_posix.cc - src/core/lib/gpr/tmpfile_windows.cc - src/core/lib/gpr/wrap_memcpy.cc + - src/core/lib/gprpp/fork.cc - src/core/lib/gprpp/thd_posix.cc - src/core/lib/gprpp/thd_windows.cc - src/core/lib/profiling/basic_timers.cc @@ -174,7 +174,6 @@ filegroups: headers: - src/core/lib/gpr/arena.h - src/core/lib/gpr/env.h - - src/core/lib/gpr/fork.h - src/core/lib/gpr/host_port.h - src/core/lib/gpr/mpscq.h - src/core/lib/gpr/murmur_hash.h @@ -192,6 +191,7 @@ filegroups: - src/core/lib/gprpp/atomic.h - src/core/lib/gprpp/atomic_with_atm.h - src/core/lib/gprpp/atomic_with_std.h + - src/core/lib/gprpp/fork.h - src/core/lib/gprpp/manual_constructor.h - src/core/lib/gprpp/memory.h - src/core/lib/gprpp/thd.h @@ -2320,7 +2320,7 @@ targets: build: test language: c src: - - test/core/gpr/fork_test.cc + - test/core/gprpp/fork_test.cc deps: - gpr_test_util - gpr diff --git a/config.m4 b/config.m4 index 57fc2dbab92..17d2f9c1a65 100644 --- a/config.m4 +++ b/config.m4 @@ -49,7 +49,6 @@ if test "$PHP_GRPC" != "no"; then src/core/lib/gpr/env_linux.cc \ src/core/lib/gpr/env_posix.cc \ src/core/lib/gpr/env_windows.cc \ - src/core/lib/gpr/fork.cc \ src/core/lib/gpr/host_port.cc \ src/core/lib/gpr/log.cc \ src/core/lib/gpr/log_android.cc \ @@ -74,6 +73,7 @@ if test "$PHP_GRPC" != "no"; then src/core/lib/gpr/tmpfile_posix.cc \ src/core/lib/gpr/tmpfile_windows.cc \ src/core/lib/gpr/wrap_memcpy.cc \ + src/core/lib/gprpp/fork.cc \ src/core/lib/gprpp/thd_posix.cc \ src/core/lib/gprpp/thd_windows.cc \ src/core/lib/profiling/basic_timers.cc \ diff --git a/config.w32 b/config.w32 index 580607dd3de..7c5a76acef9 100644 --- a/config.w32 +++ b/config.w32 @@ -26,7 +26,6 @@ if (PHP_GRPC != "no") { "src\\core\\lib\\gpr\\env_linux.cc " + "src\\core\\lib\\gpr\\env_posix.cc " + "src\\core\\lib\\gpr\\env_windows.cc " + - "src\\core\\lib\\gpr\\fork.cc " + "src\\core\\lib\\gpr\\host_port.cc " + "src\\core\\lib\\gpr\\log.cc " + "src\\core\\lib\\gpr\\log_android.cc " + @@ -51,6 +50,7 @@ if (PHP_GRPC != "no") { "src\\core\\lib\\gpr\\tmpfile_posix.cc " + "src\\core\\lib\\gpr\\tmpfile_windows.cc " + "src\\core\\lib\\gpr\\wrap_memcpy.cc " + + "src\\core\\lib\\gprpp\\fork.cc " + "src\\core\\lib\\gprpp\\thd_posix.cc " + "src\\core\\lib\\gprpp\\thd_windows.cc " + "src\\core\\lib\\profiling\\basic_timers.cc " + diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index 65a5dc66b4c..22428cc03db 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -213,7 +213,6 @@ Pod::Spec.new do |s| 'src/cpp/codegen/codegen_init.cc', 'src/core/lib/gpr/arena.h', 'src/core/lib/gpr/env.h', - 'src/core/lib/gpr/fork.h', 'src/core/lib/gpr/host_port.h', 'src/core/lib/gpr/mpscq.h', 'src/core/lib/gpr/murmur_hash.h', @@ -231,6 +230,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/atomic.h', 'src/core/lib/gprpp/atomic_with_atm.h', 'src/core/lib/gprpp/atomic_with_std.h', + 'src/core/lib/gprpp/fork.h', 'src/core/lib/gprpp/manual_constructor.h', 'src/core/lib/gprpp/memory.h', 'src/core/lib/gprpp/thd.h', @@ -495,7 +495,6 @@ Pod::Spec.new do |s| 'src/cpp/thread_manager/thread_manager.h', 'src/core/lib/gpr/arena.h', 'src/core/lib/gpr/env.h', - 'src/core/lib/gpr/fork.h', 'src/core/lib/gpr/host_port.h', 'src/core/lib/gpr/mpscq.h', 'src/core/lib/gpr/murmur_hash.h', @@ -513,6 +512,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/atomic.h', 'src/core/lib/gprpp/atomic_with_atm.h', 'src/core/lib/gprpp/atomic_with_std.h', + 'src/core/lib/gprpp/fork.h', 'src/core/lib/gprpp/manual_constructor.h', 'src/core/lib/gprpp/memory.h', 'src/core/lib/gprpp/thd.h', diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec index 7f61719cb16..631dfed013a 100644 --- a/gRPC-Core.podspec +++ b/gRPC-Core.podspec @@ -185,7 +185,6 @@ Pod::Spec.new do |s| # To save you from scrolling, this is the last part of the podspec. ss.source_files = 'src/core/lib/gpr/arena.h', 'src/core/lib/gpr/env.h', - 'src/core/lib/gpr/fork.h', 'src/core/lib/gpr/host_port.h', 'src/core/lib/gpr/mpscq.h', 'src/core/lib/gpr/murmur_hash.h', @@ -203,6 +202,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/atomic.h', 'src/core/lib/gprpp/atomic_with_atm.h', 'src/core/lib/gprpp/atomic_with_std.h', + 'src/core/lib/gprpp/fork.h', 'src/core/lib/gprpp/manual_constructor.h', 'src/core/lib/gprpp/memory.h', 'src/core/lib/gprpp/thd.h', @@ -217,7 +217,6 @@ Pod::Spec.new do |s| 'src/core/lib/gpr/env_linux.cc', 'src/core/lib/gpr/env_posix.cc', 'src/core/lib/gpr/env_windows.cc', - 'src/core/lib/gpr/fork.cc', 'src/core/lib/gpr/host_port.cc', 'src/core/lib/gpr/log.cc', 'src/core/lib/gpr/log_android.cc', @@ -242,6 +241,7 @@ Pod::Spec.new do |s| 'src/core/lib/gpr/tmpfile_posix.cc', 'src/core/lib/gpr/tmpfile_windows.cc', 'src/core/lib/gpr/wrap_memcpy.cc', + 'src/core/lib/gprpp/fork.cc', 'src/core/lib/gprpp/thd_posix.cc', 'src/core/lib/gprpp/thd_windows.cc', 'src/core/lib/profiling/basic_timers.cc', @@ -778,7 +778,6 @@ Pod::Spec.new do |s| ss.private_header_files = 'src/core/lib/gpr/arena.h', 'src/core/lib/gpr/env.h', - 'src/core/lib/gpr/fork.h', 'src/core/lib/gpr/host_port.h', 'src/core/lib/gpr/mpscq.h', 'src/core/lib/gpr/murmur_hash.h', @@ -796,6 +795,7 @@ Pod::Spec.new do |s| 'src/core/lib/gprpp/atomic.h', 'src/core/lib/gprpp/atomic_with_atm.h', 'src/core/lib/gprpp/atomic_with_std.h', + 'src/core/lib/gprpp/fork.h', 'src/core/lib/gprpp/manual_constructor.h', 'src/core/lib/gprpp/memory.h', 'src/core/lib/gprpp/thd.h', diff --git a/grpc.gemspec b/grpc.gemspec index 3df7cea33fb..d85d7af4eb6 100644 --- a/grpc.gemspec +++ b/grpc.gemspec @@ -76,7 +76,6 @@ Gem::Specification.new do |s| s.files += %w( include/grpc/impl/codegen/sync_windows.h ) s.files += %w( src/core/lib/gpr/arena.h ) s.files += %w( src/core/lib/gpr/env.h ) - s.files += %w( src/core/lib/gpr/fork.h ) s.files += %w( src/core/lib/gpr/host_port.h ) s.files += %w( src/core/lib/gpr/mpscq.h ) s.files += %w( src/core/lib/gpr/murmur_hash.h ) @@ -94,6 +93,7 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/gprpp/atomic.h ) s.files += %w( src/core/lib/gprpp/atomic_with_atm.h ) s.files += %w( src/core/lib/gprpp/atomic_with_std.h ) + s.files += %w( src/core/lib/gprpp/fork.h ) s.files += %w( src/core/lib/gprpp/manual_constructor.h ) s.files += %w( src/core/lib/gprpp/memory.h ) s.files += %w( src/core/lib/gprpp/thd.h ) @@ -108,7 +108,6 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/gpr/env_linux.cc ) s.files += %w( src/core/lib/gpr/env_posix.cc ) s.files += %w( src/core/lib/gpr/env_windows.cc ) - s.files += %w( src/core/lib/gpr/fork.cc ) s.files += %w( src/core/lib/gpr/host_port.cc ) s.files += %w( src/core/lib/gpr/log.cc ) s.files += %w( src/core/lib/gpr/log_android.cc ) @@ -133,6 +132,7 @@ Gem::Specification.new do |s| s.files += %w( src/core/lib/gpr/tmpfile_posix.cc ) s.files += %w( src/core/lib/gpr/tmpfile_windows.cc ) s.files += %w( src/core/lib/gpr/wrap_memcpy.cc ) + s.files += %w( src/core/lib/gprpp/fork.cc ) s.files += %w( src/core/lib/gprpp/thd_posix.cc ) s.files += %w( src/core/lib/gprpp/thd_windows.cc ) s.files += %w( src/core/lib/profiling/basic_timers.cc ) diff --git a/grpc.gyp b/grpc.gyp index 38597a5b4f3..d1acde001b3 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -182,7 +182,6 @@ 'src/core/lib/gpr/env_linux.cc', 'src/core/lib/gpr/env_posix.cc', 'src/core/lib/gpr/env_windows.cc', - 'src/core/lib/gpr/fork.cc', 'src/core/lib/gpr/host_port.cc', 'src/core/lib/gpr/log.cc', 'src/core/lib/gpr/log_android.cc', @@ -207,6 +206,7 @@ 'src/core/lib/gpr/tmpfile_posix.cc', 'src/core/lib/gpr/tmpfile_windows.cc', 'src/core/lib/gpr/wrap_memcpy.cc', + 'src/core/lib/gprpp/fork.cc', 'src/core/lib/gprpp/thd_posix.cc', 'src/core/lib/gprpp/thd_windows.cc', 'src/core/lib/profiling/basic_timers.cc', diff --git a/package.xml b/package.xml index c4a6f6fc92d..83ad34ee66a 100644 --- a/package.xml +++ b/package.xml @@ -83,7 +83,6 @@ - @@ -101,6 +100,7 @@ + @@ -115,7 +115,6 @@ - @@ -140,6 +139,7 @@ + diff --git a/src/core/lib/gpr/fork.cc b/src/core/lib/gprpp/fork.cc similarity index 62% rename from src/core/lib/gpr/fork.cc rename to src/core/lib/gprpp/fork.cc index ec25848bd02..78f6a2fa0d2 100644 --- a/src/core/lib/gpr/fork.cc +++ b/src/core/lib/gprpp/fork.cc @@ -18,7 +18,7 @@ #include -#include "src/core/lib/gpr/fork.h" +#include "src/core/lib/gprpp/fork.h" #include @@ -28,12 +28,15 @@ #include "src/core/lib/gpr/env.h" #include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/memory.h" /* * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK * AROUND VERY SPECIFIC USE CASES. */ +namespace grpc_core { +namespace internal { // The exec_ctx_count has 2 modes, blocked and unblocked. // When unblocked, the count is 2-indexed; exec_ctx_count=2 indicates // 0 active ExecCtxs, exex_ctx_count=3 indicates 1 active ExecCtxs... @@ -45,16 +48,15 @@ #define BLOCKED(n) (n) class ExecCtxState { - public: - ExecCtxState() : fork_complete_(true) { - gpr_mu_init(&mu_); - gpr_cv_init(&cv_); - gpr_atm_no_barrier_store(&count_, UNBLOCKED(0)); - } + public: + ExecCtxState() : fork_complete_(true) { + gpr_mu_init(&mu_); + gpr_cv_init(&cv_); + gpr_atm_no_barrier_store(&count_, UNBLOCKED(0)); + } void IncExecCtxCount() { - intptr_t count = static_cast( - gpr_atm_no_barrier_load(&count_)); + intptr_t count = static_cast(gpr_atm_no_barrier_load(&count_)); while (true) { if (count <= BLOCKED(1)) { // This only occurs if we are trying to fork. Wait until the fork() @@ -62,27 +64,22 @@ class ExecCtxState { gpr_mu_lock(&mu_); if (gpr_atm_no_barrier_load(&count_) <= BLOCKED(1)) { while (!fork_complete_) { - gpr_cv_wait(&cv_, &mu_, - gpr_inf_future(GPR_CLOCK_REALTIME)); + gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME)); } } gpr_mu_unlock(&mu_); - } else if (gpr_atm_no_barrier_cas(&count_, count, - count + 1)) { + } else if (gpr_atm_no_barrier_cas(&count_, count, count + 1)) { break; } count = gpr_atm_no_barrier_load(&count_); } } - void DecExecCtxCount() { - gpr_atm_no_barrier_fetch_add(&count_, -1); - } + void DecExecCtxCount() { gpr_atm_no_barrier_fetch_add(&count_, -1); } bool BlockExecCtx() { // Assumes there is an active ExecCtx when this function is called - if (gpr_atm_no_barrier_cas(&count_, UNBLOCKED(1), - BLOCKED(1))) { + if (gpr_atm_no_barrier_cas(&count_, UNBLOCKED(1), BLOCKED(1))) { fork_complete_ = false; return true; } @@ -94,21 +91,27 @@ class ExecCtxState { gpr_atm_no_barrier_store(&count_, UNBLOCKED(0)); fork_complete_ = true; gpr_cv_broadcast(&cv_); - gpr_mu_unlock(&g_mu); + gpr_mu_unlock(&mu_); } - void ~ExecCtxState() { + ~ExecCtxState() { gpr_mu_destroy(&mu_); gpr_cv_destroy(&cv_); } -} + + private: + bool fork_complete_; + gpr_mu mu_; + gpr_cv cv_; + gpr_atm count_; +}; class ThreadState { - public: - ThreadState() : awaiting_threads_(false), threads_done_(false), count_(0) { - gpr_mu_init(&mu_); - gpr_cv_init(&cv_); - } + public: + ThreadState() : awaiting_threads_(false), threads_done_(false), count_(0) { + gpr_mu_init(&mu_); + gpr_cv_init(&cv_); + } void IncThreadCount() { gpr_mu_lock(&mu_); @@ -120,7 +123,7 @@ class ThreadState { gpr_mu_lock(&mu_); count_--; if (awaiting_threads_ && count_ == 0) { - threads_done = true; + threads_done_ = true; gpr_cv_signal(&cv_); } gpr_mu_unlock(&mu_); @@ -130,8 +133,7 @@ class ThreadState { awaiting_threads_ = true; threads_done_ = (count_ == 0); while (!threads_done_) { - gpr_cv_wait(&cv_, &mu_, - gpr_inf_future(GPR_CLOCK_REALTIME)); + gpr_cv_wait(&cv_, &mu_, gpr_inf_future(GPR_CLOCK_REALTIME)); } awaiting_threads_ = true; gpr_mu_unlock(&mu_); @@ -141,13 +143,22 @@ class ThreadState { gpr_mu_destroy(&mu_); gpr_cv_destroy(&cv_); } -} -static void Fork::GlobalInit() { + private: + bool awaiting_threads_; + bool threads_done_; + gpr_mu mu_; + gpr_cv cv_; + int count_; +}; + +} // namespace + +void Fork::GlobalInit() { #ifdef GRPC_ENABLE_FORK_SUPPORT - bool supportEnabled_ = true; + supportEnabled_ = true; #else - bool supportEnabled_ = false; + supportEnabled_ = false; #endif bool env_var_set = false; char* env = gpr_getenv("GRPC_ENABLE_FORK_SUPPORT"); @@ -178,62 +189,52 @@ static void Fork::GlobalInit() { supportEnabled_ = (overrideEnabled_ == 1); } if (supportEnabled_) { - execCtxState_ = grpc_core::New(); - threadState_ = grpc_core::New(); + execCtxState_ = grpc_core::New(); + threadState_ = grpc_core::New(); } } - static void Fork::GlobalShutdown() { - if (supportEnabled_) { - grpc_core::Delete(execCtxState_); - grpc_core::Delete(threadState_); - } +void Fork::GlobalShutdown() { + if (supportEnabled_) { + grpc_core::Delete(execCtxState_); + grpc_core::Delete(threadState_); } +} - static bool Fork::Enabled() { - return supportEnabled_; - } +bool Fork::Enabled() { return supportEnabled_; } - // Testing Only - static void Fork::Enable(bool enable) { - overrideEnabled_ = enable ? 1 : 0; - } +// Testing Only +void Fork::Enable(bool enable) { overrideEnabled_ = enable ? 1 : 0; } - static void Fork::IncExecCtxCount() { - if(supportEnabled_) { - execCtxState->IncExecCtxCount(); - } +void Fork::IncExecCtxCount() { + if (supportEnabled_) { + execCtxState_->IncExecCtxCount(); } +} - static void Fork::DecExecCtxCount() { - if(supportEnabled_) { - execCtxState->DecExecCtxCount(); - } +void Fork::DecExecCtxCount() { + if (supportEnabled_) { + execCtxState_->DecExecCtxCount(); } +} - static bool Fork::BlockExecCtx() { - if(supportEnabled_) { - return execCtxState->BlockExecCtx(); - } - return false; +bool Fork::BlockExecCtx() { + if (supportEnabled_) { + return execCtxState_->BlockExecCtx(); } + return false; +} - static void Fork::AllowExecCtx() { - execCtxState->AllowExecCtx(); - } +void Fork::AllowExecCtx() { execCtxState_->AllowExecCtx(); } - static void Fork::IncThreadCount() { - threadState->IncThreadCount(); - } +void Fork::IncThreadCount() { threadState_->IncThreadCount(); } - static void Fork::DecThreadCount() { - threadState_->DecThreadCount(); - } - static void Fork::AwaitThreads() { - threadState_->AwaitThreads(); - } +void Fork::DecThreadCount() { threadState_->DecThreadCount(); } +void Fork::AwaitThreads() { threadState_->AwaitThreads(); } -private: - ExecCtxState* execCtxState_; - ThreadState* threadState_; -} +internal::ExecCtxState* Fork::execCtxState_ = nullptr; +internal::ThreadState* Fork::threadState_ = nullptr; +bool Fork::supportEnabled_ = false; +int Fork::overrideEnabled_ = -1; + +} // namespace grpc_core diff --git a/src/core/lib/gpr/fork.h b/src/core/lib/gprpp/fork.h similarity index 76% rename from src/core/lib/gpr/fork.h rename to src/core/lib/gprpp/fork.h index bf5acc3292d..830bb6b5050 100644 --- a/src/core/lib/gpr/fork.h +++ b/src/core/lib/gprpp/fork.h @@ -26,14 +26,12 @@ namespace grpc_core { -namespace { - class ExecCtxState; - class ThreadState; -} - namespace internal { +class ExecCtxState; +class ThreadState; +} // namespace internal -class ForkSupport { +class Fork { public: static void GlobalInit(); static void GlobalShutdown(); @@ -43,40 +41,39 @@ class ForkSupport { // Increment the count of active ExecCtxs. // Will block until a pending fork is complete if one is in progress. - void IncExecCtxCount(); + static void IncExecCtxCount(); // Decrement the count of active ExecCtxs - void DecExecCtxCount(); + static void DecExecCtxCount(); // Check if there is a single active ExecCtx // (the one used to invoke this function). If there are more, // return false. Otherwise, return true and block creation of // more ExecCtx s until AlloWExecCtx() is called // - bool BlockExecCtx(); - void AllowExecCtx(); + static bool BlockExecCtx(); + static void AllowExecCtx(); // Increment the count of active threads. - void IncThreadCount(); + static void IncThreadCount(); // Decrement the count of active threads. - void DecThreadCount(); + static void DecThreadCount(); // Await all core threads to be joined. - void AwaitThreads(); + static void AwaitThreads(); // Test only: overrides environment variables/compile flags // Must be called before grpc_init() - void Enable(bool enable); + static void Enable(bool enable); private: - static ExecCtxState* execCtxState_ = nullptr; - static ThreadState* threadState_ = nullptr; - static bool supportEnabled_ = false; - static int overrideEnabled_ = -1; -} - -} // namespace internal -} // namespace grpc_core + static internal::ExecCtxState* execCtxState_; + static internal::ThreadState* threadState_; + static bool supportEnabled_; + static int overrideEnabled_; +}; + +} // namespace grpc_core #endif /* GRPC_CORE_LIB_GPR_FORK_H */ diff --git a/src/core/lib/gprpp/thd_posix.cc b/src/core/lib/gprpp/thd_posix.cc index 1a20a0bbc34..533c07e7d88 100644 --- a/src/core/lib/gprpp/thd_posix.cc +++ b/src/core/lib/gprpp/thd_posix.cc @@ -32,8 +32,8 @@ #include #include -#include "src/core/lib/gpr/fork.h" #include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/fork.h" #include "src/core/lib/gprpp/memory.h" namespace grpc_core { @@ -63,7 +63,7 @@ class ThreadInternalsPosix info->body = thd_body; info->arg = arg; info->name = thd_name; - grpc_fork_inc_thd_count(); + grpc_core::Fork::IncThreadCount(); GPR_ASSERT(pthread_attr_init(&attr) == 0); GPR_ASSERT(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) == @@ -98,7 +98,7 @@ class ThreadInternalsPosix gpr_mu_unlock(&arg.thread->mu_); (*arg.body)(arg.arg); - grpc_fork_dec_thd_count(); + grpc_core::Fork::DecThreadCount(); return nullptr; }, info) == 0); @@ -108,7 +108,7 @@ class ThreadInternalsPosix if (!success) { /* don't use gpr_free, as this was allocated using malloc (see above) */ free(info); - grpc_fork_dec_thd_count(); + grpc_core::Fork::DecThreadCount(); } }; diff --git a/src/core/lib/iomgr/exec_ctx.h b/src/core/lib/iomgr/exec_ctx.h index b5b59621f9e..5eb4faaea37 100644 --- a/src/core/lib/iomgr/exec_ctx.h +++ b/src/core/lib/iomgr/exec_ctx.h @@ -25,8 +25,8 @@ #include #include -#include "src/core/lib/gpr/fork.h" #include "src/core/lib/gpr/tls.h" +#include "src/core/lib/gprpp/fork.h" #include "src/core/lib/iomgr/closure.h" typedef gpr_atm grpc_millis; @@ -78,13 +78,13 @@ class ExecCtx { /** Default Constructor */ ExecCtx() : flags_(GRPC_EXEC_CTX_FLAG_IS_FINISHED) { - grpc_fork_inc_exec_ctx_count(); + grpc_core::Fork::IncExecCtxCount(); Set(this); } /** Parameterised Constructor */ ExecCtx(uintptr_t fl) : flags_(fl) { - grpc_fork_inc_exec_ctx_count(); + grpc_core::Fork::IncExecCtxCount(); Set(this); } @@ -93,7 +93,7 @@ class ExecCtx { flags_ |= GRPC_EXEC_CTX_FLAG_IS_FINISHED; Flush(); Set(last_exec_ctx_); - grpc_fork_dec_exec_ctx_count(); + grpc_core::Fork::DecExecCtxCount(); } /** Disallow copy and assignment operators */ diff --git a/src/core/lib/iomgr/fork_posix.cc b/src/core/lib/iomgr/fork_posix.cc index dd0f9f612f7..b37384b8db7 100644 --- a/src/core/lib/iomgr/fork_posix.cc +++ b/src/core/lib/iomgr/fork_posix.cc @@ -28,7 +28,7 @@ #include #include "src/core/lib/gpr/env.h" -#include "src/core/lib/gpr/fork.h" +#include "src/core/lib/gprpp/fork.h" #include "src/core/lib/gprpp/thd.h" #include "src/core/lib/iomgr/ev_posix.h" #include "src/core/lib/iomgr/executor.h" @@ -52,13 +52,13 @@ void grpc_prefork() { if (!grpc_is_initialized()) { return; } - if (!grpc_fork_support_enabled()) { + if (!grpc_core::Fork::Enabled()) { gpr_log(GPR_ERROR, "Fork support not enabled; try running with the " "environment variable GRPC_ENABLE_FORK_SUPPORT=1"); return; } - if (!grpc_fork_block_exec_ctx()) { + if (!grpc_core::Fork::BlockExecCtx()) { gpr_log(GPR_INFO, "Other threads are currently calling into gRPC, skipping fork() " "handlers"); @@ -67,13 +67,13 @@ void grpc_prefork() { grpc_timer_manager_set_threading(false); grpc_executor_set_threading(false); grpc_core::ExecCtx::Get()->Flush(); - grpc_fork_await_thds(); + grpc_core::Fork::AwaitThreads(); skipped_handler = false; } void grpc_postfork_parent() { if (!skipped_handler) { - grpc_fork_allow_exec_ctx(); + grpc_core::Fork::AllowExecCtx(); grpc_core::ExecCtx exec_ctx; grpc_timer_manager_set_threading(true); grpc_executor_set_threading(true); @@ -82,7 +82,7 @@ void grpc_postfork_parent() { void grpc_postfork_child() { if (!skipped_handler) { - grpc_fork_allow_exec_ctx(); + grpc_core::Fork::AllowExecCtx(); grpc_core::ExecCtx exec_ctx; grpc_timer_manager_set_threading(true); grpc_executor_set_threading(true); @@ -90,7 +90,7 @@ void grpc_postfork_child() { } void grpc_fork_handlers_auto_register() { - if (grpc_fork_support_enabled() & !registered_handlers) { + if (grpc_core::Fork::Enabled() & !registered_handlers) { #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK pthread_atfork(grpc_prefork, grpc_postfork_parent, grpc_postfork_child); registered_handlers = true; diff --git a/src/core/lib/surface/init.cc b/src/core/lib/surface/init.cc index 285e6f7ddc7..8bbcef467ad 100644 --- a/src/core/lib/surface/init.cc +++ b/src/core/lib/surface/init.cc @@ -31,8 +31,7 @@ #include "src/core/lib/channel/handshaker_registry.h" #include "src/core/lib/debug/stats.h" #include "src/core/lib/debug/trace.h" -#include "src/core/lib/gpr/fork.h" -#include "src/core/lib/gprpp/thd.h" +#include "src/core/lib/gprpp/fork.h" #include "src/core/lib/http/parser.h" #include "src/core/lib/iomgr/call_combiner.h" #include "src/core/lib/iomgr/combiner.h" @@ -120,7 +119,7 @@ void grpc_init(void) { gpr_mu_lock(&g_init_mu); if (++g_initializations == 1) { - grpc_fork_support_init(); + grpc_core::Fork::GlobalInit(); grpc_fork_handlers_auto_register(); gpr_time_init(); grpc_stats_init(); @@ -176,7 +175,7 @@ void grpc_shutdown(void) { grpc_handshaker_factory_registry_shutdown(); grpc_slice_intern_shutdown(); grpc_stats_shutdown(); - grpc_fork_support_destroy(); + grpc_core::Fork::GlobalShutdown(); } grpc_core::ExecCtx::GlobalShutdown(); } diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index dd3a8f963f8..c1654358a38 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -25,7 +25,6 @@ CORE_SOURCE_FILES = [ 'src/core/lib/gpr/env_linux.cc', 'src/core/lib/gpr/env_posix.cc', 'src/core/lib/gpr/env_windows.cc', - 'src/core/lib/gpr/fork.cc', 'src/core/lib/gpr/host_port.cc', 'src/core/lib/gpr/log.cc', 'src/core/lib/gpr/log_android.cc', @@ -50,6 +49,7 @@ CORE_SOURCE_FILES = [ 'src/core/lib/gpr/tmpfile_posix.cc', 'src/core/lib/gpr/tmpfile_windows.cc', 'src/core/lib/gpr/wrap_memcpy.cc', + 'src/core/lib/gprpp/fork.cc', 'src/core/lib/gprpp/thd_posix.cc', 'src/core/lib/gprpp/thd_windows.cc', 'src/core/lib/profiling/basic_timers.cc', diff --git a/test/core/gpr/fork_test.cc b/test/core/gprpp/fork_test.cc similarity index 60% rename from test/core/gpr/fork_test.cc rename to test/core/gprpp/fork_test.cc index bc388c4a77f..c4a8a4e5f5b 100644 --- a/test/core/gpr/fork_test.cc +++ b/test/core/gprpp/fork_test.cc @@ -16,30 +16,30 @@ * */ -#include "src/core/lib/gpr/fork.h" +#include "src/core/lib/gprpp/fork.h" #include "src/core/lib/gprpp/thd.h" #include "test/core/util/test_config.h" static void test_init() { - GPR_ASSERT(!grpc_fork_support_enabled()); + GPR_ASSERT(!grpc_core::Fork::Enabled()); // Default fork support (disabled) - grpc_fork_support_init(); - GPR_ASSERT(!grpc_fork_support_enabled()); - grpc_fork_support_destroy(); + grpc_core::Fork::GlobalInit(); + GPR_ASSERT(!grpc_core::Fork::Enabled()); + grpc_core::Fork::GlobalShutdown(); // Explicitly disabled fork support - grpc_enable_fork_support(false); - grpc_fork_support_init(); - GPR_ASSERT(!grpc_fork_support_enabled()); - grpc_fork_support_destroy(); + grpc_core::Fork::Enable(false); + grpc_core::Fork::GlobalInit(); + GPR_ASSERT(!grpc_core::Fork::Enabled()); + grpc_core::Fork::GlobalShutdown(); // Explicitly enabled fork support - grpc_enable_fork_support(true); - grpc_fork_support_init(); - GPR_ASSERT(grpc_fork_support_enabled()); - grpc_fork_support_destroy(); + grpc_core::Fork::Enable(true); + grpc_core::Fork::GlobalInit(); + GPR_ASSERT(grpc_core::Fork::Enabled()); + grpc_core::Fork::GlobalShutdown(); } #define THREAD_DELAY_MS 3000 @@ -54,13 +54,13 @@ static void sleeping_thd(void* arg) { static void test_thd_count() { // Test no active threads - grpc_enable_fork_support(true); - grpc_fork_support_init(); - grpc_fork_await_thds(); - grpc_fork_support_destroy(); + grpc_core::Fork::Enable(true); + grpc_core::Fork::GlobalInit(); + grpc_core::Fork::AwaitThreads(); + grpc_core::Fork::GlobalShutdown(); - grpc_enable_fork_support(true); - grpc_fork_support_init(); + grpc_core::Fork::Enable(true); + grpc_core::Fork::GlobalInit(); grpc_core::Thread thds[CONCURRENT_TEST_THREADS]; gpr_timespec est_end_time = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), @@ -74,56 +74,56 @@ static void test_thd_count() { grpc_core::Thread("grpc_fork_test", sleeping_thd, (void*)sleep_time_ms); thds[i].Start(); } - grpc_fork_await_thds(); + grpc_core::Fork::AwaitThreads(); gpr_timespec end_time = gpr_now(GPR_CLOCK_REALTIME); for (auto& thd : thds) { thd.Join(); } GPR_ASSERT(gpr_time_similar(end_time, est_end_time, tolerance)); - grpc_fork_support_destroy(); + grpc_core::Fork::GlobalShutdown(); } static void exec_ctx_thread(void* arg) { bool* exec_ctx_created = (bool*)arg; - grpc_fork_inc_exec_ctx_count(); + grpc_core::Fork::IncExecCtxCount(); *exec_ctx_created = true; } static void test_exec_count() { - grpc_fork_inc_exec_ctx_count(); - grpc_enable_fork_support(true); - grpc_fork_support_init(); - - grpc_fork_inc_exec_ctx_count(); - GPR_ASSERT(grpc_fork_block_exec_ctx()); - grpc_fork_dec_exec_ctx_count(); - grpc_fork_allow_exec_ctx(); - - grpc_fork_inc_exec_ctx_count(); - grpc_fork_inc_exec_ctx_count(); - GPR_ASSERT(!grpc_fork_block_exec_ctx()); - grpc_fork_dec_exec_ctx_count(); - grpc_fork_dec_exec_ctx_count(); - - grpc_fork_inc_exec_ctx_count(); - GPR_ASSERT(grpc_fork_block_exec_ctx()); - grpc_fork_dec_exec_ctx_count(); - grpc_fork_allow_exec_ctx(); - - // Test that block_exec_ctx() blocks grpc_fork_inc_exec_ctx_count + grpc_core::Fork::IncExecCtxCount(); + grpc_core::Fork::Enable(true); + grpc_core::Fork::GlobalInit(); + + grpc_core::Fork::IncExecCtxCount(); + GPR_ASSERT(grpc_core::Fork::BlockExecCtx()); + grpc_core::Fork::DecExecCtxCount(); + grpc_core::Fork::AllowExecCtx(); + + grpc_core::Fork::IncExecCtxCount(); + grpc_core::Fork::IncExecCtxCount(); + GPR_ASSERT(!grpc_core::Fork::BlockExecCtx()); + grpc_core::Fork::DecExecCtxCount(); + grpc_core::Fork::DecExecCtxCount(); + + grpc_core::Fork::IncExecCtxCount(); + GPR_ASSERT(grpc_core::Fork::BlockExecCtx()); + grpc_core::Fork::DecExecCtxCount(); + grpc_core::Fork::AllowExecCtx(); + + // Test that block_exec_ctx() blocks grpc_core::Fork::IncExecCtxCount bool exec_ctx_created = false; grpc_core::Thread thd = grpc_core::Thread("grpc_fork_test", exec_ctx_thread, &exec_ctx_created); - grpc_fork_inc_exec_ctx_count(); - GPR_ASSERT(grpc_fork_block_exec_ctx()); - grpc_fork_dec_exec_ctx_count(); + grpc_core::Fork::IncExecCtxCount(); + GPR_ASSERT(grpc_core::Fork::BlockExecCtx()); + grpc_core::Fork::DecExecCtxCount(); thd.Start(); gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(1, GPR_TIMESPAN))); GPR_ASSERT(!exec_ctx_created); - grpc_fork_allow_exec_ctx(); + grpc_core::Fork::AllowExecCtx(); thd.Join(); // This ensure that the call got un-blocked - grpc_fork_support_destroy(); + grpc_core::Fork::GlobalShutdown(); } int main(int argc, char* argv[]) { diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index ff5abc679d8..095c3c3dcb3 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1023,7 +1023,6 @@ src/core/lib/debug/stats_data.h \ src/core/lib/debug/trace.h \ src/core/lib/gpr/arena.h \ src/core/lib/gpr/env.h \ -src/core/lib/gpr/fork.h \ src/core/lib/gpr/host_port.h \ src/core/lib/gpr/mpscq.h \ src/core/lib/gpr/murmur_hash.h \ @@ -1042,6 +1041,7 @@ src/core/lib/gprpp/atomic.h \ src/core/lib/gprpp/atomic_with_atm.h \ src/core/lib/gprpp/atomic_with_std.h \ src/core/lib/gprpp/debug_location.h \ +src/core/lib/gprpp/fork.h \ src/core/lib/gprpp/inlined_vector.h \ src/core/lib/gprpp/manual_constructor.h \ src/core/lib/gprpp/memory.h \ diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index e7e9e04979e..688ec34abf1 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -1074,8 +1074,6 @@ src/core/lib/gpr/env.h \ src/core/lib/gpr/env_linux.cc \ src/core/lib/gpr/env_posix.cc \ src/core/lib/gpr/env_windows.cc \ -src/core/lib/gpr/fork.cc \ -src/core/lib/gpr/fork.h \ src/core/lib/gpr/host_port.cc \ src/core/lib/gpr/host_port.h \ src/core/lib/gpr/log.cc \ @@ -1119,6 +1117,8 @@ src/core/lib/gprpp/atomic.h \ src/core/lib/gprpp/atomic_with_atm.h \ src/core/lib/gprpp/atomic_with_std.h \ src/core/lib/gprpp/debug_location.h \ +src/core/lib/gprpp/fork.cc \ +src/core/lib/gprpp/fork.h \ src/core/lib/gprpp/inlined_vector.h \ src/core/lib/gprpp/manual_constructor.h \ src/core/lib/gprpp/memory.h \ diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json index 0ddecffa5f6..f609f8de7cf 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -612,7 +612,7 @@ "language": "c", "name": "fork_test", "src": [ - "test/core/gpr/fork_test.cc" + "test/core/gprpp/fork_test.cc" ], "third_party": false, "type": "target" @@ -8779,7 +8779,6 @@ "src/core/lib/gpr/env_linux.cc", "src/core/lib/gpr/env_posix.cc", "src/core/lib/gpr/env_windows.cc", - "src/core/lib/gpr/fork.cc", "src/core/lib/gpr/host_port.cc", "src/core/lib/gpr/log.cc", "src/core/lib/gpr/log_android.cc", @@ -8804,6 +8803,7 @@ "src/core/lib/gpr/tmpfile_posix.cc", "src/core/lib/gpr/tmpfile_windows.cc", "src/core/lib/gpr/wrap_memcpy.cc", + "src/core/lib/gprpp/fork.cc", "src/core/lib/gprpp/thd_posix.cc", "src/core/lib/gprpp/thd_windows.cc", "src/core/lib/profiling/basic_timers.cc", @@ -8836,7 +8836,6 @@ "include/grpc/support/time.h", "src/core/lib/gpr/arena.h", "src/core/lib/gpr/env.h", - "src/core/lib/gpr/fork.h", "src/core/lib/gpr/host_port.h", "src/core/lib/gpr/mpscq.h", "src/core/lib/gpr/murmur_hash.h", @@ -8854,6 +8853,7 @@ "src/core/lib/gprpp/atomic.h", "src/core/lib/gprpp/atomic_with_atm.h", "src/core/lib/gprpp/atomic_with_std.h", + "src/core/lib/gprpp/fork.h", "src/core/lib/gprpp/manual_constructor.h", "src/core/lib/gprpp/memory.h", "src/core/lib/gprpp/thd.h", @@ -8882,7 +8882,6 @@ "include/grpc/support/time.h", "src/core/lib/gpr/arena.h", "src/core/lib/gpr/env.h", - "src/core/lib/gpr/fork.h", "src/core/lib/gpr/host_port.h", "src/core/lib/gpr/mpscq.h", "src/core/lib/gpr/murmur_hash.h", @@ -8900,6 +8899,7 @@ "src/core/lib/gprpp/atomic.h", "src/core/lib/gprpp/atomic_with_atm.h", "src/core/lib/gprpp/atomic_with_std.h", + "src/core/lib/gprpp/fork.h", "src/core/lib/gprpp/manual_constructor.h", "src/core/lib/gprpp/memory.h", "src/core/lib/gprpp/thd.h",