diff --git a/grpc.def b/grpc.def index a9fba8dff2b..e0a08d22c19 100644 --- a/grpc.def +++ b/grpc.def @@ -16,7 +16,7 @@ EXPORTS grpc_init grpc_shutdown grpc_is_initialized - grpc_maybe_wait_for_async_shutdown + grpc_shutdown_blocking grpc_version_string grpc_g_stands_for grpc_completion_queue_factory_lookup diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h index e6988f489f2..eb4248f8eb1 100644 --- a/include/grpc/grpc.h +++ b/include/grpc/grpc.h @@ -86,9 +86,10 @@ GRPCAPI void grpc_shutdown(void); part of stabilizing the fork support API, as tracked in https://github.com/grpc/grpc/issues/15334 */ GRPCAPI int grpc_is_initialized(void); -/** EXPERIMENTAL. Wait for grpc_shutdown to finish if it is in process. + +/** EXPERIMENTAL. Blocking shut down grpc library. This is only for wrapped language to use now. */ -GRPCAPI void grpc_maybe_wait_for_async_shutdown(void); +GRPCAPI void grpc_shutdown_blocking(void); /** Return a string representing the current version of grpc */ GRPCAPI const char* grpc_version_string(void); diff --git a/src/core/lib/surface/init.cc b/src/core/lib/surface/init.cc index fca85ac876a..d8eeaf1c424 100644 --- a/src/core/lib/surface/init.cc +++ b/src/core/lib/surface/init.cc @@ -164,16 +164,8 @@ void grpc_init(void) { GRPC_API_TRACE("grpc_init(void)", 0, ()); } -void grpc_shutdown_internal(void* ignored) { +void grpc_shutdown_internal_locked(void) { int i; - GRPC_API_TRACE("grpc_shutdown_internal", 0, ()); - gpr_mu_lock(&g_init_mu); - // We have released lock from the shutdown thread and it is possible that - // another grpc_init has been called, and do nothing if that is the case. - if (--g_initializations != 0) { - gpr_mu_unlock(&g_init_mu); - return; - } { grpc_core::ExecCtx exec_ctx(0); grpc_iomgr_shutdown_background_closure(); @@ -200,6 +192,18 @@ void grpc_shutdown_internal(void* ignored) { grpc_core::ApplicationCallbackExecCtx::GlobalShutdown(); g_shutting_down = false; gpr_cv_broadcast(g_shutting_down_cv); +} + +void grpc_shutdown_internal(void* ignored) { + GRPC_API_TRACE("grpc_shutdown_internal", 0, ()); + gpr_mu_lock(&g_init_mu); + // We have released lock from the shutdown thread and it is possible that + // another grpc_init has been called, and do nothing if that is the case. + if (--g_initializations != 0) { + gpr_mu_unlock(&g_init_mu); + return; + } + grpc_shutdown_internal_locked(); gpr_mu_unlock(&g_init_mu); } @@ -219,6 +223,16 @@ void grpc_shutdown(void) { gpr_mu_unlock(&g_init_mu); } +void grpc_shutdown_blocking(void) { + GRPC_API_TRACE("grpc_shutdown_blocking(void)", 0, ()); + gpr_mu_lock(&g_init_mu); + if (--g_initializations == 0) { + g_shutting_down = true; + grpc_shutdown_internal_locked(); + } + gpr_mu_unlock(&g_init_mu); +} + int grpc_is_initialized(void) { int r; gpr_once_init(&g_basic_init, do_basic_init); diff --git a/src/core/lib/surface/init.h b/src/core/lib/surface/init.h index 193f51447d9..6eaa488d054 100644 --- a/src/core/lib/surface/init.h +++ b/src/core/lib/surface/init.h @@ -22,5 +22,6 @@ void grpc_register_security_filters(void); void grpc_security_pre_init(void); void grpc_security_init(void); +void grpc_maybe_wait_for_async_shutdown(void); #endif /* GRPC_CORE_LIB_SURFACE_INIT_H */ diff --git a/src/php/ext/grpc/php_grpc.c b/src/php/ext/grpc/php_grpc.c index 256efad37a8..fa6f0be837b 100644 --- a/src/php/ext/grpc/php_grpc.c +++ b/src/php/ext/grpc/php_grpc.c @@ -361,8 +361,7 @@ PHP_MSHUTDOWN_FUNCTION(grpc) { zend_hash_destroy(&grpc_target_upper_bound_map); grpc_shutdown_timeval(TSRMLS_C); grpc_php_shutdown_completion_queue(TSRMLS_C); - grpc_shutdown(); - grpc_maybe_wait_for_async_shutdown(); + grpc_shutdown_blocking(); GRPC_G(initialized) = 0; } return SUCCESS; diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.c b/src/ruby/ext/grpc/rb_grpc_imports.generated.c index 0ff5bcbf44e..fdbe0df4e52 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.c +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.c @@ -39,7 +39,7 @@ grpc_register_plugin_type grpc_register_plugin_import; grpc_init_type grpc_init_import; grpc_shutdown_type grpc_shutdown_import; grpc_is_initialized_type grpc_is_initialized_import; -grpc_maybe_wait_for_async_shutdown_type grpc_maybe_wait_for_async_shutdown_import; +grpc_shutdown_blocking_type grpc_shutdown_blocking_import; grpc_version_string_type grpc_version_string_import; grpc_g_stands_for_type grpc_g_stands_for_import; grpc_completion_queue_factory_lookup_type grpc_completion_queue_factory_lookup_import; @@ -307,7 +307,7 @@ void grpc_rb_load_imports(HMODULE library) { grpc_init_import = (grpc_init_type) GetProcAddress(library, "grpc_init"); grpc_shutdown_import = (grpc_shutdown_type) GetProcAddress(library, "grpc_shutdown"); grpc_is_initialized_import = (grpc_is_initialized_type) GetProcAddress(library, "grpc_is_initialized"); - grpc_maybe_wait_for_async_shutdown_import = (grpc_maybe_wait_for_async_shutdown_type) GetProcAddress(library, "grpc_maybe_wait_for_async_shutdown"); + grpc_shutdown_blocking_import = (grpc_shutdown_blocking_type) GetProcAddress(library, "grpc_shutdown_blocking"); grpc_version_string_import = (grpc_version_string_type) GetProcAddress(library, "grpc_version_string"); grpc_g_stands_for_import = (grpc_g_stands_for_type) GetProcAddress(library, "grpc_g_stands_for"); grpc_completion_queue_factory_lookup_import = (grpc_completion_queue_factory_lookup_type) GetProcAddress(library, "grpc_completion_queue_factory_lookup"); diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.h b/src/ruby/ext/grpc/rb_grpc_imports.generated.h index 3008e631115..cf16f0ca33b 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.h +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.h @@ -92,9 +92,9 @@ extern grpc_shutdown_type grpc_shutdown_import; typedef int(*grpc_is_initialized_type)(void); extern grpc_is_initialized_type grpc_is_initialized_import; #define grpc_is_initialized grpc_is_initialized_import -typedef void(*grpc_maybe_wait_for_async_shutdown_type)(void); -extern grpc_maybe_wait_for_async_shutdown_type grpc_maybe_wait_for_async_shutdown_import; -#define grpc_maybe_wait_for_async_shutdown grpc_maybe_wait_for_async_shutdown_import +typedef void(*grpc_shutdown_blocking_type)(void); +extern grpc_shutdown_blocking_type grpc_shutdown_blocking_import; +#define grpc_shutdown_blocking grpc_shutdown_blocking_import typedef const char*(*grpc_version_string_type)(void); extern grpc_version_string_type grpc_version_string_import; #define grpc_version_string grpc_version_string_import diff --git a/test/core/memory_usage/client.cc b/test/core/memory_usage/client.cc index 9552e1b88ed..097288c5efa 100644 --- a/test/core/memory_usage/client.cc +++ b/test/core/memory_usage/client.cc @@ -29,7 +29,6 @@ #include "src/core/lib/gpr/env.h" #include "src/core/lib/gpr/string.h" #include "src/core/lib/gpr/useful.h" -#include "src/core/lib/surface/init.h" #include "test/core/util/cmdline.h" #include "test/core/util/memory_counters.h" @@ -286,8 +285,7 @@ int main(int argc, char** argv) { grpc_slice_unref(slice); grpc_completion_queue_destroy(cq); - grpc_shutdown(); - grpc_maybe_wait_for_async_shutdown(); + grpc_shutdown_blocking(); gpr_log(GPR_INFO, "---------client stats--------"); gpr_log( diff --git a/test/core/memory_usage/server.cc b/test/core/memory_usage/server.cc index 0c67ee4fcdf..6fb14fa31a0 100644 --- a/test/core/memory_usage/server.cc +++ b/test/core/memory_usage/server.cc @@ -34,7 +34,6 @@ #include #include "src/core/lib/gpr/host_port.h" -#include "src/core/lib/surface/init.h" #include "test/core/end2end/data/ssl_test_data.h" #include "test/core/util/cmdline.h" #include "test/core/util/memory_counters.h" @@ -319,8 +318,7 @@ int main(int argc, char** argv) { grpc_server_destroy(server); grpc_completion_queue_destroy(cq); - grpc_shutdown(); - grpc_maybe_wait_for_async_shutdown(); + grpc_shutdown_blocking(); grpc_memory_counters_destroy(); return 0; } diff --git a/test/core/surface/init_test.cc b/test/core/surface/init_test.cc index 0ca83b4359c..583dd1b6de9 100644 --- a/test/core/surface/init_test.cc +++ b/test/core/surface/init_test.cc @@ -36,6 +36,16 @@ static void test(int rounds) { grpc_maybe_wait_for_async_shutdown(); } +static void test_blocking(int rounds) { + int i; + for (i = 0; i < rounds; i++) { + grpc_init(); + } + for (i = 0; i < rounds; i++) { + grpc_shutdown_blocking(); + } +} + static void test_mixed(void) { grpc_init(); grpc_init(); @@ -53,8 +63,7 @@ static void test_plugin() { grpc_register_plugin(plugin_init, plugin_destroy); grpc_init(); GPR_ASSERT(g_flag == 1); - grpc_shutdown(); - grpc_maybe_wait_for_async_shutdown(); + grpc_shutdown_blocking(); GPR_ASSERT(g_flag == 2); } @@ -71,6 +80,9 @@ int main(int argc, char** argv) { test(1); test(2); test(3); + test_blocking(1); + test_blocking(2); + test_blocking(3); test_mixed(); test_plugin(); test_repeatedly(); diff --git a/test/core/surface/public_headers_must_be_c89.c b/test/core/surface/public_headers_must_be_c89.c index 200dba1a1d9..04d0506b3c2 100644 --- a/test/core/surface/public_headers_must_be_c89.c +++ b/test/core/surface/public_headers_must_be_c89.c @@ -78,7 +78,7 @@ int main(int argc, char **argv) { printf("%lx", (unsigned long) grpc_init); printf("%lx", (unsigned long) grpc_shutdown); printf("%lx", (unsigned long) grpc_is_initialized); - printf("%lx", (unsigned long) grpc_maybe_wait_for_async_shutdown); + printf("%lx", (unsigned long) grpc_shutdown_blocking); printf("%lx", (unsigned long) grpc_version_string); printf("%lx", (unsigned long) grpc_g_stands_for); printf("%lx", (unsigned long) grpc_completion_queue_factory_lookup); diff --git a/test/core/util/port.cc b/test/core/util/port.cc index 14d648b7eaf..fe4caa6faf6 100644 --- a/test/core/util/port.cc +++ b/test/core/util/port.cc @@ -34,7 +34,6 @@ #include "src/core/lib/http/httpcli.h" #include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/sockaddr_utils.h" -#include "src/core/lib/surface/init.h" #include "test/core/util/port_server_client.h" static int* chosen_ports = nullptr; @@ -67,8 +66,7 @@ static void free_chosen_ports(void) { for (i = 0; i < num_chosen_ports; i++) { grpc_free_port_using_server(chosen_ports[i]); } - grpc_shutdown(); - grpc_maybe_wait_for_async_shutdown(); + grpc_shutdown_blocking(); gpr_free(chosen_ports); } diff --git a/test/core/util/test_config.cc b/test/core/util/test_config.cc index 0caca1b164c..0c0492fdbbd 100644 --- a/test/core/util/test_config.cc +++ b/test/core/util/test_config.cc @@ -25,7 +25,6 @@ #include #include -#include #include #include diff --git a/test/cpp/naming/address_sorting_test.cc b/test/cpp/naming/address_sorting_test.cc index 33c35c46409..e6b14888ffb 100644 --- a/test/cpp/naming/address_sorting_test.cc +++ b/test/cpp/naming/address_sorting_test.cc @@ -47,7 +47,6 @@ #include "src/core/lib/iomgr/iomgr.h" #include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/sockaddr_utils.h" -#include "src/core/lib/surface/init.h" #include "test/core/util/port.h" #include "test/core/util/test_config.h" @@ -198,10 +197,7 @@ void VerifyLbAddrOutputs(const grpc_core::ServerAddressList addresses, class AddressSortingTest : public ::testing::Test { protected: void SetUp() override { grpc_init(); } - void TearDown() override { - grpc_shutdown(); - grpc_maybe_wait_for_async_shutdown(); - } + void TearDown() override { grpc_shutdown_blocking(); } }; /* Tests for rule 1 */