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_SRC_CORE_UTIL_FORK_H 20 #define GRPC_SRC_CORE_UTIL_FORK_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <atomic> 25 #include <set> 26 27 // 28 // NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK 29 // AROUND VERY SPECIFIC USE CASES. 30 // 31 32 namespace grpc_core { 33 34 class GPR_DLL Fork { 35 public: 36 typedef void (*child_postfork_func)(void); 37 38 static void GlobalInit(); 39 40 // Returns true if fork support is enabled, false otherwise 41 static bool Enabled(); 42 43 // Increment the count of active ExecCtxs. 44 // Will block until a pending fork is complete if one is in progress. IncExecCtxCount()45 static void IncExecCtxCount() { 46 if (GPR_UNLIKELY(support_enabled_.load(std::memory_order_relaxed))) { 47 DoIncExecCtxCount(); 48 } 49 } 50 51 // Decrement the count of active ExecCtxs DecExecCtxCount()52 static void DecExecCtxCount() { 53 if (GPR_UNLIKELY(support_enabled_.load(std::memory_order_relaxed))) { 54 DoDecExecCtxCount(); 55 } 56 } 57 58 // Provide a function that will be invoked in the child's postfork handler to 59 // reset the polling engine's internal state. 60 // Returns true if reset_child_polling_engine was not previously registered, 61 // otherwise returns false and does nothing. 62 static bool RegisterResetChildPollingEngineFunc( 63 child_postfork_func reset_child_polling_engine); 64 static const std::set<child_postfork_func>& GetResetChildPollingEngineFunc(); 65 66 // Check if there is a single active ExecCtx 67 // (the one used to invoke this function). If there are more, 68 // return false. Otherwise, return true and block creation of 69 // more ExecCtx s until AllowExecCtx() is called 70 // 71 static bool BlockExecCtx(); 72 static void AllowExecCtx(); 73 74 // Increment the count of active threads. 75 static void IncThreadCount(); 76 77 // Decrement the count of active threads. 78 static void DecThreadCount(); 79 80 // Await all core threads to be joined. 81 static void AwaitThreads(); 82 83 // Test only: overrides environment variables/compile flags 84 // Must be called before grpc_init() 85 static void Enable(bool enable); 86 87 private: 88 static void DoIncExecCtxCount(); 89 static void DoDecExecCtxCount(); 90 91 static std::atomic<bool> support_enabled_; 92 static bool override_enabled_; 93 static std::set<child_postfork_func>* reset_child_polling_engine_; 94 }; 95 96 } // namespace grpc_core 97 98 #endif // GRPC_SRC_CORE_UTIL_FORK_H 99