blob: 8f34aeaca15c7ae3af2f8bba200db27d69612686 (
plain)
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
|
#ifndef INTRUSIVE_PTR_H
#define INTRUSIVE_PTR_H
#include "atomic.h"
#include "opthelpers.h"
namespace al {
template<typename T>
class intrusive_ref {
RefCount mRef{1u};
public:
unsigned int add_ref() noexcept { return IncrementRef(mRef); }
unsigned int release() noexcept
{
auto ref = DecrementRef(mRef);
if(UNLIKELY(ref == 0))
delete static_cast<T*>(this);
return ref;
}
/**
* Release only if doing so would not bring the object to 0 references and
* delete it. Returns false if the object could not be released.
*
* NOTE: The caller is responsible for handling a failed release, as it
* means the object has no other references and needs to be be deleted
* somehow.
*/
bool releaseIfNoDelete() noexcept
{
auto val = mRef.load(std::memory_order_acquire);
while(val > 1 && !mRef.compare_exchange_strong(val, val-1, std::memory_order_acq_rel))
{
/* val was updated with the current value on failure, so just try
* again.
*/
}
return val >= 2;
}
};
} // namespace al
#endif /* INTRUSIVE_PTR_H */
|