diff options
author | Tor Egge <Tor.Egge@yahoo-inc.com> | 2017-02-17 14:01:13 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@yahoo-inc.com> | 2017-02-17 14:01:13 +0000 |
commit | 1b386b599a847c7e7edbdc3e861b784a32ed9614 (patch) | |
tree | bd72e9885287f4b6e69a7d718c43da604537e2d5 /searchcore | |
parent | dd6de6111d2ac222e8ce7b757c80d480abbb6b14 (diff) |
Add MonitoredRefCount class, replacing simliar functionality in documentdb.
It is used to wait for external references to document db being drained
(along with other threads doing things to the document db) before close
method can start to tear down stuff.
Diffstat (limited to 'searchcore')
3 files changed, 73 insertions, 0 deletions
diff --git a/searchcore/src/vespa/searchcore/proton/common/CMakeLists.txt b/searchcore/src/vespa/searchcore/proton/common/CMakeLists.txt index 544e23b20ac..889b3eb33b8 100644 --- a/searchcore/src/vespa/searchcore/proton/common/CMakeLists.txt +++ b/searchcore/src/vespa/searchcore/proton/common/CMakeLists.txt @@ -13,6 +13,7 @@ vespa_add_library(searchcore_pcommon STATIC feeddebugger.cpp feedtoken.cpp hw_info_sampler.cpp + monitored_refcount.cpp schemautil.cpp selectpruner.cpp selectcontext.cpp diff --git a/searchcore/src/vespa/searchcore/proton/common/monitored_refcount.cpp b/searchcore/src/vespa/searchcore/proton/common/monitored_refcount.cpp new file mode 100644 index 00000000000..0d8de9f8631 --- /dev/null +++ b/searchcore/src/vespa/searchcore/proton/common/monitored_refcount.cpp @@ -0,0 +1,44 @@ +// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "monitored_refcount.h" +#include <assert.h> + +namespace proton { + +MonitoredRefCount::MonitoredRefCount() + : _lock(), + _cv(), + _refCount(0u) +{ +} + +MonitoredRefCount::~MonitoredRefCount() +{ + assert(_refCount == 0u); +} + +void +MonitoredRefCount::retain() +{ + std::lock_guard<std::mutex> guard(_lock); + ++_refCount; +} + +void +MonitoredRefCount::release() +{ + std::lock_guard<std::mutex> guard(_lock); + --_refCount; + if (_refCount == 0u) { + _cv.notify_all(); + } +} + +void +MonitoredRefCount::waitForZeroRefCount() +{ + std::unique_lock<std::mutex> guard(_lock); + _cv.wait(guard, [this] { return (_refCount == 0u); }); +} + +} // namespace proton diff --git a/searchcore/src/vespa/searchcore/proton/common/monitored_refcount.h b/searchcore/src/vespa/searchcore/proton/common/monitored_refcount.h new file mode 100644 index 00000000000..71f16161d21 --- /dev/null +++ b/searchcore/src/vespa/searchcore/proton/common/monitored_refcount.h @@ -0,0 +1,28 @@ +// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include <mutex> +#include <condition_variable> + +namespace proton { + +/* + * Class containing a reference count that can be waited on to become zero. + * Typically ancestor or member of a class that has to be careful of when + * portions object can be properly torn down before destruction itself. + */ +class MonitoredRefCount +{ + std::mutex _lock; + std::condition_variable _cv; + uint32_t _refCount; + +public: + MonitoredRefCount(); + virtual ~MonitoredRefCount(); + void retain(); + void release(); + void waitForZeroRefCount(); +}; + +} // namespace proton |