GRPC Core  18.0.0
fork.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_GPRPP_FORK_H
20 #define GRPC_CORE_LIB_GPRPP_FORK_H
21 
23 
25 
26 /*
27  * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
28  * AROUND VERY SPECIFIC USE CASES.
29  */
30 
31 namespace grpc_core {
32 
33 namespace internal {
34 class ExecCtxState;
35 class ThreadState;
36 } // namespace internal
37 
38 class Fork {
39  public:
40  typedef void (*child_postfork_func)(void);
41 
42  static void GlobalInit();
43  static void GlobalShutdown();
44 
45  // Returns true if fork suppport is enabled, false otherwise
46  static bool Enabled();
47 
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))) {
52  DoIncExecCtxCount();
53  }
54  }
55 
56  // Decrement the count of active ExecCtxs
57  static void DecExecCtxCount() {
58  if (GPR_UNLIKELY(support_enabled_.Load(MemoryOrder::RELAXED))) {
59  DoDecExecCtxCount();
60  }
61  }
62 
63  // Provide a function that will be invoked in the child's postfork handler to
64  // reset the polling engine's internal state.
66  child_postfork_func reset_child_polling_engine);
68 
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
73  //
74  static bool BlockExecCtx();
75  static void AllowExecCtx();
76 
77  // Increment the count of active threads.
78  static void IncThreadCount();
79 
80  // Decrement the count of active threads.
81  static void DecThreadCount();
82 
83  // Await all core threads to be joined.
84  static void AwaitThreads();
85 
86  // Test only: overrides environment variables/compile flags
87  // Must be called before grpc_init()
88  static void Enable(bool enable);
89 
90  private:
91  static void DoIncExecCtxCount();
92  static void DoDecExecCtxCount();
93 
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_;
99 };
100 
101 } // namespace grpc_core
102 
103 #endif /* GRPC_CORE_LIB_GPRPP_FORK_H */
T Load(MemoryOrder order) const
Definition: atomic.h:44
Definition: fork.h:38
static void AllowExecCtx()
Definition: fork.cc:214
static child_postfork_func GetResetChildPollingEngineFunc()
Definition: fork.cc:203
static void DecExecCtxCount()
Definition: fork.h:57
static bool Enabled()
Definition: fork.cc:187
static void GlobalShutdown()
Definition: fork.cc:180
static void IncExecCtxCount()
Definition: fork.h:50
static void GlobalInit()
Definition: fork.cc:169
static void IncThreadCount()
Definition: fork.cc:220
static void SetResetChildPollingEngineFunc(child_postfork_func reset_child_polling_engine)
Definition: fork.cc:199
void(* child_postfork_func)(void)
Definition: fork.h:40
static bool BlockExecCtx()
Definition: fork.cc:207
static void AwaitThreads()
Definition: fork.cc:231
static void Enable(bool enable)
Definition: fork.cc:190
static void DecThreadCount()
Definition: fork.cc:226
Definition: fork.cc:60
Definition: fork.cc:121
#define GPR_UNLIKELY(x)
Definition: port_platform.h:660
Round Robin Policy.
Definition: backend_metric.cc:26