Commit 18046154 authored by Alan Frindell's avatar Alan Frindell Committed by Facebook Github Bot

Option to set thread name for EventBaseThread

Summary: as in title

Reviewed By: yfeldblum

Differential Revision: D5382995

fbshipit-source-id: 87534e6092f167399943287182ec61ae21e608c6
parent 81c9d2a6
...@@ -23,10 +23,13 @@ namespace folly { ...@@ -23,10 +23,13 @@ namespace folly {
EventBaseThread::EventBaseThread() : EventBaseThread(true) {} EventBaseThread::EventBaseThread() : EventBaseThread(true) {}
EventBaseThread::EventBaseThread(bool autostart, EventBaseManager* ebm) EventBaseThread::EventBaseThread(
bool autostart,
EventBaseManager* ebm,
folly::StringPiece threadName)
: ebm_(ebm) { : ebm_(ebm) {
if (autostart) { if (autostart) {
start(); start(threadName);
} }
} }
...@@ -47,11 +50,11 @@ bool EventBaseThread::running() const { ...@@ -47,11 +50,11 @@ bool EventBaseThread::running() const {
return !!th_; return !!th_;
} }
void EventBaseThread::start() { void EventBaseThread::start(folly::StringPiece threadName) {
if (th_) { if (th_) {
return; return;
} }
th_ = std::make_unique<ScopedEventBaseThread>(ebm_); th_ = std::make_unique<ScopedEventBaseThread>(ebm_, threadName);
} }
void EventBaseThread::stop() { void EventBaseThread::stop() {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#pragma once #pragma once
#include <folly/Range.h>
#include <memory> #include <memory>
namespace folly { namespace folly {
...@@ -27,7 +28,10 @@ class ScopedEventBaseThread; ...@@ -27,7 +28,10 @@ class ScopedEventBaseThread;
class EventBaseThread { class EventBaseThread {
public: public:
EventBaseThread(); EventBaseThread();
explicit EventBaseThread(bool autostart, EventBaseManager* ebm = nullptr); explicit EventBaseThread(
bool autostart,
EventBaseManager* ebm = nullptr,
folly::StringPiece threadName = folly::StringPiece());
explicit EventBaseThread(EventBaseManager* ebm); explicit EventBaseThread(EventBaseManager* ebm);
~EventBaseThread(); ~EventBaseThread();
...@@ -37,7 +41,7 @@ class EventBaseThread { ...@@ -37,7 +41,7 @@ class EventBaseThread {
EventBase* getEventBase() const; EventBase* getEventBase() const;
bool running() const; bool running() const;
void start(); void start(folly::StringPiece threadName = folly::StringPiece());
void stop(); void stop();
private: private:
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <chrono> #include <chrono>
#include <folly/Baton.h> #include <folly/Baton.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>
...@@ -29,10 +30,13 @@ using namespace folly; ...@@ -29,10 +30,13 @@ using namespace folly;
class EventBaseThreadTest : public testing::Test {}; class EventBaseThreadTest : public testing::Test {};
TEST_F(EventBaseThreadTest, example) { TEST_F(EventBaseThreadTest, example) {
EventBaseThread ebt; EventBaseThread ebt(true, nullptr, "monkey");
Baton<> done; Baton<> done;
ebt.getEventBase()->runInEventBaseThread([&] { done.post(); }); ebt.getEventBase()->runInEventBaseThread([&] {
EXPECT_EQ(getCurrentThreadName().value(), "monkey");
done.post();
});
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.timed_wait(seconds(1)));
} }
......
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