Go to the documentation of this file.
19 #ifndef GRPC_CORE_LIB_GPRPP_REF_COUNTED_H
20 #define GRPC_CORE_LIB_GPRPP_REF_COUNTED_H
71 template <
typename TraceFlagT = TraceFlag>
82 trace_flag_(trace_flag),
91 if (trace_flag_ !=
nullptr && trace_flag_->
enabled()) {
93 trace_flag_->
name(),
this, prior, prior + n);
96 value_.
FetchAdd(n, MemoryOrder::RELAXED);
101 const Value prior = value_.
FetchAdd(n, MemoryOrder::RELAXED);
102 if (trace_flag_ !=
nullptr && trace_flag_->
enabled()) {
104 trace_flag_->
name(),
this, location.
file(), location.
line(),
105 prior, prior + n, reason);
112 value_.
FetchAdd(n, MemoryOrder::RELAXED);
119 const Value prior = value_.
FetchAdd(1, MemoryOrder::RELAXED);
120 if (trace_flag_ !=
nullptr && trace_flag_->
enabled()) {
122 trace_flag_->
name(),
this, prior, prior + 1);
126 value_.
FetchAdd(1, MemoryOrder::RELAXED);
131 const Value prior = value_.
FetchAdd(1, MemoryOrder::RELAXED);
132 if (trace_flag_ !=
nullptr && trace_flag_->
enabled()) {
134 trace_flag_->
name(),
this, location.
file(), location.
line(),
135 prior, prior + 1, reason);
148 if (trace_flag_ !=
nullptr && trace_flag_->
enabled()) {
149 const Value prior = get();
151 trace_flag_->
name(),
this, prior, prior + 1);
158 if (trace_flag_ !=
nullptr && trace_flag_->
enabled()) {
159 const Value prior = get();
161 "%s:%p %s:%d ref_if_non_zero "
162 "%" PRIdPTR
" -> %" PRIdPTR
" %s",
163 trace_flag_->
name(),
this, location.
file(), location.
line(),
164 prior, prior + 1, reason);
178 auto* trace_flag = trace_flag_;
180 const Value prior = value_.
FetchSub(1, MemoryOrder::ACQ_REL);
182 if (trace_flag !=
nullptr && trace_flag->enabled()) {
184 trace_flag->name(),
this, prior, prior - 1);
194 auto* trace_flag = trace_flag_;
196 const Value prior = value_.
FetchSub(1, MemoryOrder::ACQ_REL);
198 if (trace_flag !=
nullptr && trace_flag->enabled()) {
200 trace_flag->name(),
this, location.
file(), location.
line(), prior,
213 Value get()
const {
return value_.
Load(MemoryOrder::RELAXED); }
216 TraceFlag* trace_flag_;
218 Atomic<Value> value_;
247 template <
typename Child,
typename Impl = PolymorphicRefCount>
260 IncrementRefCount(location, reason);
270 delete static_cast<Child*
>(
this);
275 delete static_cast<Child*
>(
this);
292 template <
typename TraceFlagT = TraceFlag>
294 intptr_t initial_refcount = 1)
295 : refs_(initial_refcount, trace_flag) {}
299 template <
typename T>
302 void IncrementRefCount() { refs_.
Ref(); }
303 void IncrementRefCount(
const DebugLocation& location,
const char* reason) {
304 refs_.
Ref(location, reason);
#define GPR_INFO
Definition: log.h:56
~NonPolymorphicRefCount()=default
intptr_t Value
Definition: ref_counted.h:64
RefCounted(TraceFlagT *trace_flag=nullptr, intptr_t initial_refcount=1)
Definition: ref_counted.h:293
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
Log a message.
bool RefIfNonZero()
Definition: ref_counted.h:279
void Ref(Value n=1)
Definition: ref_counted.h:88
bool Unref()
Definition: ref_counted.h:174
Definition: debug_location.h:31
T Load(MemoryOrder order) const
Definition: atomic.h:44
Round Robin Policy.
Definition: backend_metric.cc:24
bool RefIfNonZero(const DebugLocation &location, const char *reason)
Definition: ref_counted.h:156
const char * file() const
Definition: debug_location.h:34
Definition: ref_counted.h:248
const char * name() const
Definition: trace.h:68
T FetchAdd(Arg arg, MemoryOrder order=MemoryOrder::SEQ_CST)
Definition: atomic.h:71
virtual ~PolymorphicRefCount()=default
Definition: ref_counted.h:49
Definition: ref_counted_ptr.h:35
RefCountedPtr< Child > Ref() GRPC_MUST_USE_RESULT
Definition: ref_counted.h:253
void RefNonZero()
Definition: ref_counted.h:117
bool IncrementIfNonzero(MemoryOrder load_order=MemoryOrder::ACQUIRE)
Definition: atomic.h:84
RefCounted & operator=(const RefCounted &)=delete
void RefNonZero(const DebugLocation &location, const char *reason)
Definition: ref_counted.h:129
#define GPR_DEBUG_ASSERT(x)
Definition: log.h:103
bool RefIfNonZero(const DebugLocation &location, const char *reason)
Definition: ref_counted.h:280
bool enabled()
Definition: trace.h:80
Definition: ref_counted.h:62
int line() const
Definition: debug_location.h:35
RefCounted(const RefCounted &)=delete
void Unref(const DebugLocation &location, const char *reason)
Definition: ref_counted.h:273
T FetchSub(Arg arg, MemoryOrder order=MemoryOrder::SEQ_CST)
Definition: atomic.h:77
bool Unref(const DebugLocation &location, const char *reason)
Definition: ref_counted.h:190
void Ref(const DebugLocation &location, const char *reason, Value n=1)
Definition: ref_counted.h:99
constexpr RefCount(Value init=1, TraceFlagT *trace_flag=nullptr)
Definition: ref_counted.h:72
void Unref()
Definition: ref_counted.h:268
bool RefIfNonZero()
Definition: ref_counted.h:146
RefCountedPtr< Child > Ref(const DebugLocation &location, const char *reason) GRPC_MUST_USE_RESULT
Definition: ref_counted.h:258
Definition: ref_counted.h:41