diff options
author | Håvard Pettersen <havardpe@yahooinc.com> | 2023-03-08 08:45:54 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@yahooinc.com> | 2023-03-08 08:45:54 +0000 |
commit | 66ae46c80878aab4b0fc19d4c693e1a74cd8977a (patch) | |
tree | f2211ceae2fe5137d130363c8db8edc2dd6e0d15 | |
parent | 0d234088e4cace78188d71e599c2c1e48d30321a (diff) |
use ref_counted for ReplyGate
-rw-r--r-- | messagebus/src/tests/replygate/replygate.cpp | 4 | ||||
-rw-r--r-- | messagebus/src/vespa/messagebus/intermediatesession.cpp | 5 | ||||
-rw-r--r-- | messagebus/src/vespa/messagebus/intermediatesession.h | 13 | ||||
-rw-r--r-- | messagebus/src/vespa/messagebus/replygate.cpp | 7 | ||||
-rw-r--r-- | messagebus/src/vespa/messagebus/replygate.h | 4 | ||||
-rw-r--r-- | messagebus/src/vespa/messagebus/sourcesession.cpp | 7 | ||||
-rw-r--r-- | messagebus/src/vespa/messagebus/sourcesession.h | 5 | ||||
-rw-r--r-- | vespalib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | vespalib/src/tests/referencecounter/.gitignore | 4 | ||||
-rw-r--r-- | vespalib/src/tests/referencecounter/CMakeLists.txt | 8 | ||||
-rw-r--r-- | vespalib/src/tests/referencecounter/referencecounter_test.cpp | 56 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/overview.h | 59 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/referencecounter.h | 62 |
13 files changed, 23 insertions, 212 deletions
diff --git a/messagebus/src/tests/replygate/replygate.cpp b/messagebus/src/tests/replygate/replygate.cpp index 3de48fce130..c71993c9368 100644 --- a/messagebus/src/tests/replygate/replygate.cpp +++ b/messagebus/src/tests/replygate/replygate.cpp @@ -59,7 +59,7 @@ TEST("replygate_test") { { RoutableQueue q; MySender sender; - MyGate *gate = new MyGate(sender); + auto gate = vespalib::make_ref_counted<MyGate>(sender); { auto msg = std::make_unique<SimpleMessage>("test"); msg->pushHandler(q); @@ -79,7 +79,7 @@ TEST("replygate_test") { EXPECT_TRUE(MyReply::dtorCnt == 1); EXPECT_TRUE(MyGate::ctorCnt == 1); EXPECT_TRUE(MyGate::dtorCnt == 0); - gate->subRef(); + gate = vespalib::ref_counted<MyGate>(); EXPECT_TRUE(MyGate::ctorCnt == 1); EXPECT_TRUE(MyGate::dtorCnt == 1); } diff --git a/messagebus/src/vespa/messagebus/intermediatesession.cpp b/messagebus/src/vespa/messagebus/intermediatesession.cpp index 2b8830f07e8..61cd77c0165 100644 --- a/messagebus/src/vespa/messagebus/intermediatesession.cpp +++ b/messagebus/src/vespa/messagebus/intermediatesession.cpp @@ -4,6 +4,8 @@ #include "messagebus.h" #include "replygate.h" +using vespalib::make_ref_counted; + namespace mbus { IntermediateSession::IntermediateSession(MessageBus &mbus, const IntermediateSessionParams ¶ms) : @@ -11,14 +13,13 @@ IntermediateSession::IntermediateSession(MessageBus &mbus, const IntermediateSes _name(params.getName()), _msgHandler(params.getMessageHandler()), _replyHandler(params.getReplyHandler()), - _gate(new ReplyGate(_mbus)) + _gate(make_ref_counted<ReplyGate>(_mbus)) { } IntermediateSession::~IntermediateSession() { _gate->close(); close(); - _gate->subRef(); } void diff --git a/messagebus/src/vespa/messagebus/intermediatesession.h b/messagebus/src/vespa/messagebus/intermediatesession.h index 0a17aa1e42a..8d938b6cb9e 100644 --- a/messagebus/src/vespa/messagebus/intermediatesession.h +++ b/messagebus/src/vespa/messagebus/intermediatesession.h @@ -4,11 +4,11 @@ #include "reply.h" #include "imessagehandler.h" #include "intermediatesessionparams.h" +#include "replygate.h" namespace mbus { class MessageBus; -class ReplyGate; class Message; /** @@ -21,12 +21,13 @@ class IntermediateSession : public IMessageHandler, private: friend class MessageBus; using MessageUP = std::unique_ptr<Message>; + template <typename T> using ref_counted = vespalib::ref_counted<T>; - MessageBus &_mbus; - string _name; - IMessageHandler &_msgHandler; - IReplyHandler &_replyHandler; - ReplyGate *_gate; + MessageBus &_mbus; + string _name; + IMessageHandler &_msgHandler; + IReplyHandler &_replyHandler; + ref_counted<ReplyGate> _gate; /** * This constructor is declared package private since only MessageBus is supposed to instantiate it. diff --git a/messagebus/src/vespa/messagebus/replygate.cpp b/messagebus/src/vespa/messagebus/replygate.cpp index 8094119f14c..1028d46aff4 100644 --- a/messagebus/src/vespa/messagebus/replygate.cpp +++ b/messagebus/src/vespa/messagebus/replygate.cpp @@ -6,7 +6,6 @@ namespace mbus { ReplyGate::ReplyGate(IMessageHandler &sender) : - vespalib::ReferenceCounter(), _sender(sender), _open(true) { } @@ -14,7 +13,7 @@ ReplyGate::ReplyGate(IMessageHandler &sender) : void ReplyGate::handleMessage(Message::UP msg) { - addRef(); + internal_addref(); msg->pushHandler(*this, *this); _sender.handleMessage(std::move(msg)); } @@ -34,13 +33,13 @@ ReplyGate::handleReply(Reply::UP reply) } else { reply->discard(); } - subRef(); + internal_subref(); } void ReplyGate::handleDiscard(Context) { - subRef(); + internal_subref(); } } // namespace mbus diff --git a/messagebus/src/vespa/messagebus/replygate.h b/messagebus/src/vespa/messagebus/replygate.h index 0c487de3ecf..e09dfe63365 100644 --- a/messagebus/src/vespa/messagebus/replygate.h +++ b/messagebus/src/vespa/messagebus/replygate.h @@ -5,7 +5,7 @@ #include "idiscardhandler.h" #include "imessagehandler.h" #include "ireplyhandler.h" -#include <vespa/vespalib/util/referencecounter.h> +#include <vespa/vespalib/util/ref_counted.h> #include <atomic> namespace mbus { @@ -20,7 +20,7 @@ namespace mbus { * is handled outside this class. Note that this class is only intended for * internal use. */ -class ReplyGate : public vespalib::ReferenceCounter, +class ReplyGate : public vespalib::enable_ref_counted, public IDiscardHandler, public IMessageHandler, public IReplyHandler diff --git a/messagebus/src/vespa/messagebus/sourcesession.cpp b/messagebus/src/vespa/messagebus/sourcesession.cpp index 0691e0c07f9..feff3beed58 100644 --- a/messagebus/src/vespa/messagebus/sourcesession.cpp +++ b/messagebus/src/vespa/messagebus/sourcesession.cpp @@ -6,15 +6,17 @@ #include "tracelevel.h" #include <vespa/messagebus/routing/routingtable.h> #include <vespa/vespalib/util/stringfmt.h> +#include <cassert> using vespalib::make_string; +using vespalib::make_ref_counted; namespace mbus { SourceSession::SourceSession(MessageBus &mbus, const SourceSessionParams ¶ms) : _lock(), _mbus(mbus), - _gate(new ReplyGate(_mbus)), + _gate(make_ref_counted<ReplyGate>(_mbus)), _sequencer(*_gate), _replyHandler(params.getReplyHandler()), _throttlePolicy(params.getThrottlePolicy()), @@ -31,9 +33,6 @@ SourceSession::~SourceSession() // Ensure that no more replies propagate from mbus. _gate->close(); _mbus.sync(); - - // Tell gate that we will no longer use it. - _gate->subRef(); } Result diff --git a/messagebus/src/vespa/messagebus/sourcesession.h b/messagebus/src/vespa/messagebus/sourcesession.h index d918724ad4f..03628f06c56 100644 --- a/messagebus/src/vespa/messagebus/sourcesession.h +++ b/messagebus/src/vespa/messagebus/sourcesession.h @@ -5,13 +5,13 @@ #include "result.h" #include "sequencer.h" #include "sourcesessionparams.h" +#include "replygate.h" #include <atomic> #include <condition_variable> namespace mbus { class MessageBus; -class ReplyGate; /** * A SourceSession is used to send Message objects along a named or explicitly defined route and get Reply @@ -21,11 +21,12 @@ class ReplyGate; class SourceSession : public IReplyHandler { private: friend class MessageBus; + template <typename T> using ref_counted = vespalib::ref_counted<T>; std::mutex _lock; std::condition_variable _cond; MessageBus &_mbus; - ReplyGate *_gate; + ref_counted<ReplyGate> _gate; Sequencer _sequencer; IReplyHandler &_replyHandler; IThrottlePolicy::SP _throttlePolicy; diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt index b689d587bee..dc99146fd3f 100644 --- a/vespalib/CMakeLists.txt +++ b/vespalib/CMakeLists.txt @@ -141,7 +141,6 @@ vespa_define_module( src/tests/programoptions src/tests/random src/tests/ref_counted - src/tests/referencecounter src/tests/regex src/tests/rendezvous src/tests/require diff --git a/vespalib/src/tests/referencecounter/.gitignore b/vespalib/src/tests/referencecounter/.gitignore deleted file mode 100644 index 7c753a29fab..00000000000 --- a/vespalib/src/tests/referencecounter/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -.depend -Makefile -referencecounter_test -vespalib_referencecounter_test_app diff --git a/vespalib/src/tests/referencecounter/CMakeLists.txt b/vespalib/src/tests/referencecounter/CMakeLists.txt deleted file mode 100644 index ee909b48281..00000000000 --- a/vespalib/src/tests/referencecounter/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(vespalib_referencecounter_test_app TEST - SOURCES - referencecounter_test.cpp - DEPENDS - vespalib -) -vespa_add_test(NAME vespalib_referencecounter_test_app COMMAND vespalib_referencecounter_test_app) diff --git a/vespalib/src/tests/referencecounter/referencecounter_test.cpp b/vespalib/src/tests/referencecounter/referencecounter_test.cpp deleted file mode 100644 index 99dd455bcc8..00000000000 --- a/vespalib/src/tests/referencecounter/referencecounter_test.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/log/log.h> -LOG_SETUP("referencecounter_test"); -#include <vespa/vespalib/testkit/testapp.h> -#include <vespa/vespalib/util/referencecounter.h> - -struct Data -{ - int ctorCnt; - int dtorCnt; - Data() : ctorCnt(0), dtorCnt(0) {} -}; - -class DataRef : public vespalib::ReferenceCounter -{ -private: - Data &_d; - DataRef(const DataRef &); - DataRef &operator=(const DataRef &); -public: - DataRef(Data &d) : _d(d) { ++d.ctorCnt; } - ~DataRef() { ++_d.dtorCnt; } - int getCtorCnt() const { return _d.ctorCnt; } - int getDtorCnt() const { return _d.dtorCnt; } -}; - -using namespace vespalib; - -TEST_SETUP(Test); - -int -Test::Main() -{ - TEST_INIT("referencecounter_test"); - - Data data; - { - DataRef *pt1 = new DataRef(data); - - EXPECT_TRUE(pt1->refCount() == 1); - - DataRef *pt2 = pt1; - pt2->addRef(); - - EXPECT_TRUE(pt1->refCount() == 2); - - EXPECT_TRUE(data.ctorCnt == 1); - EXPECT_TRUE(data.dtorCnt == 0); - pt1->subRef(); - EXPECT_TRUE(pt1->refCount() == 1); - pt2->subRef(); - } - EXPECT_TRUE(data.ctorCnt == 1); - EXPECT_TRUE(data.dtorCnt == 1); - TEST_DONE(); -} diff --git a/vespalib/src/vespa/vespalib/util/overview.h b/vespalib/src/vespa/vespalib/util/overview.h deleted file mode 100644 index 74bf0a33310..00000000000 --- a/vespalib/src/vespa/vespalib/util/overview.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/*! \mainpage Vespalib - C++ utility library for Vespa components - * - * \section intro_sec Introduction - * - * vespalib is a collection of simple utility classes shared - * between most Vespa components that are written in C++. - * Most of these aren't Vespa specific in any way, except that - * they reflect the Vespa code standard and conventions. - * - * \section install_sec Installation - * - * install vespa_vespalib_dev - * - * \section overview_sec Overview - * - * Most of the classes in vespalib can be used by themselves, - * for a full list see the alphabetical class list. - * Here are the major groups of classes: - * - * Generation counter - * - * vespalib::GenCnt - * - * Reference counting - * - * <BR> vespalib::ReferenceCounter - * - * Advanced pointer utilities - * - * \ref vespalib::PtrHolder<T> - * - * Simple hashmap - * - * \ref vespalib::HashMap<T> - * - * Scope guards for easier exception-safety - * - * vespalib::CounterGuard - * <BR> vespalib::DirPointer - * <BR> vespalib::FileDescriptor - * <BR> vespalib::FilePointer - * <BR> \ref vespalib::MaxValueGuard<T> - * <BR> \ref vespalib::ValueGuard<T> - * - * General utility macros - * <BR> \ref VESPA_STRLOC - * <BR> \ref VESPA_STRINGIZE(str) - * - * Standalone testing framework - * - * vespalib::TestApp - * - * Text handling - * - * vespalib::Utf8Reader - * <BR> vespalib::Utf8Writer - * <BR> vespalib::LowerCase - */ diff --git a/vespalib/src/vespa/vespalib/util/referencecounter.h b/vespalib/src/vespa/vespalib/util/referencecounter.h deleted file mode 100644 index 28bc927fe03..00000000000 --- a/vespalib/src/vespa/vespalib/util/referencecounter.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/** - * @file referencecounter.h - * @author Thomas F. Gundersen - * @version $Id$ - * @date 2004-03-19 - **/ - -#pragma once - -#include <atomic> -#include <cassert> - -namespace vespalib -{ - -/** - * @brief Inherit this class to create a self-destroying class. - * - * Allows for objects to be shared without worrying about who "owns" - * the object. When a new owner is given the object, addRef() should - * be called. When finished with the object, subRef() should be - * called. When the last owner calls subRef(), the object is deleted. -*/ -class ReferenceCounter -{ -public: - /** - * @brief Constructor. The object will initially have 1 reference. - **/ - ReferenceCounter() : _refs(1) {} - - /** - * @brief Add an owner of this object. - * - * When the owner is finished with the - * object, call subRef(). - **/ - void addRef() { _refs.fetch_add(1); } - - /** - * @brief Remove an owner of this object. - * - * If that was the last owner, delete the object. - **/ - void subRef() { - if (_refs.fetch_sub(1) == 1) { - delete this; - } - } - unsigned refCount() const { return _refs; } -protected: - /** - * @brief Destructor. Does sanity check only. - **/ - virtual ~ReferenceCounter() { assert (_refs == 0); }; -private: - std::atomic<unsigned> _refs; -}; - -} // namespace vespalib - |