/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
namespace folly {
VirtualEventBase::VirtualEventBase(EventBase& evb) : evb_(evb) {
- evbLoopKeepAlive_ = evb_.loopKeepAliveAtomic();
- loopKeepAlive_ = loopKeepAliveAtomic();
+ evbLoopKeepAlive_ = evb_.getKeepAliveToken();
}
-VirtualEventBase::~VirtualEventBase() {
- CHECK(!evb_.inRunningEventBaseThread());
+std::future<void> VirtualEventBase::destroy() {
+ CHECK(evb_.runInEventBaseThread([this] { loopKeepAlive_.reset(); }));
- CHECK(evb_.runInEventBaseThread([&] { loopKeepAlive_.reset(); }));
- loopKeepAliveBaton_.wait();
+ return std::move(destroyFuture_);
+}
- CHECK(evb_.runInEventBaseThreadAndWait([&] {
+void VirtualEventBase::destroyImpl() {
+ // Make sure we release EventBase KeepAlive token even if exception occurs
+ auto evbLoopKeepAlive = std::move(evbLoopKeepAlive_);
+ try {
clearCobTimeouts();
onDestructionCallbacks_.withWLock([&](LoopCallbackList& callbacks) {
}
});
- evbLoopKeepAlive_.reset();
- }));
+ destroyPromise_.set_value();
+ } catch (...) {
+ destroyPromise_.set_exception(std::current_exception());
+ }
+}
+
+VirtualEventBase::~VirtualEventBase() {
+ if (!destroyFuture_.valid()) {
+ return;
+ }
+ CHECK(!evb_.inRunningEventBaseThread());
+ destroy().get();
}
void VirtualEventBase::runOnDestruction(EventBase::LoopCallback* callback) {
callbacks.push_back(*callback);
});
}
-}
+} // namespace folly