Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / gprpp / fork.h
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 #include <grpc/support/port_platform.h>
23
24 #include "src/core/lib/gprpp/atomic.h"
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.
65   static void SetResetChildPollingEngineFunc(
66       child_postfork_func reset_child_polling_engine);
67   static child_postfork_func GetResetChildPollingEngineFunc();
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 */