Summary: [Folly] ScopedEventBaseThread should support an EventBaseManager context.
Reviewed By: @Gownta
Differential Revision:
D2314049
-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();
}
}
if (autostart) {
start();
}
}
+ScopedEventBaseThread::ScopedEventBaseThread(
+ EventBaseManager* ebm) :
+ ScopedEventBaseThread(true, ebm) {}
+
ScopedEventBaseThread::~ScopedEventBaseThread() {
stop();
}
ScopedEventBaseThread::~ScopedEventBaseThread() {
stop();
}
return;
}
eventBase_ = make_unique<EventBase>();
return;
}
eventBase_ = make_unique<EventBase>();
- thread_ = make_unique<thread>(&EventBase::loopForever, &*eventBase_);
+ thread_ = make_unique<thread>(run, ebm_, eventBase_.get());
eventBase_->waitUntilRunning();
}
eventBase_->waitUntilRunning();
}
#include <memory>
#include <thread>
#include <folly/io/async/EventBase.h>
#include <memory>
#include <thread>
#include <folly/io/async/EventBase.h>
+#include <folly/io/async/EventBaseManager.h>
- * 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
*
* The new thread will be started by the ScopedEventBaseThread constructor.
* When the ScopedEventBaseThread object is destroyed, the thread will be
*/
class ScopedEventBaseThread {
public:
*/
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;
/**
~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();
*/
EventBase* getEventBase() const {
return eventBase_.get();
ScopedEventBaseThread(const ScopedEventBaseThread& other) = delete;
ScopedEventBaseThread& operator=(const ScopedEventBaseThread& other) = delete;
ScopedEventBaseThread(const ScopedEventBaseThread& other) = delete;
ScopedEventBaseThread& operator=(const ScopedEventBaseThread& other) = delete;
+ EventBaseManager* ebm_;
std::unique_ptr<EventBase> eventBase_;
std::unique_ptr<std::thread> thread_;
};
std::unique_ptr<EventBase> eventBase_;
std::unique_ptr<std::thread> thread_;
};
sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
done.timed_wait(steady_clock::now() + milliseconds(100));
}
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));
+}