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

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
parent 5cef863e
...@@ -19,13 +19,19 @@ ...@@ -19,13 +19,19 @@
#include <thread> #include <thread>
#include <folly/Function.h> #include <folly/Function.h>
#include <folly/Range.h>
#include <folly/ThreadName.h>
#include <folly/io/async/EventBaseManager.h> #include <folly/io/async/EventBaseManager.h>
using namespace std; using namespace std;
namespace folly { 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); ebm->setEventBase(eb, false);
eb->loopForever(); eb->loopForever();
...@@ -36,12 +42,20 @@ static void run(EventBaseManager* ebm, EventBase* eb) { ...@@ -36,12 +42,20 @@ static void run(EventBaseManager* ebm, EventBase* eb) {
} }
ScopedEventBaseThread::ScopedEventBaseThread() ScopedEventBaseThread::ScopedEventBaseThread()
: ScopedEventBaseThread(nullptr) {} : ScopedEventBaseThread(nullptr, "") {}
ScopedEventBaseThread::ScopedEventBaseThread(const StringPiece& name)
: ScopedEventBaseThread(nullptr, name) {}
ScopedEventBaseThread::ScopedEventBaseThread(EventBaseManager* ebm) ScopedEventBaseThread::ScopedEventBaseThread(EventBaseManager* ebm)
: ScopedEventBaseThread(ebm, "") {}
ScopedEventBaseThread::ScopedEventBaseThread(
EventBaseManager* ebm,
const StringPiece& name)
: ebm_(ebm ? ebm : EventBaseManager::get()) { : ebm_(ebm ? ebm : EventBaseManager::get()) {
new (&eb_) EventBase(); new (&eb_) EventBase();
th_ = thread(run, ebm_, &eb_); th_ = thread(run, ebm_, &eb_, name);
eb_.waitUntilRunning(); eb_.waitUntilRunning();
} }
......
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
namespace folly { namespace folly {
class EventBaseManager; class EventBaseManager;
template <class Iter>
class Range;
typedef Range<const char*> StringPiece;
/** /**
* A helper class to start a new thread running a EventBase loop. * A helper class to start a new thread running a EventBase loop.
...@@ -35,7 +38,11 @@ class EventBaseManager; ...@@ -35,7 +38,11 @@ class EventBaseManager;
class ScopedEventBaseThread { class ScopedEventBaseThread {
public: public:
ScopedEventBaseThread(); ScopedEventBaseThread();
explicit ScopedEventBaseThread(const StringPiece& name);
explicit ScopedEventBaseThread(EventBaseManager* ebm); explicit ScopedEventBaseThread(EventBaseManager* ebm);
explicit ScopedEventBaseThread(
EventBaseManager* ebm,
const StringPiece& name);
~ScopedEventBaseThread(); ~ScopedEventBaseThread();
EventBase* getEventBase() const { EventBase* getEventBase() const {
......
...@@ -17,9 +17,11 @@ ...@@ -17,9 +17,11 @@
#include <folly/io/async/ScopedEventBaseThread.h> #include <folly/io/async/ScopedEventBaseThread.h>
#include <chrono> #include <chrono>
#include <string>
#include <folly/Baton.h> #include <folly/Baton.h>
#include <folly/Optional.h> #include <folly/Optional.h>
#include <folly/ThreadName.h>
#include <folly/io/async/EventBaseManager.h> #include <folly/io/async/EventBaseManager.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
...@@ -37,6 +39,24 @@ TEST_F(ScopedEventBaseThreadTest, example) { ...@@ -37,6 +39,24 @@ TEST_F(ScopedEventBaseThreadTest, example) {
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.timed_wait(seconds(1)));
} }
TEST_F(ScopedEventBaseThreadTest, named_example) {
static constexpr StringPiece kThreadName{"named_example"};
Optional<std::string> 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) { TEST_F(ScopedEventBaseThreadTest, default_manager) {
auto ebm = EventBaseManager::get(); auto ebm = EventBaseManager::get();
ScopedEventBaseThread sebt; ScopedEventBaseThread sebt;
......
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