Commit c5992522 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by facebook-github-bot-9

ScopedEventBaseThread should support an EventBaseManager context.

Summary: [Folly] ScopedEventBaseThread should support an EventBaseManager context.

Reviewed By: @Gownta

Differential Revision: D2314049
parent d807e4c1
...@@ -23,12 +23,29 @@ using namespace std; ...@@ -23,12 +23,29 @@ using namespace std;
namespace folly { namespace folly {
ScopedEventBaseThread::ScopedEventBaseThread(bool autostart) { static void run(EventBaseManager* ebm, EventBase* eb) {
if (ebm) {
ebm->setEventBase(eb, false);
}
CHECK_NOTNULL(eb)->loopForever();
if (ebm) {
ebm->clearEventBase();
}
}
ScopedEventBaseThread::ScopedEventBaseThread(
bool autostart,
EventBaseManager* ebm) :
ebm_(ebm) {
if (autostart) { if (autostart) {
start(); start();
} }
} }
ScopedEventBaseThread::ScopedEventBaseThread(
EventBaseManager* ebm) :
ScopedEventBaseThread(true, ebm) {}
ScopedEventBaseThread::~ScopedEventBaseThread() { ScopedEventBaseThread::~ScopedEventBaseThread() {
stop(); stop();
} }
...@@ -44,7 +61,7 @@ void ScopedEventBaseThread::start() { ...@@ -44,7 +61,7 @@ void ScopedEventBaseThread::start() {
return; return;
} }
eventBase_ = make_unique<EventBase>(); eventBase_ = make_unique<EventBase>();
thread_ = make_unique<thread>(&EventBase::loopForever, &*eventBase_); thread_ = make_unique<thread>(run, ebm_, eventBase_.get());
eventBase_->waitUntilRunning(); eventBase_->waitUntilRunning();
} }
......
...@@ -19,11 +19,12 @@ ...@@ -19,11 +19,12 @@
#include <memory> #include <memory>
#include <thread> #include <thread>
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#include <folly/io/async/EventBaseManager.h>
namespace folly { namespace folly {
/** /**
* A helper class to start a new thread running a TEventBase loop. * A helper class to start a new thread running a EventBase loop.
* *
* The new thread will be started by the ScopedEventBaseThread constructor. * The new thread will be started by the ScopedEventBaseThread constructor.
* When the ScopedEventBaseThread object is destroyed, the thread will be * When the ScopedEventBaseThread object is destroyed, the thread will be
...@@ -31,14 +32,18 @@ namespace folly { ...@@ -31,14 +32,18 @@ namespace folly {
*/ */
class ScopedEventBaseThread { class ScopedEventBaseThread {
public: public:
explicit ScopedEventBaseThread(bool autostart = true); explicit ScopedEventBaseThread(
bool autostart = true,
EventBaseManager* ebm = nullptr);
explicit ScopedEventBaseThread(
EventBaseManager* ebm);
~ScopedEventBaseThread(); ~ScopedEventBaseThread();
ScopedEventBaseThread(ScopedEventBaseThread&& other) noexcept; ScopedEventBaseThread(ScopedEventBaseThread&& other) noexcept;
ScopedEventBaseThread &operator=(ScopedEventBaseThread&& other) noexcept; ScopedEventBaseThread &operator=(ScopedEventBaseThread&& other) noexcept;
/** /**
* Get a pointer to the TEventBase driving this thread. * Get a pointer to the EventBase driving this thread.
*/ */
EventBase* getEventBase() const { EventBase* getEventBase() const {
return eventBase_.get(); return eventBase_.get();
...@@ -52,6 +57,7 @@ class ScopedEventBaseThread { ...@@ -52,6 +57,7 @@ class ScopedEventBaseThread {
ScopedEventBaseThread(const ScopedEventBaseThread& other) = delete; ScopedEventBaseThread(const ScopedEventBaseThread& other) = delete;
ScopedEventBaseThread& operator=(const ScopedEventBaseThread& other) = delete; ScopedEventBaseThread& operator=(const ScopedEventBaseThread& other) = delete;
EventBaseManager* ebm_;
std::unique_ptr<EventBase> eventBase_; std::unique_ptr<EventBase> eventBase_;
std::unique_ptr<std::thread> thread_; std::unique_ptr<std::thread> thread_;
}; };
......
...@@ -77,3 +77,14 @@ TEST_F(ScopedEventBaseThreadTest, self_move) { ...@@ -77,3 +77,14 @@ TEST_F(ScopedEventBaseThreadTest, self_move) {
sebt.getEventBase()->runInEventBaseThread([&] { done.post(); }); sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
done.timed_wait(steady_clock::now() + milliseconds(100)); done.timed_wait(steady_clock::now() + milliseconds(100));
} }
TEST_F(ScopedEventBaseThreadTest, manager) {
EventBaseManager ebm;
ScopedEventBaseThread sebt(&ebm);
auto sebt_eb = sebt.getEventBase();
auto ebm_eb = (EventBase*)nullptr;
sebt_eb->runInEventBaseThreadAndWait([&] {
ebm_eb = ebm.getEventBase();
});
EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
}
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