Commit 29ba83e5 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

assume thread_local in AtFork

Reviewed By: Orvid

Differential Revision: D27671208

fbshipit-source-id: 44710468544eebbb8f4ed0ecd5621bce0b56d248
parent 6ed9d378
...@@ -30,38 +30,16 @@ namespace detail { ...@@ -30,38 +30,16 @@ namespace detail {
namespace { namespace {
#if !FOLLY_MOBILE && !defined(__APPLE__) && !defined(_MSC_VER) struct SkipAtForkHandlers {
#define FOLLY_ATFORK_USE_FOLLY_TLS 1 static thread_local bool value;
#else
#define FOLLY_ATFORK_USE_FOLLY_TLS 0 struct Guard {
#endif bool saved = value;
Guard() { value = true; }
template <bool enabled = FOLLY_ATFORK_USE_FOLLY_TLS> ~Guard() { value = saved; }
struct SkipAtForkHandlers; };
template <>
struct SkipAtForkHandlers<false> {
static bool available() { return false; }
static bool get() { return false; }
[[noreturn]] static void set(bool) { std::terminate(); }
}; };
thread_local bool SkipAtForkHandlers::value;
#if FOLLY_ATFORK_USE_FOLLY_TLS
template <>
struct SkipAtForkHandlers<true> {
static bool available() { return true; }
static bool get() { return value_; }
static void set(bool value) { value_ = value; }
private:
static thread_local bool value_;
};
thread_local bool SkipAtForkHandlers<true>::value_{false};
#endif
struct AtForkTask { struct AtForkTask {
void const* handle; void const* handle;
...@@ -78,7 +56,7 @@ class AtForkList { ...@@ -78,7 +56,7 @@ class AtForkList {
} }
static void prepare() noexcept { static void prepare() noexcept {
if (SkipAtForkHandlers<>::get()) { if (SkipAtForkHandlers::value) {
return; return;
} }
instance().tasksLock.lock(); instance().tasksLock.lock();
...@@ -100,7 +78,7 @@ class AtForkList { ...@@ -100,7 +78,7 @@ class AtForkList {
} }
static void parent() noexcept { static void parent() noexcept {
if (SkipAtForkHandlers<>::get()) { if (SkipAtForkHandlers::value) {
return; return;
} }
auto& tasks = instance().tasks; auto& tasks = instance().tasks;
...@@ -111,7 +89,7 @@ class AtForkList { ...@@ -111,7 +89,7 @@ class AtForkList {
} }
static void child() noexcept { static void child() noexcept {
if (SkipAtForkHandlers<>::get()) { if (SkipAtForkHandlers::value) {
return; return;
} }
// if we fork a multithreaded process // if we fork a multithreaded process
...@@ -181,20 +159,17 @@ void AtFork::unregisterHandler(void const* handle) { ...@@ -181,20 +159,17 @@ void AtFork::unregisterHandler(void const* handle) {
} }
pid_t AtFork::forkInstrumented(fork_t forkFn) { pid_t AtFork::forkInstrumented(fork_t forkFn) {
if (SkipAtForkHandlers<>::available()) { AtForkList::prepare();
AtForkList::prepare(); auto ret = [&] {
SkipAtForkHandlers<>::set(true); SkipAtForkHandlers::Guard guard;
auto ret = forkFn();
SkipAtForkHandlers<>::set(false);
if (ret) {
AtForkList::parent();
} else {
AtForkList::child();
}
return ret;
} else {
return forkFn(); return forkFn();
}();
if (ret) {
AtForkList::parent();
} else {
AtForkList::child();
} }
return ret;
} }
} // namespace detail } // namespace detail
} // namespace folly } // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment