• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 /*
23  * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
24  *       AROUND VERY SPECIFIC USE CASES.
25  */
26 
27 namespace grpc_core {
28 
29 namespace internal {
30 class ExecCtxState;
31 class ThreadState;
32 }  // namespace internal
33 
34 class Fork {
35  public:
36   typedef void (*child_postfork_func)(void);
37 
38   static void GlobalInit();
39   static void GlobalShutdown();
40 
41   // Returns true if fork suppport is enabled, false otherwise
42   static bool Enabled();
43 
44   // Increment the count of active ExecCtxs.
45   // Will block until a pending fork is complete if one is in progress.
46   static void IncExecCtxCount();
47 
48   // Decrement the count of active ExecCtxs
49   static void DecExecCtxCount();
50 
51   // Provide a function that will be invoked in the child's postfork handler to
52   // reset the polling engine's internal state.
53   static void SetResetChildPollingEngineFunc(
54       child_postfork_func reset_child_polling_engine);
55   static child_postfork_func GetResetChildPollingEngineFunc();
56 
57   // Check if there is a single active ExecCtx
58   // (the one used to invoke this function).  If there are more,
59   // return false.  Otherwise, return true and block creation of
60   // more ExecCtx s until AlloWExecCtx() is called
61   //
62   static bool BlockExecCtx();
63   static void AllowExecCtx();
64 
65   // Increment the count of active threads.
66   static void IncThreadCount();
67 
68   // Decrement the count of active threads.
69   static void DecThreadCount();
70 
71   // Await all core threads to be joined.
72   static void AwaitThreads();
73 
74   // Test only: overrides environment variables/compile flags
75   // Must be called before grpc_init()
76   static void Enable(bool enable);
77 
78  private:
79   static internal::ExecCtxState* exec_ctx_state_;
80   static internal::ThreadState* thread_state_;
81   static bool support_enabled_;
82   static bool override_enabled_;
83   static child_postfork_func reset_child_polling_engine_;
84 };
85 
86 }  // namespace grpc_core
87 
88 #endif /* GRPC_CORE_LIB_GPRPP_FORK_H */
89