Commit 7e9d7487 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

VirtualExecutor join should wait for function to be destroyed

Reviewed By: aary

Differential Revision: D8393345

fbshipit-source-id: 5fc9bbfb3fa1dc63077cd406f4335c2a7967a53d
parent b51d7408
...@@ -519,7 +519,8 @@ nobase_follyinclude_HEADERS = \ ...@@ -519,7 +519,8 @@ nobase_follyinclude_HEADERS = \
Uri.h \ Uri.h \
Uri-inl.h \ Uri-inl.h \
Utility.h \ Utility.h \
Varint.h Varint.h \
VirtualExecutor.h
libfollybasesse42_la_SOURCES = \ libfollybasesse42_la_SOURCES = \
detail/RangeSse42.cpp \ detail/RangeSse42.cpp \
......
...@@ -30,6 +30,24 @@ namespace folly { ...@@ -30,6 +30,24 @@ namespace folly {
* backed by it are released. * backed by it are released.
*/ */
class VirtualExecutor : public DefaultKeepAliveExecutor { class VirtualExecutor : public DefaultKeepAliveExecutor {
auto wrapFunc(Func f) {
class FuncAndKeepAlive {
public:
FuncAndKeepAlive(Func&& f, VirtualExecutor* executor)
: keepAlive_(getKeepAliveToken(executor)), f_(std::move(f)) {}
void operator()() {
f_();
}
private:
Executor::KeepAlive<VirtualExecutor> keepAlive_;
Func f_;
};
return FuncAndKeepAlive(std::move(f), this);
}
public: public:
explicit VirtualExecutor(KeepAlive<> executor) explicit VirtualExecutor(KeepAlive<> executor)
: executor_(std::move(executor)) { : executor_(std::move(executor)) {
...@@ -50,16 +68,11 @@ class VirtualExecutor : public DefaultKeepAliveExecutor { ...@@ -50,16 +68,11 @@ class VirtualExecutor : public DefaultKeepAliveExecutor {
} }
void add(Func f) override { void add(Func f) override {
executor_->add([func = std::move(f), executor_->add(wrapFunc(std::move(f)));
me = getKeepAliveToken(this)]() mutable { func(); });
} }
void addWithPriority(Func f, int8_t priority) override { void addWithPriority(Func f, int8_t priority) override {
executor_->addWithPriority( executor_->addWithPriority(wrapFunc(std::move(f)), priority);
[func = std::move(f), me = getKeepAliveToken(this)]() mutable {
func();
},
priority);
} }
~VirtualExecutor() override { ~VirtualExecutor() override {
......
...@@ -775,6 +775,21 @@ static void virtualExecutorTest() { ...@@ -775,6 +775,21 @@ static void virtualExecutorTest() {
.semi(); .semi();
} }
EXPECT_EQ(1, counter); EXPECT_EQ(1, counter);
bool functionDestroyed{false};
bool functionCalled{false};
{
VirtualExecutor ve(fe);
auto guard = makeGuard([&functionDestroyed] {
std::this_thread::sleep_for(100ms);
functionDestroyed = true;
});
ve.add([&functionCalled, guard = std::move(guard)] {
functionCalled = true;
});
}
EXPECT_TRUE(functionCalled);
EXPECT_TRUE(functionDestroyed);
} }
EXPECT_TRUE(f->isReady()); EXPECT_TRUE(f->isReady());
EXPECT_NO_THROW(std::move(*f).get()); EXPECT_NO_THROW(std::move(*f).get());
......
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