From: Christopher Dykes Date: Thu, 4 May 2017 23:46:53 +0000 (-0700) Subject: Support naming a ScopedEventBaseThread X-Git-Tag: v2017.05.08.00~9 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=cf05856cc2cf5e5d1e5717fc19ce3cbad2317ade Support naming a ScopedEventBaseThread Summary: The setThreadName API is in the process of being changed to not accept a thread id, which means the thread itself needs to set the name. There are times where a `ScopedEventBaseThread` needs to be named, and this makes that possible. Reviewed By: yfeldblum Differential Revision: D4916781 fbshipit-source-id: dab05b520a715183ce069151ed16864fa1331abc --- diff --git a/folly/io/async/ScopedEventBaseThread.cpp b/folly/io/async/ScopedEventBaseThread.cpp index 25f0cba7..9f14b918 100644 --- a/folly/io/async/ScopedEventBaseThread.cpp +++ b/folly/io/async/ScopedEventBaseThread.cpp @@ -19,13 +19,19 @@ #include #include +#include +#include #include using namespace std; namespace folly { -static void run(EventBaseManager* ebm, EventBase* eb) { +static void run(EventBaseManager* ebm, EventBase* eb, const StringPiece& name) { + if (name.size()) { + folly::setThreadName(name); + } + ebm->setEventBase(eb, false); eb->loopForever(); @@ -36,12 +42,20 @@ static void run(EventBaseManager* ebm, EventBase* eb) { } ScopedEventBaseThread::ScopedEventBaseThread() - : ScopedEventBaseThread(nullptr) {} + : ScopedEventBaseThread(nullptr, "") {} + +ScopedEventBaseThread::ScopedEventBaseThread(const StringPiece& name) + : ScopedEventBaseThread(nullptr, name) {} ScopedEventBaseThread::ScopedEventBaseThread(EventBaseManager* ebm) + : ScopedEventBaseThread(ebm, "") {} + +ScopedEventBaseThread::ScopedEventBaseThread( + EventBaseManager* ebm, + const StringPiece& name) : ebm_(ebm ? ebm : EventBaseManager::get()) { new (&eb_) EventBase(); - th_ = thread(run, ebm_, &eb_); + th_ = thread(run, ebm_, &eb_, name); eb_.waitUntilRunning(); } diff --git a/folly/io/async/ScopedEventBaseThread.h b/folly/io/async/ScopedEventBaseThread.h index 52824cd8..0a8775c6 100644 --- a/folly/io/async/ScopedEventBaseThread.h +++ b/folly/io/async/ScopedEventBaseThread.h @@ -24,6 +24,9 @@ namespace folly { class EventBaseManager; +template +class Range; +typedef Range StringPiece; /** * A helper class to start a new thread running a EventBase loop. @@ -35,7 +38,11 @@ class EventBaseManager; class ScopedEventBaseThread { public: ScopedEventBaseThread(); + explicit ScopedEventBaseThread(const StringPiece& name); explicit ScopedEventBaseThread(EventBaseManager* ebm); + explicit ScopedEventBaseThread( + EventBaseManager* ebm, + const StringPiece& name); ~ScopedEventBaseThread(); EventBase* getEventBase() const { diff --git a/folly/io/async/test/ScopedEventBaseThreadTest.cpp b/folly/io/async/test/ScopedEventBaseThreadTest.cpp index 02fd24df..79dbe6ff 100644 --- a/folly/io/async/test/ScopedEventBaseThreadTest.cpp +++ b/folly/io/async/test/ScopedEventBaseThreadTest.cpp @@ -17,9 +17,11 @@ #include #include +#include #include #include +#include #include #include @@ -37,6 +39,24 @@ TEST_F(ScopedEventBaseThreadTest, example) { ASSERT_TRUE(done.timed_wait(seconds(1))); } +TEST_F(ScopedEventBaseThreadTest, named_example) { + static constexpr StringPiece kThreadName{"named_example"}; + + Optional createdThreadName; + Baton<> done; + + ScopedEventBaseThread sebt{kThreadName}; + sebt.getEventBase()->runInEventBaseThread([&] { + createdThreadName = folly::getCurrentThreadName(); + done.post(); + }); + + ASSERT_TRUE(done.timed_wait(seconds(1))); + if (createdThreadName) { + ASSERT_EQ(kThreadName.toString(), createdThreadName.value()); + } +} + TEST_F(ScopedEventBaseThreadTest, default_manager) { auto ebm = EventBaseManager::get(); ScopedEventBaseThread sebt;