diff --git a/include/grpc/impl/codegen/port_platform.h b/include/grpc/impl/codegen/port_platform.h index 1a93be38e01..1906886d5e2 100644 --- a/include/grpc/impl/codegen/port_platform.h +++ b/include/grpc/impl/codegen/port_platform.h @@ -182,7 +182,6 @@ #ifndef _BSD_SOURCE #define _BSD_SOURCE #endif -#define TLS_NO_SUPPORT 1 /* thread-local storage not supported. Use GPR_TLS */ #if TARGET_OS_IPHONE #define GPR_PLATFORM_STRING "ios" #define GPR_CPU_IPHONE 1 diff --git a/src/core/lib/iomgr/exec_ctx.cc b/src/core/lib/iomgr/exec_ctx.cc index f4247db754d..e005437e0a8 100644 --- a/src/core/lib/iomgr/exec_ctx.cc +++ b/src/core/lib/iomgr/exec_ctx.cc @@ -104,17 +104,11 @@ static grpc_closure_scheduler exec_ctx_scheduler = {&exec_ctx_scheduler_vtable}; grpc_closure_scheduler* grpc_schedule_on_exec_ctx = &exec_ctx_scheduler; namespace grpc_core { -#ifdef TLS_NO_SUPPORT GPR_TLS_CLASS_DEF(ExecCtx::exec_ctx_); -#else -thread_local ExecCtx* ExecCtx::exec_ctx_ = nullptr; -#endif void ExecCtx::GlobalInit(void) { g_start_time = gpr_now(GPR_CLOCK_MONOTONIC); -#ifdef TLS_NO_SUPPORT gpr_tls_init(&exec_ctx_); -#endif } bool ExecCtx::Flush() { diff --git a/src/core/lib/iomgr/exec_ctx.h b/src/core/lib/iomgr/exec_ctx.h index b04aab61fab..c8700f33524 100644 --- a/src/core/lib/iomgr/exec_ctx.h +++ b/src/core/lib/iomgr/exec_ctx.h @@ -170,19 +170,11 @@ on outside context */ static void GlobalInit(void); /** Global shutdown for ExecCtx. Called by iomgr */ - static void GlobalShutdown(void) { -#ifdef TLS_NO_SUPPORT - gpr_tls_destroy(&exec_ctx_); -#endif - } + static void GlobalShutdown(void) { gpr_tls_destroy(&exec_ctx_); } /** Gets pointer to current exec_ctx */ static ExecCtx* Get() { -#ifdef TLS_NO_SUPPORT return reinterpret_cast(gpr_tls_get(&exec_ctx_)); -#else - return exec_ctx_; -#endif } protected: @@ -192,11 +184,7 @@ on outside context */ private: /** Set exec_ctx_ to exec_ctx */ void Set(ExecCtx* exec_ctx) { -#ifdef TLS_NO_SUPPORT gpr_tls_set(&exec_ctx_, reinterpret_cast(exec_ctx)); -#else - exec_ctx_ = exec_ctx; -#endif } grpc_closure_list closure_list_ = GRPC_CLOSURE_LIST_INIT; @@ -207,11 +195,7 @@ on outside context */ bool now_is_valid_ = false; grpc_millis now_ = 0; -#ifdef TLS_NO_SUPPORT GPR_TLS_CLASS_DECL(exec_ctx_); -#else - static thread_local ExecCtx* exec_ctx_; -#endif ExecCtx* last_exec_ctx_ = Get(); }; } // namespace grpc_core diff --git a/src/core/lib/iomgr/iomgr.cc b/src/core/lib/iomgr/iomgr.cc index 570d97443d8..27e93762720 100644 --- a/src/core/lib/iomgr/iomgr.cc +++ b/src/core/lib/iomgr/iomgr.cc @@ -46,15 +46,12 @@ static int g_shutdown; static grpc_iomgr_object g_root_object; void grpc_iomgr_init() { + grpc_core::ExecCtx _local_exec_ctx; g_shutdown = 0; gpr_mu_init(&g_mu); gpr_cv_init(&g_rcv); - grpc_core::ExecCtx::GlobalInit(); - { - grpc_core::ExecCtx _local_exec_ctx; - grpc_executor_init(); - grpc_timer_list_init(); - } + grpc_executor_init(); + grpc_timer_list_init(); g_root_object.next = g_root_object.prev = &g_root_object; g_root_object.name = (char*)"root"; grpc_network_status_init(); @@ -85,7 +82,6 @@ void grpc_iomgr_shutdown() { gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME); { - grpc_core::ExecCtx _local_exec_ctx(0); grpc_timer_manager_shutdown(); grpc_iomgr_platform_flush(); grpc_executor_shutdown(); @@ -141,8 +137,8 @@ void grpc_iomgr_shutdown() { } } gpr_mu_unlock(&g_mu); - grpc_timer_list_shutdown(); + grpc_core::ExecCtx::Get()->Flush(); } /* ensure all threads have left g_mu */ @@ -150,7 +146,6 @@ void grpc_iomgr_shutdown() { gpr_mu_unlock(&g_mu); grpc_iomgr_platform_shutdown(); - grpc_core::ExecCtx::GlobalShutdown(); grpc_network_status_shutdown(); gpr_mu_destroy(&g_mu); gpr_cv_destroy(&g_rcv); diff --git a/src/core/lib/surface/init.cc b/src/core/lib/surface/init.cc index 2d7a33e147b..e6ff475a90a 100644 --- a/src/core/lib/surface/init.cc +++ b/src/core/lib/surface/init.cc @@ -124,6 +124,7 @@ void grpc_init(void) { grpc_mdctx_global_init(); grpc_channel_init_init(); grpc_security_pre_init(); + grpc_core::ExecCtx::GlobalInit(); grpc_iomgr_init(); gpr_timers_global_init(); grpc_handshaker_factory_registry_init(); @@ -156,8 +157,8 @@ void grpc_shutdown(void) { gpr_mu_lock(&g_init_mu); if (--g_initializations == 0) { { + grpc_core::ExecCtx _local_exec_ctx(0); { - grpc_core::ExecCtx _local_exec_ctx(0); grpc_executor_shutdown(); grpc_timer_manager_set_threading( false); // shutdown timer_manager thread @@ -166,15 +167,16 @@ void grpc_shutdown(void) { g_all_of_the_plugins[i].destroy(); } } - grpc_mdctx_global_shutdown(); - grpc_handshaker_factory_registry_shutdown(); } - grpc_iomgr_shutdown(); gpr_timers_global_destroy(); grpc_tracer_shutdown(); + grpc_handshaker_factory_registry_shutdown(); + grpc_iomgr_shutdown(); + grpc_mdctx_global_shutdown(); grpc_slice_intern_shutdown(); grpc_stats_shutdown(); } + grpc_core::ExecCtx::GlobalShutdown(); } gpr_mu_unlock(&g_init_mu); } diff --git a/src/objective-c/tests/CoreCronetEnd2EndTests/CoreCronetEnd2EndTests.mm b/src/objective-c/tests/CoreCronetEnd2EndTests/CoreCronetEnd2EndTests.mm index 55c5105de4c..7a09e8f3952 100644 --- a/src/objective-c/tests/CoreCronetEnd2EndTests/CoreCronetEnd2EndTests.mm +++ b/src/objective-c/tests/CoreCronetEnd2EndTests/CoreCronetEnd2EndTests.mm @@ -110,7 +110,7 @@ static void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) { static void cronet_init_client_simple_ssl_secure_fullstack( grpc_end2end_test_fixture *f, grpc_channel_args *client_args) { - grpc_exec_ctx _local_exec_ctx; + grpc_core::ExecCtx _local_exec_ctx; stream_engine *cronetEngine = [Cronet getGlobalEngine]; grpc_channel_args *new_client_args = grpc_channel_args_copy(client_args); diff --git a/test/core/end2end/fuzzers/api_fuzzer.cc b/test/core/end2end/fuzzers/api_fuzzer.cc index f3127e92cc4..ce916bd3414 100644 --- a/test/core/end2end/fuzzers/api_fuzzer.cc +++ b/test/core/end2end/fuzzers/api_fuzzer.cc @@ -745,8 +745,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { input_stream inp = {data, data + size}; grpc_tcp_client_connect_impl = my_tcp_client_connect; gpr_now_impl = now_impl; - grpc_timer_manager_set_threading(false); grpc_init(); + grpc_timer_manager_set_threading(false); { grpc_core::ExecCtx _local_exec_ctx; grpc_executor_set_threading(false); diff --git a/test/core/http/httpscli_test.cc b/test/core/http/httpscli_test.cc index ee2bff18dbb..418afb6992e 100644 --- a/test/core/http/httpscli_test.cc +++ b/test/core/http/httpscli_test.cc @@ -89,7 +89,7 @@ static void test_get(int port) { "pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&g_pops), &worker, n_seconds_time(1)))); gpr_mu_unlock(g_mu); - + grpc_core::ExecCtx::Get()->Flush(); gpr_mu_lock(g_mu); } gpr_mu_unlock(g_mu); @@ -129,7 +129,7 @@ static void test_post(int port) { "pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&g_pops), &worker, n_seconds_time(1)))); gpr_mu_unlock(g_mu); - + grpc_core::ExecCtx::Get()->Flush(); gpr_mu_lock(g_mu); } gpr_mu_unlock(g_mu); @@ -144,7 +144,6 @@ static void destroy_pops(void* p, grpc_error* error) { int main(int argc, char** argv) { grpc_closure destroyed; - grpc_core::ExecCtx _local_exec_ctx; gpr_subprocess* server; char* me = argv[0]; char* lslash = strrchr(me, '/'); @@ -196,6 +195,7 @@ int main(int argc, char** argv) { test_post(port); { + grpc_core::ExecCtx _local_exec_ctx; grpc_httpcli_context_destroy(&g_context); GRPC_CLOSURE_INIT(&destroyed, destroy_pops, &g_pops, grpc_schedule_on_exec_ctx); diff --git a/test/core/slice/percent_decode_fuzzer.cc b/test/core/slice/percent_decode_fuzzer.cc index 3603177c47f..81eb031014f 100644 --- a/test/core/slice/percent_decode_fuzzer.cc +++ b/test/core/slice/percent_decode_fuzzer.cc @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -31,6 +32,7 @@ bool leak_check = true; extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { struct grpc_memory_counters counters; + grpc_init(); grpc_memory_counters_init(); grpc_slice input = grpc_slice_from_copied_buffer((const char*)data, size); grpc_slice output; @@ -46,6 +48,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { grpc_slice_unref(input); counters = grpc_memory_counters_snapshot(); grpc_memory_counters_destroy(); + grpc_shutdown(); GPR_ASSERT(counters.total_size_relative == 0); return 0; } diff --git a/test/core/slice/percent_encode_fuzzer.cc b/test/core/slice/percent_encode_fuzzer.cc index c8e3849fc8e..201ae2790eb 100644 --- a/test/core/slice/percent_encode_fuzzer.cc +++ b/test/core/slice/percent_encode_fuzzer.cc @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -31,6 +32,7 @@ bool leak_check = true; static void test(const uint8_t* data, size_t size, const uint8_t* dict) { struct grpc_memory_counters counters; + grpc_init(); grpc_memory_counters_init(); grpc_slice input = grpc_slice_from_copied_buffer((const char*)data, size); grpc_slice output = grpc_percent_encode_slice(input, dict); @@ -48,6 +50,7 @@ static void test(const uint8_t* data, size_t size, const uint8_t* dict) { grpc_slice_unref(permissive_decoded_output); counters = grpc_memory_counters_snapshot(); grpc_memory_counters_destroy(); + grpc_shutdown(); GPR_ASSERT(counters.total_size_relative == 0); } diff --git a/test/core/util/one_corpus_entry_fuzzer.cc b/test/core/util/one_corpus_entry_fuzzer.cc index fc75125a9bc..c745eb5dc65 100644 --- a/test/core/util/one_corpus_entry_fuzzer.cc +++ b/test/core/util/one_corpus_entry_fuzzer.cc @@ -36,12 +36,10 @@ int main(int argc, char** argv) { /* TODO(yashkt) Calling grpc_init breaks tests. Fix the tests and replace * grpc_core::ExecCtx::GlobalInit with grpc_init and GlobalShutdown with * grpc_shutdown */ - grpc_core::ExecCtx::GlobalInit(); GPR_ASSERT( GRPC_LOG_IF_ERROR("load_file", grpc_load_file(argv[1], 0, &buffer))); LLVMFuzzerTestOneInput(GRPC_SLICE_START_PTR(buffer), GRPC_SLICE_LENGTH(buffer)); - grpc_core::ExecCtx::GlobalShutdown(); grpc_core::ExecCtx::GlobalInit(); grpc_slice_unref(buffer); grpc_core::ExecCtx::GlobalShutdown(); diff --git a/test/cpp/common/channel_arguments_test.cc b/test/cpp/common/channel_arguments_test.cc index d6ed2e5aa24..f330c01281e 100644 --- a/test/cpp/common/channel_arguments_test.cc +++ b/test/cpp/common/channel_arguments_test.cc @@ -249,5 +249,8 @@ TEST_F(ChannelArgumentsTest, SetUserAgentPrefix) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + grpc_init(); + int ret = RUN_ALL_TESTS(); + grpc_shutdown(); + return ret; } diff --git a/test/cpp/grpclb/grpclb_api_test.cc b/test/cpp/grpclb/grpclb_api_test.cc index 7b62080b497..a494d6f5195 100644 --- a/test/cpp/grpclb/grpclb_api_test.cc +++ b/test/cpp/grpclb/grpclb_api_test.cc @@ -17,6 +17,7 @@ */ #include +#include #include #include "src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h" @@ -135,5 +136,8 @@ TEST_F(GrpclbTest, ParseResponseServerList) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + grpc_init(); + int ret = RUN_ALL_TESTS(); + grpc_shutdown(); + return ret; } diff --git a/test/cpp/server/server_builder_test.cc b/test/cpp/server/server_builder_test.cc index d18459cec9d..694ce549c06 100644 --- a/test/cpp/server/server_builder_test.cc +++ b/test/cpp/server/server_builder_test.cc @@ -22,6 +22,8 @@ #include #include +#include + #include "src/proto/grpc/testing/echo.grpc.pb.h" #include "test/core/util/port.h" @@ -77,5 +79,8 @@ TEST(ServerBuilderTest, CreateServerRepeatedPortWithDisallowedReusePort) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + grpc_init(); + int ret = RUN_ALL_TESTS(); + grpc_shutdown(); + return ret; } diff --git a/test/cpp/util/byte_buffer_test.cc b/test/cpp/util/byte_buffer_test.cc index 8fb51bc6635..d603b289c88 100644 --- a/test/cpp/util/byte_buffer_test.cc +++ b/test/cpp/util/byte_buffer_test.cc @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -109,5 +110,8 @@ TEST_F(ByteBufferTest, SerializationMakesCopy) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + grpc_init(); + int ret = RUN_ALL_TESTS(); + grpc_shutdown(); + return ret; } diff --git a/test/cpp/util/slice_test.cc b/test/cpp/util/slice_test.cc index 8a8962d7ee0..c2e55f3374f 100644 --- a/test/cpp/util/slice_test.cc +++ b/test/cpp/util/slice_test.cc @@ -18,6 +18,7 @@ #include +#include #include #include @@ -127,5 +128,8 @@ TEST_F(SliceTest, Cslice) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + grpc_init(); + int ret = RUN_ALL_TESTS(); + grpc_shutdown(); + return ret; }