Changeset View
Changeset View
Standalone View
Standalone View
src/rcu.h
Show First 20 Lines • Show All 190 Lines • ▼ Show 20 Lines | public: | ||||
*/ | */ | ||||
friend std::ostream &operator<<(std::ostream &stream, const RCUPtr &ptr) { | friend std::ostream &operator<<(std::ostream &stream, const RCUPtr &ptr) { | ||||
return stream << ptr.ptr; | return stream << ptr.ptr; | ||||
} | } | ||||
}; | }; | ||||
#define IMPLEMENT_RCU_REFCOUNT(T) \ | #define IMPLEMENT_RCU_REFCOUNT(T) \ | ||||
private: \ | private: \ | ||||
std::atomic<T> refcount{0}; \ | mutable std::atomic<T> refcount{0}; \ | ||||
\ | \ | ||||
void acquire() { refcount++; } \ | void acquire() const { refcount++; } \ | ||||
\ | \ | ||||
bool tryDecrement() { \ | bool tryDecrement() const { \ | ||||
T count = refcount.load(); \ | T count = refcount.load(); \ | ||||
while (count > 0) { \ | while (count > 0) { \ | ||||
if (refcount.compare_exchange_weak(count, count - 1)) { \ | if (refcount.compare_exchange_weak(count, count - 1)) { \ | ||||
return true; \ | return true; \ | ||||
} \ | } \ | ||||
} \ | } \ | ||||
\ | \ | ||||
return false; \ | return false; \ | ||||
} \ | } \ | ||||
\ | \ | ||||
void release() { \ | void release() const { \ | ||||
if (tryDecrement()) { \ | if (tryDecrement()) { \ | ||||
return; \ | return; \ | ||||
} \ | } \ | ||||
\ | \ | ||||
RCULock::registerCleanup([this] { \ | RCULock::registerCleanup([this] { \ | ||||
if (tryDecrement()) { \ | if (tryDecrement()) { \ | ||||
return; \ | return; \ | ||||
} \ | } \ | ||||
Show All 11 Lines |