fm_ = fm;
}
-template <typename EventBaseT>
-inline void EventBaseLoopControllerT<EventBaseT>::schedule() {
+template <>
+inline void EventBaseLoopControllerT<folly::EventBase>::schedule() {
if (eventBase_ == nullptr) {
// In this case we need to postpone scheduling.
awaitingScheduling_ = true;
}
}
+template <>
+inline void EventBaseLoopControllerT<folly::VirtualEventBase>::schedule() {
+ if (eventBase_ == nullptr) {
+ // In this case we need to postpone scheduling.
+ awaitingScheduling_ = true;
+ } else {
+ // Schedule it to run in current iteration.
+
+ if (!eventBaseKeepAlive_) {
+ eventBaseKeepAlive_ = eventBase_->getKeepAliveToken();
+ }
+ eventBase_->getEventBase().runInLoop(&callback_, true);
+ awaitingScheduling_ = false;
+ }
+}
+
template <typename EventBaseT>
inline void EventBaseLoopControllerT<EventBaseT>::cancel() {
callback_.cancelLoopCallback();
template <typename EventBaseT>
inline void EventBaseLoopControllerT<EventBaseT>::runLoop() {
if (!eventBaseKeepAlive_) {
+ // runLoop can be called twice if both schedule() and scheduleThreadSafe()
+ // were called.
+ if (!fm_->hasTasks()) {
+ return;
+ }
eventBaseKeepAlive_ = eventBase_->getKeepAliveToken();
}
if (loopRunner_) {
*/
void runOnDestruction(EventBase::LoopCallback* callback);
- /**
- * @see EventBase::runInLoop
- */
- template <typename F>
- void runInLoop(F&& f, bool thisIteration = false) {
- evb_.runInLoop(std::forward<F>(f), thisIteration);
- }
-
/**
* VirtualEventBase destructor blocks until all tasks scheduled through its
* runInEventBaseThread are complete.
* KeepAlive handle can be released from EventBase loop only.
*/
KeepAlive getKeepAliveToken() override {
+ DCHECK(loopKeepAliveCount_ + loopKeepAliveCountAtomic_.load() > 0);
+
if (evb_.inRunningEventBaseThread()) {
++loopKeepAliveCount_;
} else {
EventBase& evb_;
- ssize_t loopKeepAliveCount_{0};
+ ssize_t loopKeepAliveCount_{1};
std::atomic<ssize_t> loopKeepAliveCountAtomic_{0};
std::promise<void> destroyPromise_;
std::future<void> destroyFuture_{destroyPromise_.get_future()};
- KeepAlive loopKeepAlive_;
+ KeepAlive loopKeepAlive_{makeKeepAlive()};
KeepAlive evbLoopKeepAlive_;