#include <boost/intrusive/list.hpp>
#include <boost/utility.hpp>
+#include <folly/CallOnce.h>
#include <folly/Executor.h>
#include <folly/Function.h>
#include <folly/Portability.h>
loopOnce();
}
- struct LoopKeepAliveDeleter {
- void operator()(EventBase* evb) {
- DCHECK(evb->isInEventBaseThread());
- evb->loopKeepAliveCount_--;
- }
- };
- using LoopKeepAlive = std::unique_ptr<EventBase, LoopKeepAliveDeleter>;
-
/// Returns you a handle which make loop() behave like loopForever() until
/// destroyed. loop() will return to its original behavior only when all
/// loop keep-alives are released. Loop holder is safe to release only from
/// EventBase thread.
- ///
- /// May return no op LoopKeepAlive if loopForever() is already running.
- LoopKeepAlive loopKeepAlive() {
- DCHECK(isInEventBaseThread());
- loopKeepAliveCount_++;
- return LoopKeepAlive(this);
- }
-
- // Thread-safe version of loopKeepAlive()
- LoopKeepAlive loopKeepAliveAtomic() {
+ KeepAlive getKeepAliveToken() override {
if (inRunningEventBaseThread()) {
- return loopKeepAlive();
+ loopKeepAliveCount_++;
+ } else {
+ loopKeepAliveCountAtomic_.fetch_add(1, std::memory_order_relaxed);
}
- loopKeepAliveCountAtomic_.fetch_add(1, std::memory_order_relaxed);
- return LoopKeepAlive(this);
+ return makeKeepAlive();
}
// TimeoutManager
return isInEventBaseThread();
}
+ // Returns a VirtualEventBase attached to this EventBase. Can be used to
+ // pass to APIs which expect VirtualEventBase. This VirtualEventBase will be
+ // destroyed together with the EventBase.
+ //
+ // Any number of VirtualEventBases instances may be independently constructed,
+ // which are backed by this EventBase. This method should be only used if you
+ // don't need to manage the life time of the VirtualEventBase used.
+ folly::VirtualEventBase& getVirtualEventBase();
+
+ protected:
+ void keepAliveRelease() override {
+ DCHECK(isInEventBaseThread());
+ loopKeepAliveCount_--;
+ }
+
private:
void applyLoopKeepAlive();
// see EventBaseLocal
friend class detail::EventBaseLocalBase;
template <typename T> friend class EventBaseLocal;
- std::mutex localStorageMutex_;
std::unordered_map<uint64_t, std::shared_ptr<void>> localStorage_;
std::unordered_set<detail::EventBaseLocalBaseBase*> localStorageToDtor_;
+
+ folly::once_flag virtualEventBaseInitFlag_;
+ std::unique_ptr<VirtualEventBase> virtualEventBase_;
};
template <typename T>