17 #ifndef GRPC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H
18 #define GRPC_CORE_LIB_GPRPP_DUAL_REF_COUNTED_H
51 template <
typename Child>
63 IncrementRefCount(location, reason);
69 const uint64_t prev_ref_pair =
71 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
73 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
74 if (trace_ !=
nullptr) {
75 gpr_log(
GPR_INFO,
"%s:%p unref %d -> %d, weak_ref %d -> %d", trace_,
this,
76 strong_refs, strong_refs - 1, weak_refs, weak_refs + 1);
87 const uint64_t prev_ref_pair =
89 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
91 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
92 if (trace_ !=
nullptr) {
94 trace_,
this, location.
file(), location.
line(), strong_refs,
95 strong_refs - 1, weak_refs, weak_refs + 1, reason);
113 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
115 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
116 if (trace_ !=
nullptr) {
118 trace_,
this, strong_refs, strong_refs + 1, weak_refs);
121 if (strong_refs == 0)
return nullptr;
132 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
134 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
135 if (trace_ !=
nullptr) {
137 "%s:%p %s:%d ref_if_non_zero %d -> %d (weak_refs=%d) %s",
138 trace_,
this, location.file(), location.line(), strong_refs,
139 strong_refs + 1, weak_refs, reason);
146 if (strong_refs == 0)
return nullptr;
154 IncrementWeakRefCount();
160 IncrementWeakRefCount(location, reason);
169 const char* trace = trace_;
171 const uint64_t prev_ref_pair =
174 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
175 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
176 if (trace !=
nullptr) {
178 weak_refs, weak_refs - 1, strong_refs);
183 delete static_cast<Child*
>(
this);
191 const char* trace = trace_;
193 const uint64_t prev_ref_pair =
196 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
197 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
198 if (trace !=
nullptr) {
199 gpr_log(
GPR_INFO,
"%s:%p %s:%d weak_unref %d -> %d (refs=%d) %s", trace,
200 this, location.
file(), location.
line(), weak_refs, weak_refs - 1,
201 strong_refs, reason);
210 delete static_cast<Child*
>(
this);
227 int32_t initial_refcount = 1)
232 refs_(MakeRefPair(initial_refcount, 0)) {
237 template <
typename T>
240 template <
typename T>
244 static uint64_t MakeRefPair(uint32_t strong, uint32_t weak) {
245 return (
static_cast<uint64_t
>(strong) << 32) +
static_cast<int64_t
>(weak);
247 static uint32_t GetStrongRefs(uint64_t ref_pair) {
248 return static_cast<uint32_t
>(ref_pair >> 32);
250 static uint32_t GetWeakRefs(uint64_t ref_pair) {
251 return static_cast<uint32_t
>(ref_pair & 0xffffffffu);
254 void IncrementRefCount() {
256 const uint64_t prev_ref_pair =
258 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
259 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
261 if (trace_ !=
nullptr) {
263 strong_refs, strong_refs + 1, weak_refs);
269 void IncrementRefCount(
const DebugLocation& location,
const char* reason) {
271 const uint64_t prev_ref_pair =
273 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
274 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
276 if (trace_ !=
nullptr) {
277 gpr_log(
GPR_INFO,
"%s:%p %s:%d ref %d -> %d (weak_refs=%d) %s", trace_,
278 this, location.file(), location.line(), strong_refs,
279 strong_refs + 1, weak_refs, reason);
289 void IncrementWeakRefCount() {
291 const uint64_t prev_ref_pair =
293 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
294 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
295 if (trace_ !=
nullptr) {
297 weak_refs, weak_refs + 1, strong_refs);
303 void IncrementWeakRefCount(
const DebugLocation& location,
304 const char* reason) {
306 const uint64_t prev_ref_pair =
308 const uint32_t strong_refs = GetStrongRefs(prev_ref_pair);
309 const uint32_t weak_refs = GetWeakRefs(prev_ref_pair);
310 if (trace_ !=
nullptr) {
311 gpr_log(
GPR_INFO,
"%s:%p %s:%d weak_ref %d -> %d (refs=%d) %s", trace_,
312 this, location.file(), location.line(), weak_refs, weak_refs + 1,
313 strong_refs, reason);
326 Atomic<uint64_t> refs_;
T FetchSub(Arg arg, MemoryOrder order=MemoryOrder::SEQ_CST)
Definition: atomic.h:77
bool CompareExchangeWeak(T *expected, T desired, MemoryOrder success, MemoryOrder failure)
Definition: atomic.h:56
T Load(MemoryOrder order) const
Definition: atomic.h:44
T FetchAdd(Arg arg, MemoryOrder order=MemoryOrder::SEQ_CST)
Definition: atomic.h:71
Definition: debug_location.h:31
int line() const
Definition: debug_location.h:35
const char * file() const
Definition: debug_location.h:34
Definition: dual_ref_counted.h:52
void Unref(const DebugLocation &location, const char *reason)
Definition: dual_ref_counted.h:86
DualRefCounted(const char *trace=nullptr, int32_t initial_refcount=1)
Definition: dual_ref_counted.h:220
WeakRefCountedPtr< Child > WeakRef() GRPC_MUST_USE_RESULT
Definition: dual_ref_counted.h:153
DualRefCounted & operator=(const DualRefCounted &)=delete
void WeakUnref()
Definition: dual_ref_counted.h:164
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: dual_ref_counted.h:56
RefCountedPtr< Child > RefIfNonZero(const DebugLocation &location, const char *reason) GRPC_MUST_USE_RESULT
Definition: dual_ref_counted.h:128
~DualRefCounted() override=default
RefCountedPtr< Child > Ref(const DebugLocation &location, const char *reason) GRPC_MUST_USE_RESULT
Definition: dual_ref_counted.h:61
void WeakUnref(const DebugLocation &location, const char *reason)
Definition: dual_ref_counted.h:186
DualRefCounted(const DualRefCounted &)=delete
WeakRefCountedPtr< Child > WeakRef(const DebugLocation &location, const char *reason) GRPC_MUST_USE_RESULT
Definition: dual_ref_counted.h:158
RefCountedPtr< Child > RefIfNonZero() GRPC_MUST_USE_RESULT
Definition: dual_ref_counted.h:110
void Unref()
Definition: dual_ref_counted.h:67
Definition: orphanable.h:42
Definition: ref_counted_ptr.h:35
Definition: ref_counted_ptr.h:186
#define GPR_ASSERT(x)
abort() the process if x is zero, having written a line to the log.
Definition: log.h:92
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
Log a message.
#define GPR_INFO
Definition: log.h:54
Round Robin Policy.
Definition: backend_metric.cc:26