summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2023-03-08 10:31:25 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2023-03-08 10:31:25 +0000
commitc95beeaee379877c9c706000529074e826788bf9 (patch)
tree6705e409430d21072d4df05b78619f86e1efd8c4
parent1a47ec734d8a266059f2c03c95a96bd5ae655aab (diff)
added reset function
-rw-r--r--messagebus/src/tests/replygate/replygate.cpp2
-rw-r--r--vespalib/src/tests/ref_counted/ref_counted_test.cpp11
-rw-r--r--vespalib/src/vespa/vespalib/util/ref_counted.h1
3 files changed, 13 insertions, 1 deletions
diff --git a/messagebus/src/tests/replygate/replygate.cpp b/messagebus/src/tests/replygate/replygate.cpp
index c71993c9368..56b849833e0 100644
--- a/messagebus/src/tests/replygate/replygate.cpp
+++ b/messagebus/src/tests/replygate/replygate.cpp
@@ -79,7 +79,7 @@ TEST("replygate_test") {
EXPECT_TRUE(MyReply::dtorCnt == 1);
EXPECT_TRUE(MyGate::ctorCnt == 1);
EXPECT_TRUE(MyGate::dtorCnt == 0);
- gate = vespalib::ref_counted<MyGate>();
+ gate.reset();
EXPECT_TRUE(MyGate::ctorCnt == 1);
EXPECT_TRUE(MyGate::dtorCnt == 1);
}
diff --git a/vespalib/src/tests/ref_counted/ref_counted_test.cpp b/vespalib/src/tests/ref_counted/ref_counted_test.cpp
index 2a5321b7b27..113c64bb55e 100644
--- a/vespalib/src/tests/ref_counted/ref_counted_test.cpp
+++ b/vespalib/src/tests/ref_counted/ref_counted_test.cpp
@@ -230,6 +230,17 @@ TEST(RefCountedTest, self_assign) {
EXPECT_EQ(ref->val, 10);
}
+TEST(RefCountedTest, reset) {
+ CheckObjects check(1);
+ auto ref = make_ref_counted<Base>(10);
+ auto pre_cnt = Base::dtor_cnt.load(std::memory_order_relaxed);
+ EXPECT_TRUE(ref);
+ ref.reset();
+ EXPECT_FALSE(ref);
+ auto post_cnt = Base::dtor_cnt.load(std::memory_order_relaxed);
+ EXPECT_EQ(post_cnt, pre_cnt + 1);
+}
+
TEST(RefCountedTest, with_threads) {
CheckObjects check(2,1);
ThreadPool pool;
diff --git a/vespalib/src/vespa/vespalib/util/ref_counted.h b/vespalib/src/vespa/vespalib/util/ref_counted.h
index aff8ae7bb7e..5b1e1b66407 100644
--- a/vespalib/src/vespa/vespalib/util/ref_counted.h
+++ b/vespalib/src/vespa/vespalib/util/ref_counted.h
@@ -94,6 +94,7 @@ public:
T *operator->() const noexcept { return _ptr; }
T &operator*() const noexcept { return *_ptr; }
operator bool() const noexcept { return (_ptr != nullptr); }
+ void reset() noexcept { replace_with(nullptr); }
~ref_counted() noexcept { maybe_subref(); }
// NB: will not call subref
T *internal_detach() noexcept { return std::exchange(_ptr, nullptr); }