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 {
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<ScopedEventBaseThread>(ebm_);
th_ = std::make_unique<ScopedEventBaseThread>(ebm_, threadName);
}
void EventBaseThread::stop() {
......
......@@ -16,6 +16,7 @@
#pragma once
#include <folly/Range.h>
#include <memory>
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:
......
......@@ -19,6 +19,7 @@
#include <chrono>
#include <folly/Baton.h>
#include <folly/ThreadName.h>
#include <folly/io/async/EventBaseManager.h>
#include <folly/portability/GTest.h>
......@@ -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)));
}
......
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