aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/gprpp/ref_counted.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lib/gprpp/ref_counted.h')
-rw-r--r--src/core/lib/gprpp/ref_counted.h40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/core/lib/gprpp/ref_counted.h b/src/core/lib/gprpp/ref_counted.h
index c68118a71a..ab589fc01a 100644
--- a/src/core/lib/gprpp/ref_counted.h
+++ b/src/core/lib/gprpp/ref_counted.h
@@ -26,16 +26,28 @@
#include "src/core/lib/gprpp/abstract.h"
#include "src/core/lib/gprpp/debug_location.h"
#include "src/core/lib/gprpp/memory.h"
+#include "src/core/lib/gprpp/ref_counted_ptr.h"
namespace grpc_core {
// A base class for reference-counted objects.
// New objects should be created via New() and start with a refcount of 1.
// When the refcount reaches 0, the object will be deleted via Delete().
+//
+// This will commonly be used by CRTP (curiously-recurring template pattern)
+// e.g., class MyClass : public RefCounted<MyClass>
+template <typename Child>
class RefCounted {
public:
- void Ref() { gpr_ref(&refs_); }
+ RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
+ IncrementRefCount();
+ return RefCountedPtr<Child>(static_cast<Child*>(this));
+ }
+ // TODO(roth): Once all of our code is converted to C++ and can use
+ // RefCountedPtr<> instead of manual ref-counting, make this method
+ // private, since it will only be used by RefCountedPtr<>, which is a
+ // friend of this class.
void Unref() {
if (gpr_unref(&refs_)) {
Delete(this);
@@ -58,6 +70,11 @@ class RefCounted {
virtual ~RefCounted() {}
private:
+ // Allow RefCountedPtr<> to access IncrementRefCount().
+ friend class RefCountedPtr<Child>;
+
+ void IncrementRefCount() { gpr_ref(&refs_); }
+
gpr_refcount refs_;
};
@@ -67,20 +84,30 @@ class RefCounted {
// pointers and legacy code that is manually calling Ref() and Unref().
// Once all of our code is converted to idiomatic C++, we may be able to
// eliminate this class.
+template <typename Child>
class RefCountedWithTracing {
public:
- void Ref() { gpr_ref(&refs_); }
+ RefCountedPtr<Child> Ref() GRPC_MUST_USE_RESULT {
+ IncrementRefCount();
+ return RefCountedPtr<Child>(static_cast<Child*>(this));
+ }
- void Ref(const DebugLocation& location, const char* reason) {
+ RefCountedPtr<Child> Ref(const DebugLocation& location,
+ const char* reason) GRPC_MUST_USE_RESULT {
if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) {
gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count);
gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s",
trace_flag_->name(), this, location.file(), location.line(),
old_refs, old_refs + 1, reason);
}
- Ref();
+ return Ref();
}
+ // TODO(roth): Once all of our code is converted to C++ and can use
+ // RefCountedPtr<> instead of manual ref-counting, make the Unref() methods
+ // private, since they will only be used by RefCountedPtr<>, which is a
+ // friend of this class.
+
void Unref() {
if (gpr_unref(&refs_)) {
Delete(this);
@@ -124,6 +151,11 @@ class RefCountedWithTracing {
virtual ~RefCountedWithTracing() {}
private:
+ // Allow RefCountedPtr<> to access IncrementRefCount().
+ friend class RefCountedPtr<Child>;
+
+ void IncrementRefCount() { gpr_ref(&refs_); }
+
TraceFlag* trace_flag_ = nullptr;
gpr_refcount refs_;
};