diff options
author | Mark D. Roth <roth@google.com> | 2017-11-29 07:37:41 -0800 |
---|---|---|
committer | Mark D. Roth <roth@google.com> | 2017-11-29 07:37:41 -0800 |
commit | 853fff8d085e60ca56dca834c962e840fe5ad50a (patch) | |
tree | 5b3e83981f44b61bfcd756517ac1d90a399ed036 /src/core/lib | |
parent | 2e1912374057355de4f4e0ceaff02c89d45f43e5 (diff) |
Split tracing code into its own class.
Diffstat (limited to 'src/core/lib')
-rw-r--r-- | src/core/lib/support/reference_counted.h | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/src/core/lib/support/reference_counted.h b/src/core/lib/support/reference_counted.h index ff74de082c..3858d61b72 100644 --- a/src/core/lib/support/reference_counted.h +++ b/src/core/lib/support/reference_counted.h @@ -19,8 +19,8 @@ #ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H #define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H -#include <grpc/support/sync.h> #include <grpc/support/log.h> +#include <grpc/support/sync.h> #include "src/core/lib/debug/trace.h" #include "src/core/lib/support/debug_location.h" @@ -35,6 +35,39 @@ class ReferenceCounted { public: void Ref() { gpr_ref(&refs_); } + void Unref() { + if (gpr_unref(&refs_)) { + Delete(this); + } + } + + // Not copyable nor movable. + ReferenceCounted(const ReferenceCounted&) = delete; + ReferenceCounted& operator=(const ReferenceCounted&) = delete; + + protected: + // Allow Delete() to access destructor. + template <typename T> + friend void Delete(T*); + + ReferenceCounted() { gpr_ref_init(&refs_, 1); } + + virtual ~ReferenceCounted() {} + + private: + gpr_refcount refs_; +}; + +// An alternative version of the ReferenceCounted base class that +// supports tracing. This is intended to be used in cases where the +// object will be handled both by idiomatic C++ code using smart +// 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. +class ReferenceCountedWithTracing { + public: + void Ref() { gpr_ref(&refs_); } + void Ref(const DebugLocation& location, const char* reason) { if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) { gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count); @@ -62,21 +95,23 @@ class ReferenceCounted { } // Not copyable nor movable. - ReferenceCounted(const ReferenceCounted&) = delete; - ReferenceCounted& operator=(const ReferenceCounted&) = delete; + ReferenceCountedWithTracing(const ReferenceCountedWithTracing&) = delete; + ReferenceCountedWithTracing& operator=(const ReferenceCountedWithTracing&) = + delete; protected: // Allow Delete() to access destructor. template <typename T> friend void Delete(T*); - ReferenceCounted() : ReferenceCounted(nullptr) {} + ReferenceCountedWithTracing() : ReferenceCountedWithTracing(nullptr) {} - explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) { + explicit ReferenceCountedWithTracing(TraceFlag* trace_flag) + : trace_flag_(trace_flag) { gpr_ref_init(&refs_, 1); } - virtual ~ReferenceCounted() {} + virtual ~ReferenceCountedWithTracing() {} private: TraceFlag* trace_flag_ = nullptr; |