Commit 439dc4ea authored by Sven Over's avatar Sven Over Committed by Facebook Github Bot

add Executor::KeepAlive::copy() method

Summary: We do not want implicit copies of `KeepAlive`, because they may be accidental and they impose an overhead. However, making a copy by calling `folly::getKeepAliveToken(keep_alive.get())` does not look very nice. Also, in case that `keep_alive` is a dummy, it makes an unnecessary call to `Executor::keepAliveAcquire`, which will return false but cannot be inlined usually because it's a virtual function. This diff adds a `copy() const` method which returns a copy of the `KeepAlive` object. It only calls `Executor::keepAliveAcquire` if the original object is not a dummy.

Reviewed By: yfeldblum, andriigrynenko

Differential Revision: D8203839

fbshipit-source-id: bb595d3836405bd84e55c4214ac19127b1e0e42a
parent 9dd22f55
...@@ -110,6 +110,10 @@ class Executor { ...@@ -110,6 +110,10 @@ class Executor {
return get(); return get();
} }
KeepAlive copy() const {
return getKeepAliveToken(get());
}
private: private:
static constexpr intptr_t kDummyFlag = 1; static constexpr intptr_t kDummyFlag = 1;
static constexpr intptr_t kExecutorMask = ~kDummyFlag; static constexpr intptr_t kExecutorMask = ~kDummyFlag;
......
...@@ -91,4 +91,23 @@ TEST(ExecutorTest, KeepAliveConvert) { ...@@ -91,4 +91,23 @@ TEST(ExecutorTest, KeepAliveConvert) {
EXPECT_EQ(0, exec.refCount); EXPECT_EQ(0, exec.refCount);
} }
TEST(ExecutorTest, KeepAliveCopy) {
KeepAliveTestExecutor exec;
{
Executor::KeepAlive<KeepAliveTestExecutor>&& ka = getKeepAliveToken(exec);
EXPECT_TRUE(ka);
EXPECT_EQ(std::addressof(exec), ka.get());
EXPECT_EQ(1, exec.refCount);
Executor::KeepAlive<KeepAliveTestExecutor>&& ka2 = ka.copy();
EXPECT_TRUE(ka);
EXPECT_TRUE(ka2);
EXPECT_EQ(std::addressof(exec), ka2.get());
EXPECT_EQ(2, exec.refCount);
}
EXPECT_EQ(0, exec.refCount);
}
} // namespace folly } // namespace folly
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