blob: 9eb713b12203ec529ea4bbdcffcdddd8e1d92368 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
// Copyright Yahoo. 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 RetainGuard;
/*
* 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;
void retain() noexcept;
void release() noexcept;
friend RetainGuard;
public:
MonitoredRefCount();
virtual ~MonitoredRefCount();
void waitForZeroRefCount();
};
class RetainGuard {
public:
RetainGuard(MonitoredRefCount & refCount) noexcept
: _refCount(&refCount)
{
_refCount->retain();
}
RetainGuard(const RetainGuard & rhs) = delete;
RetainGuard & operator=(const RetainGuard & rhs) = delete;
RetainGuard(RetainGuard && rhs) noexcept
: _refCount(rhs._refCount)
{
rhs._refCount = nullptr;
}
RetainGuard & operator=(RetainGuard && rhs) noexcept {
release();
_refCount = rhs._refCount;
rhs._refCount = nullptr;
return *this;
}
~RetainGuard() { release(); }
private:
void release() noexcept{
if (_refCount != nullptr) {
_refCount->release();
_refCount = nullptr;
}
}
MonitoredRefCount * _refCount;
};
} // namespace proton
|