aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/monitored_refcount.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'vespalib/src/vespa/vespalib/util/monitored_refcount.cpp')
-rw-r--r--vespalib/src/vespa/vespalib/util/monitored_refcount.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/vespalib/src/vespa/vespalib/util/monitored_refcount.cpp b/vespalib/src/vespa/vespalib/util/monitored_refcount.cpp
new file mode 100644
index 00000000000..4376e26bb66
--- /dev/null
+++ b/vespalib/src/vespa/vespalib/util/monitored_refcount.cpp
@@ -0,0 +1,44 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "monitored_refcount.h"
+#include <cassert>
+
+namespace vespalib {
+
+MonitoredRefCount::MonitoredRefCount()
+ : _lock(),
+ _cv(),
+ _refCount(0u)
+{
+}
+
+MonitoredRefCount::~MonitoredRefCount()
+{
+ assert(_refCount == 0u);
+}
+
+void
+MonitoredRefCount::retain() noexcept
+{
+ std::lock_guard<std::mutex> guard(_lock);
+ ++_refCount;
+}
+
+void
+MonitoredRefCount::release() noexcept
+{
+ 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); });
+}
+
+}