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;
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) {
start();
}
}
ScopedEventBaseThread::ScopedEventBaseThread(
EventBaseManager* ebm) :
ScopedEventBaseThread(true, ebm) {}
ScopedEventBaseThread::~ScopedEventBaseThread() {
stop();
}
......@@ -44,7 +61,7 @@ void ScopedEventBaseThread::start() {
return;
}
eventBase_ = make_unique<EventBase>();
thread_ = make_unique<thread>(&EventBase::loopForever, &*eventBase_);
thread_ = make_unique<thread>(run, ebm_, eventBase_.get());
eventBase_->waitUntilRunning();
}
......
......@@ -19,11 +19,12 @@
#include <memory>
#include <thread>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/EventBaseManager.h>
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.
* When the ScopedEventBaseThread object is destroyed, the thread will be
......@@ -31,14 +32,18 @@ namespace folly {
*/
class ScopedEventBaseThread {
public:
explicit ScopedEventBaseThread(bool autostart = true);
explicit ScopedEventBaseThread(
bool autostart = true,
EventBaseManager* ebm = nullptr);
explicit ScopedEventBaseThread(
EventBaseManager* ebm);
~ScopedEventBaseThread();
ScopedEventBaseThread(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 {
return eventBase_.get();
......@@ -52,6 +57,7 @@ class ScopedEventBaseThread {
ScopedEventBaseThread(const ScopedEventBaseThread& other) = delete;
ScopedEventBaseThread& operator=(const ScopedEventBaseThread& other) = delete;
EventBaseManager* ebm_;
std::unique_ptr<EventBase> eventBase_;
std::unique_ptr<std::thread> thread_;
};
......
......@@ -77,3 +77,14 @@ TEST_F(ScopedEventBaseThreadTest, self_move) {
sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
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