Debian Codegraph @

src/core/lib/gprpp/fork.h

raw
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 <atomic>
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 class Fork {
34 public:
35 typedef void (*child_postfork_func)(void);
36
37 static void GlobalInit();
38
39 // Returns true if fork suppport is enabled, false otherwise
40 static bool Enabled();
41
42 // Increment the count of active ExecCtxs.
43 // Will block until a pending fork is complete if one is in progress.
44 static void IncExecCtxCount() {
45 if (GPR_UNLIKELY(support_enabled_.load(std::memory_order_relaxed))) {
46 DoIncExecCtxCount();
47 }
48 }
49
50 // Decrement the count of active ExecCtxs
51 static void DecExecCtxCount() {
52 if (GPR_UNLIKELY(support_enabled_.load(std::memory_order_relaxed))) {
53 DoDecExecCtxCount();
54 }
55 }
56
57 // Provide a function that will be invoked in the child's postfork handler to
58 // reset the polling engine's internal state.
59 static void SetResetChildPollingEngineFunc(
60 child_postfork_func reset_child_polling_engine);
61 static child_postfork_func GetResetChildPollingEngineFunc();
62
63 // Check if there is a single active ExecCtx
64 // (the one used to invoke this function). If there are more,
65 // return false. Otherwise, return true and block creation of
66 // more ExecCtx s until AlloWExecCtx() is called
67 //
68 static bool BlockExecCtx();
69 static void AllowExecCtx();
70
71 // Increment the count of active threads.
72 static void IncThreadCount();
73
74 // Decrement the count of active threads.
75 static void DecThreadCount();
76
77 // Await all core threads to be joined.
78 static void AwaitThreads();
79
80 // Test only: overrides environment variables/compile flags
81 // Must be called before grpc_init()
82 static void Enable(bool enable);
83
84 private:
85 static void DoIncExecCtxCount();
86 static void DoDecExecCtxCount();
87
88 static std::atomic<bool> support_enabled_;
89 static bool override_enabled_;
90 static child_postfork_func reset_child_polling_engine_;
91 };
92
93 } // namespace grpc_core
94
95 #endif /* GRPC_CORE_LIB_GPRPP_FORK_H */