Commit 01f97980 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

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
parent f57ddfc7
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include <condition_variable> #include <condition_variable>
#include <fcntl.h> #include <fcntl.h>
#include <mutex> #include <mutex>
#include <pthread.h>
namespace folly { namespace folly {
...@@ -271,7 +270,7 @@ bool EventBase::loopBody(int flags) { ...@@ -271,7 +270,7 @@ bool EventBase::loopBody(int flags) {
std::chrono::microseconds busy; std::chrono::microseconds busy;
std::chrono::microseconds idle; 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()) { if (!name_.empty()) {
setThreadName(name_); setThreadName(name_);
......
...@@ -466,7 +466,7 @@ class EventBase : private boost::noncopyable, ...@@ -466,7 +466,7 @@ class EventBase : private boost::noncopyable,
* check if the event base loop is running. * check if the event base loop is running.
*/ */
bool isRunning() const { 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, ...@@ -484,12 +484,12 @@ class EventBase : private boost::noncopyable,
*/ */
bool isInEventBaseThread() const { bool isInEventBaseThread() const {
auto tid = loopThread_.load(std::memory_order_relaxed); 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 { bool inRunningEventBaseThread() const {
return pthread_equal( return loopThread_.load(std::memory_order_relaxed) ==
loopThread_.load(std::memory_order_relaxed), pthread_self()); std::this_thread::get_id();
} }
HHWheelTimer& timer() { HHWheelTimer& timer() {
...@@ -685,11 +685,8 @@ class EventBase : private boost::noncopyable, ...@@ -685,11 +685,8 @@ class EventBase : private boost::noncopyable,
std::atomic<bool> stop_; std::atomic<bool> stop_;
// The ID of the thread running the main loop. // The ID of the thread running the main loop.
// 0 if loop is not running. // std::thread::id{} if loop is not running.
// Note: POSIX doesn't guarantee that 0 is an invalid pthread_t (or std::atomic<std::thread::id> loopThread_;
// even that atomic<pthread_t> is valid), but that's how it is
// everywhere (at least on Linux, FreeBSD, and OSX).
std::atomic<pthread_t> loopThread_;
// pointer to underlying event_base class doing the heavy lifting // pointer to underlying event_base class doing the heavy lifting
event_base* evb_; event_base* evb_;
......
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