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 #ifndef GRPC_CORE_LIB_GPRPP_FORK_H
20 #define GRPC_CORE_LIB_GPRPP_FORK_H
22 #include <grpc/support/port_platform.h>
24 #include "src/core/lib/gprpp/atomic.h"
27 * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
28 * AROUND VERY SPECIFIC USE CASES.
36 } // namespace internal
40 typedef void (*child_postfork_func)(void);
42 static void GlobalInit();
43 static void GlobalShutdown();
45 // Returns true if fork suppport is enabled, false otherwise
46 static bool Enabled();
48 // Increment the count of active ExecCtxs.
49 // Will block until a pending fork is complete if one is in progress.
50 static void IncExecCtxCount() {
51 if (GPR_UNLIKELY(support_enabled_.Load(MemoryOrder::RELAXED))) {
56 // Decrement the count of active ExecCtxs
57 static void DecExecCtxCount() {
58 if (GPR_UNLIKELY(support_enabled_.Load(MemoryOrder::RELAXED))) {
63 // Provide a function that will be invoked in the child's postfork handler to
64 // reset the polling engine's internal state.
65 static void SetResetChildPollingEngineFunc(
66 child_postfork_func reset_child_polling_engine);
67 static child_postfork_func GetResetChildPollingEngineFunc();
69 // Check if there is a single active ExecCtx
70 // (the one used to invoke this function). If there are more,
71 // return false. Otherwise, return true and block creation of
72 // more ExecCtx s until AlloWExecCtx() is called
74 static bool BlockExecCtx();
75 static void AllowExecCtx();
77 // Increment the count of active threads.
78 static void IncThreadCount();
80 // Decrement the count of active threads.
81 static void DecThreadCount();
83 // Await all core threads to be joined.
84 static void AwaitThreads();
86 // Test only: overrides environment variables/compile flags
87 // Must be called before grpc_init()
88 static void Enable(bool enable);
91 static void DoIncExecCtxCount();
92 static void DoDecExecCtxCount();
94 static internal::ExecCtxState* exec_ctx_state_;
95 static internal::ThreadState* thread_state_;
96 static grpc_core::Atomic<bool> support_enabled_;
97 static bool override_enabled_;
98 static child_postfork_func reset_child_polling_engine_;
101 } // namespace grpc_core
103 #endif /* GRPC_CORE_LIB_GPRPP_FORK_H */