Option to set thread name for EventBaseThread
authorAlan Frindell <afrind@fb.com>
Sat, 8 Jul 2017 04:06:02 +0000 (21:06 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 8 Jul 2017 04:12:39 +0000 (21:12 -0700)
Summary: as in title

Reviewed By: yfeldblum

Differential Revision: D5382995

fbshipit-source-id: 87534e6092f167399943287182ec61ae21e608c6

folly/io/async/EventBaseThread.cpp
folly/io/async/EventBaseThread.h
folly/io/async/test/EventBaseThreadTest.cpp

index 4b74ed0334d1ff69c957152714b87412c3070622..fb529eed9195115d886d122caf39a79d5b8a6de0 100644 (file)
@@ -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() {
index cff1f9e7da2c544f5aa43a9a7dddabda0fcec6fb..41940d68e7e7d85a270b59a219b04c2c5a07bfb2 100644 (file)
@@ -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:
index 7cedfd8e913a278beb45f4a5478ba691e6afa235..96939e3396e63cbb4044fc305170e8b09235d5d0 100644 (file)
@@ -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)));
 }