From 18046154bb4e4d10ceed8e1a407eb9cfaab5f4b8 Mon Sep 17 00:00:00 2001 From: Alan Frindell Date: Fri, 7 Jul 2017 21:06:02 -0700 Subject: [PATCH] Option to set thread name for EventBaseThread Summary: as in title Reviewed By: yfeldblum Differential Revision: D5382995 fbshipit-source-id: 87534e6092f167399943287182ec61ae21e608c6 --- folly/io/async/EventBaseThread.cpp | 11 +++++++---- folly/io/async/EventBaseThread.h | 8 ++++++-- folly/io/async/test/EventBaseThreadTest.cpp | 8 ++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/folly/io/async/EventBaseThread.cpp b/folly/io/async/EventBaseThread.cpp index 4b74ed03..fb529eed 100644 --- a/folly/io/async/EventBaseThread.cpp +++ b/folly/io/async/EventBaseThread.cpp @@ -23,10 +23,13 @@ namespace folly { EventBaseThread::EventBaseThread() : EventBaseThread(true) {} -EventBaseThread::EventBaseThread(bool autostart, EventBaseManager* ebm) +EventBaseThread::EventBaseThread( + bool autostart, + EventBaseManager* ebm, + folly::StringPiece threadName) : ebm_(ebm) { if (autostart) { - start(); + start(threadName); } } @@ -47,11 +50,11 @@ bool EventBaseThread::running() const { return !!th_; } -void EventBaseThread::start() { +void EventBaseThread::start(folly::StringPiece threadName) { if (th_) { return; } - th_ = std::make_unique(ebm_); + th_ = std::make_unique(ebm_, threadName); } void EventBaseThread::stop() { diff --git a/folly/io/async/EventBaseThread.h b/folly/io/async/EventBaseThread.h index cff1f9e7..41940d68 100644 --- a/folly/io/async/EventBaseThread.h +++ b/folly/io/async/EventBaseThread.h @@ -16,6 +16,7 @@ #pragma once +#include #include namespace folly { @@ -27,7 +28,10 @@ class ScopedEventBaseThread; class EventBaseThread { public: EventBaseThread(); - explicit EventBaseThread(bool autostart, EventBaseManager* ebm = nullptr); + explicit EventBaseThread( + bool autostart, + EventBaseManager* ebm = nullptr, + folly::StringPiece threadName = folly::StringPiece()); explicit EventBaseThread(EventBaseManager* ebm); ~EventBaseThread(); @@ -37,7 +41,7 @@ class EventBaseThread { EventBase* getEventBase() const; bool running() const; - void start(); + void start(folly::StringPiece threadName = folly::StringPiece()); void stop(); private: diff --git a/folly/io/async/test/EventBaseThreadTest.cpp b/folly/io/async/test/EventBaseThreadTest.cpp index 7cedfd8e..96939e33 100644 --- a/folly/io/async/test/EventBaseThreadTest.cpp +++ b/folly/io/async/test/EventBaseThreadTest.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -29,10 +30,13 @@ using namespace folly; class EventBaseThreadTest : public testing::Test {}; TEST_F(EventBaseThreadTest, example) { - EventBaseThread ebt; + EventBaseThread ebt(true, nullptr, "monkey"); Baton<> done; - ebt.getEventBase()->runInEventBaseThread([&] { done.post(); }); + ebt.getEventBase()->runInEventBaseThread([&] { + EXPECT_EQ(getCurrentThreadName().value(), "monkey"); + done.post(); + }); ASSERT_TRUE(done.timed_wait(seconds(1))); } -- 2.34.1