From 01f97980d6455d6b28c7f8286f394bf3c533bc8a Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Sun, 15 Jan 2017 17:46:24 -0800 Subject: [PATCH] Don't use Pthread in EventBase Summary: Pthread is currently a dependency of Folly that is not really necessary on Windows, or even with standard C++ for the most part, so start work on killing it in Folly. This switches EventBase to using `std::thread::id`'s instead, which also means we aren't reliant on the implementation detail that thread id 0 is invalid. Well, we are, but it's now the standard library's fault not ours. Reviewed By: yfeldblum Differential Revision: D4418128 fbshipit-source-id: a9c95ac6c7305c960156a4ad684b6db89b5856d9 --- folly/io/async/EventBase.cpp | 3 +-- folly/io/async/EventBase.h | 15 ++++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp index febf227e..31cec0bf 100644 --- a/folly/io/async/EventBase.cpp +++ b/folly/io/async/EventBase.cpp @@ -27,7 +27,6 @@ #include #include #include -#include namespace folly { @@ -271,7 +270,7 @@ bool EventBase::loopBody(int flags) { std::chrono::microseconds busy; std::chrono::microseconds idle; - loopThread_.store(pthread_self(), std::memory_order_release); + loopThread_.store(std::this_thread::get_id(), std::memory_order_release); if (!name_.empty()) { setThreadName(name_); diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index 955fab02..8bfb684d 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -466,7 +466,7 @@ class EventBase : private boost::noncopyable, * check if the event base loop is running. */ bool isRunning() const { - return loopThread_.load(std::memory_order_relaxed) != 0; + return loopThread_.load(std::memory_order_relaxed) != std::thread::id(); } /** @@ -484,12 +484,12 @@ class EventBase : private boost::noncopyable, */ bool isInEventBaseThread() const { auto tid = loopThread_.load(std::memory_order_relaxed); - return tid == 0 || pthread_equal(tid, pthread_self()); + return tid == std::thread::id() || tid == std::this_thread::get_id(); } bool inRunningEventBaseThread() const { - return pthread_equal( - loopThread_.load(std::memory_order_relaxed), pthread_self()); + return loopThread_.load(std::memory_order_relaxed) == + std::this_thread::get_id(); } HHWheelTimer& timer() { @@ -685,11 +685,8 @@ class EventBase : private boost::noncopyable, std::atomic stop_; // The ID of the thread running the main loop. - // 0 if loop is not running. - // Note: POSIX doesn't guarantee that 0 is an invalid pthread_t (or - // even that atomic is valid), but that's how it is - // everywhere (at least on Linux, FreeBSD, and OSX). - std::atomic loopThread_; + // std::thread::id{} if loop is not running. + std::atomic loopThread_; // pointer to underlying event_base class doing the heavy lifting event_base* evb_; -- 2.34.1