aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Mark D. Roth <roth@google.com>2017-11-27 14:53:26 -0800
committerGravatar Mark D. Roth <roth@google.com>2017-11-27 14:53:26 -0800
commit70db663ae8773dc45464c904603a3e73045b45c5 (patch)
treedc7b4b650ed6e898f8cf255237dc3f6f4b04eaeb
parentde93112a3f70afa39d3e9aa87da165f9f737fdef (diff)
Add ReferenceCounted base class.
-rw-r--r--BUILD17
-rw-r--r--CMakeLists.txt42
-rw-r--r--Makefile49
-rw-r--r--build.yaml17
-rw-r--r--config.m41
-rw-r--r--config.w321
-rw-r--r--gRPC-Core.podspec5
-rw-r--r--grpc.gemspec3
-rw-r--r--grpc.gyp1
-rw-r--r--package.xml3
-rw-r--r--src/core/lib/support/debug_location.h48
-rw-r--r--src/core/lib/support/reference_counted.cc58
-rw-r--r--src/core/lib/support/reference_counted.h55
-rw-r--r--src/python/grpcio/grpc_core_dependencies.py1
-rw-r--r--test/core/support/BUILD13
-rw-r--r--test/core/support/reference_counted_test.cc60
-rw-r--r--tools/doxygen/Doxyfile.c++.internal2
-rw-r--r--tools/doxygen/Doxyfile.core.internal3
-rw-r--r--tools/run_tests/generated/sources_and_headers.json24
-rw-r--r--tools/run_tests/generated/tests.json24
20 files changed, 427 insertions, 0 deletions
diff --git a/BUILD b/BUILD
index 25daba8e1e..5289023ef5 100644
--- a/BUILD
+++ b/BUILD
@@ -538,6 +538,23 @@ grpc_cc_library(
)
grpc_cc_library(
+ name = "debug_location",
+ public_hdrs = ["src/core/lib/support/debug_location.h"],
+ language = "c++",
+)
+
+grpc_cc_library(
+ name = "reference_counted",
+ srcs = ["src/core/lib/support/reference_counted.cc"],
+ public_hdrs = ["src/core/lib/support/reference_counted.h"],
+ language = "c++",
+ deps = [
+ "grpc_trace",
+ "debug_location",
+ ],
+)
+
+grpc_cc_library(
name = "grpc_base_c",
srcs = [
"src/core/lib/backoff/backoff.cc",
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 77b60e898a..ca59c84e50 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -739,6 +739,7 @@ endif()
add_dependencies(buildtests_cxx qps_worker)
add_dependencies(buildtests_cxx reconnect_interop_client)
add_dependencies(buildtests_cxx reconnect_interop_server)
+add_dependencies(buildtests_cxx reference_counted_test)
add_dependencies(buildtests_cxx secure_auth_context_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
add_dependencies(buildtests_cxx secure_sync_unary_ping_pong_test)
@@ -807,6 +808,7 @@ add_library(gpr
src/core/lib/support/log_windows.cc
src/core/lib/support/mpscq.cc
src/core/lib/support/murmur_hash.cc
+ src/core/lib/support/reference_counted.cc
src/core/lib/support/stack_lockfree.cc
src/core/lib/support/string.cc
src/core/lib/support/string_posix.cc
@@ -12260,6 +12262,46 @@ target_link_libraries(reconnect_interop_server
endif (gRPC_BUILD_TESTS)
if (gRPC_BUILD_TESTS)
+add_executable(reference_counted_test
+ test/core/support/reference_counted_test.cc
+ third_party/googletest/googletest/src/gtest-all.cc
+ third_party/googletest/googlemock/src/gmock-all.cc
+)
+
+
+target_include_directories(reference_counted_test
+ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
+ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
+ PRIVATE ${BORINGSSL_ROOT_DIR}/include
+ PRIVATE ${PROTOBUF_ROOT_DIR}/src
+ PRIVATE ${BENCHMARK_ROOT_DIR}/include
+ PRIVATE ${ZLIB_ROOT_DIR}
+ PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib
+ PRIVATE ${CARES_INCLUDE_DIR}
+ PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/cares/cares
+ PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include
+ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/abseil-cpp
+ PRIVATE third_party/googletest/googletest/include
+ PRIVATE third_party/googletest/googletest
+ PRIVATE third_party/googletest/googlemock/include
+ PRIVATE third_party/googletest/googlemock
+ PRIVATE ${_gRPC_PROTO_GENS_DIR}
+)
+
+target_link_libraries(reference_counted_test
+ ${_gRPC_PROTOBUF_LIBRARIES}
+ ${_gRPC_ALLTARGETS_LIBRARIES}
+ grpc_test_util
+ grpc++
+ grpc
+ gpr_test_util
+ gpr
+ ${_gRPC_GFLAGS_LIBRARIES}
+)
+
+endif (gRPC_BUILD_TESTS)
+if (gRPC_BUILD_TESTS)
+
add_executable(secure_auth_context_test
test/cpp/common/secure_auth_context_test.cc
third_party/googletest/googletest/src/gtest-all.cc
diff --git a/Makefile b/Makefile
index c85b1bca73..0e8437a05c 100644
--- a/Makefile
+++ b/Makefile
@@ -1165,6 +1165,7 @@ qps_openloop_test: $(BINDIR)/$(CONFIG)/qps_openloop_test
qps_worker: $(BINDIR)/$(CONFIG)/qps_worker
reconnect_interop_client: $(BINDIR)/$(CONFIG)/reconnect_interop_client
reconnect_interop_server: $(BINDIR)/$(CONFIG)/reconnect_interop_server
+reference_counted_test: $(BINDIR)/$(CONFIG)/reference_counted_test
secure_auth_context_test: $(BINDIR)/$(CONFIG)/secure_auth_context_test
secure_sync_unary_ping_pong_test: $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test
server_builder_plugin_test: $(BINDIR)/$(CONFIG)/server_builder_plugin_test
@@ -1601,6 +1602,7 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/qps_worker \
$(BINDIR)/$(CONFIG)/reconnect_interop_client \
$(BINDIR)/$(CONFIG)/reconnect_interop_server \
+ $(BINDIR)/$(CONFIG)/reference_counted_test \
$(BINDIR)/$(CONFIG)/secure_auth_context_test \
$(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \
$(BINDIR)/$(CONFIG)/server_builder_plugin_test \
@@ -1727,6 +1729,7 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/qps_worker \
$(BINDIR)/$(CONFIG)/reconnect_interop_client \
$(BINDIR)/$(CONFIG)/reconnect_interop_server \
+ $(BINDIR)/$(CONFIG)/reference_counted_test \
$(BINDIR)/$(CONFIG)/secure_auth_context_test \
$(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \
$(BINDIR)/$(CONFIG)/server_builder_plugin_test \
@@ -2130,6 +2133,8 @@ test_cxx: buildtests_cxx
$(Q) $(BINDIR)/$(CONFIG)/proto_utils_test || ( echo test proto_utils_test failed ; exit 1 )
$(E) "[RUN] Testing qps_openloop_test"
$(Q) $(BINDIR)/$(CONFIG)/qps_openloop_test || ( echo test qps_openloop_test failed ; exit 1 )
+ $(E) "[RUN] Testing reference_counted_test"
+ $(Q) $(BINDIR)/$(CONFIG)/reference_counted_test || ( echo test reference_counted_test failed ; exit 1 )
$(E) "[RUN] Testing secure_auth_context_test"
$(Q) $(BINDIR)/$(CONFIG)/secure_auth_context_test || ( echo test secure_auth_context_test failed ; exit 1 )
$(E) "[RUN] Testing secure_sync_unary_ping_pong_test"
@@ -2836,6 +2841,7 @@ LIBGPR_SRC = \
src/core/lib/support/log_windows.cc \
src/core/lib/support/mpscq.cc \
src/core/lib/support/murmur_hash.cc \
+ src/core/lib/support/reference_counted.cc \
src/core/lib/support/stack_lockfree.cc \
src/core/lib/support/string.cc \
src/core/lib/support/string_posix.cc \
@@ -16495,6 +16501,49 @@ endif
$(OBJDIR)/$(CONFIG)/test/cpp/interop/reconnect_interop_server.o: $(GENDIR)/src/proto/grpc/testing/empty.pb.cc $(GENDIR)/src/proto/grpc/testing/empty.grpc.pb.cc $(GENDIR)/src/proto/grpc/testing/messages.pb.cc $(GENDIR)/src/proto/grpc/testing/messages.grpc.pb.cc $(GENDIR)/src/proto/grpc/testing/test.pb.cc $(GENDIR)/src/proto/grpc/testing/test.grpc.pb.cc
+REFERENCE_COUNTED_TEST_SRC = \
+ test/core/support/reference_counted_test.cc \
+
+REFERENCE_COUNTED_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(REFERENCE_COUNTED_TEST_SRC))))
+ifeq ($(NO_SECURE),true)
+
+# You can't build secure targets if you don't have OpenSSL.
+
+$(BINDIR)/$(CONFIG)/reference_counted_test: openssl_dep_error
+
+else
+
+
+
+
+ifeq ($(NO_PROTOBUF),true)
+
+# You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+.
+
+$(BINDIR)/$(CONFIG)/reference_counted_test: protobuf_dep_error
+
+else
+
+$(BINDIR)/$(CONFIG)/reference_counted_test: $(PROTOBUF_DEP) $(REFERENCE_COUNTED_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
+ $(E) "[LD] Linking $@"
+ $(Q) mkdir -p `dirname $@`
+ $(Q) $(LDXX) $(LDFLAGS) $(REFERENCE_COUNTED_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/reference_counted_test
+
+endif
+
+endif
+
+$(OBJDIR)/$(CONFIG)/test/core/support/reference_counted_test.o: $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
+
+deps_reference_counted_test: $(REFERENCE_COUNTED_TEST_OBJS:.o=.dep)
+
+ifneq ($(NO_SECURE),true)
+ifneq ($(NO_DEPS),true)
+-include $(REFERENCE_COUNTED_TEST_OBJS:.o=.dep)
+endif
+endif
+
+
SECURE_AUTH_CONTEXT_TEST_SRC = \
test/cpp/common/secure_auth_context_test.cc \
diff --git a/build.yaml b/build.yaml
index 4f6194ecc4..2903b866dc 100644
--- a/build.yaml
+++ b/build.yaml
@@ -49,6 +49,7 @@ filegroups:
- src/core/lib/support/log_windows.cc
- src/core/lib/support/mpscq.cc
- src/core/lib/support/murmur_hash.cc
+ - src/core/lib/support/reference_counted.cc
- src/core/lib/support/stack_lockfree.cc
- src/core/lib/support/string.cc
- src/core/lib/support/string_posix.cc
@@ -109,11 +110,13 @@ filegroups:
- src/core/lib/support/atomic.h
- src/core/lib/support/atomic_with_atm.h
- src/core/lib/support/atomic_with_std.h
+ - src/core/lib/support/debug_location.h
- src/core/lib/support/env.h
- src/core/lib/support/manual_constructor.h
- src/core/lib/support/memory.h
- src/core/lib/support/mpscq.h
- src/core/lib/support/murmur_hash.h
+ - src/core/lib/support/reference_counted.h
- src/core/lib/support/spinlock.h
- src/core/lib/support/stack_lockfree.h
- src/core/lib/support/string.h
@@ -4541,6 +4544,20 @@ targets:
- gpr_test_util
- gpr
- grpc++_test_config
+- name: reference_counted_test
+ gtest: true
+ build: test
+ language: c++
+ src:
+ - test/core/support/reference_counted_test.cc
+ deps:
+ - grpc_test_util
+ - grpc++
+ - grpc
+ - gpr_test_util
+ - gpr
+ uses:
+ - grpc++_test
- name: secure_auth_context_test
gtest: true
build: test
diff --git a/config.m4 b/config.m4
index d2f2520fea..9d514c8552 100644
--- a/config.m4
+++ b/config.m4
@@ -62,6 +62,7 @@ if test "$PHP_GRPC" != "no"; then
src/core/lib/support/log_windows.cc \
src/core/lib/support/mpscq.cc \
src/core/lib/support/murmur_hash.cc \
+ src/core/lib/support/reference_counted.cc \
src/core/lib/support/stack_lockfree.cc \
src/core/lib/support/string.cc \
src/core/lib/support/string_posix.cc \
diff --git a/config.w32 b/config.w32
index 8a713751dc..2f24fcd8b0 100644
--- a/config.w32
+++ b/config.w32
@@ -39,6 +39,7 @@ if (PHP_GRPC != "no") {
"src\\core\\lib\\support\\log_windows.cc " +
"src\\core\\lib\\support\\mpscq.cc " +
"src\\core\\lib\\support\\murmur_hash.cc " +
+ "src\\core\\lib\\support\\reference_counted.cc " +
"src\\core\\lib\\support\\stack_lockfree.cc " +
"src\\core\\lib\\support\\string.cc " +
"src\\core\\lib\\support\\string_posix.cc " +
diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec
index 2f97565f1b..71e13633ae 100644
--- a/gRPC-Core.podspec
+++ b/gRPC-Core.podspec
@@ -192,11 +192,13 @@ Pod::Spec.new do |s|
'src/core/lib/support/atomic.h',
'src/core/lib/support/atomic_with_atm.h',
'src/core/lib/support/atomic_with_std.h',
+ 'src/core/lib/support/debug_location.h',
'src/core/lib/support/env.h',
'src/core/lib/support/manual_constructor.h',
'src/core/lib/support/memory.h',
'src/core/lib/support/mpscq.h',
'src/core/lib/support/murmur_hash.h',
+ 'src/core/lib/support/reference_counted.h',
'src/core/lib/support/spinlock.h',
'src/core/lib/support/stack_lockfree.h',
'src/core/lib/support/string.h',
@@ -226,6 +228,7 @@ Pod::Spec.new do |s|
'src/core/lib/support/log_windows.cc',
'src/core/lib/support/mpscq.cc',
'src/core/lib/support/murmur_hash.cc',
+ 'src/core/lib/support/reference_counted.cc',
'src/core/lib/support/stack_lockfree.cc',
'src/core/lib/support/string.cc',
'src/core/lib/support/string_posix.cc',
@@ -712,11 +715,13 @@ Pod::Spec.new do |s|
'src/core/lib/support/atomic.h',
'src/core/lib/support/atomic_with_atm.h',
'src/core/lib/support/atomic_with_std.h',
+ 'src/core/lib/support/debug_location.h',
'src/core/lib/support/env.h',
'src/core/lib/support/manual_constructor.h',
'src/core/lib/support/memory.h',
'src/core/lib/support/mpscq.h',
'src/core/lib/support/murmur_hash.h',
+ 'src/core/lib/support/reference_counted.h',
'src/core/lib/support/spinlock.h',
'src/core/lib/support/stack_lockfree.h',
'src/core/lib/support/string.h',
diff --git a/grpc.gemspec b/grpc.gemspec
index 0dd7ceb350..900f4afe7a 100644
--- a/grpc.gemspec
+++ b/grpc.gemspec
@@ -89,11 +89,13 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/support/atomic.h )
s.files += %w( src/core/lib/support/atomic_with_atm.h )
s.files += %w( src/core/lib/support/atomic_with_std.h )
+ s.files += %w( src/core/lib/support/debug_location.h )
s.files += %w( src/core/lib/support/env.h )
s.files += %w( src/core/lib/support/manual_constructor.h )
s.files += %w( src/core/lib/support/memory.h )
s.files += %w( src/core/lib/support/mpscq.h )
s.files += %w( src/core/lib/support/murmur_hash.h )
+ s.files += %w( src/core/lib/support/reference_counted.h )
s.files += %w( src/core/lib/support/spinlock.h )
s.files += %w( src/core/lib/support/stack_lockfree.h )
s.files += %w( src/core/lib/support/string.h )
@@ -123,6 +125,7 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/support/log_windows.cc )
s.files += %w( src/core/lib/support/mpscq.cc )
s.files += %w( src/core/lib/support/murmur_hash.cc )
+ s.files += %w( src/core/lib/support/reference_counted.cc )
s.files += %w( src/core/lib/support/stack_lockfree.cc )
s.files += %w( src/core/lib/support/string.cc )
s.files += %w( src/core/lib/support/string_posix.cc )
diff --git a/grpc.gyp b/grpc.gyp
index fb153915ff..0ee6529013 100644
--- a/grpc.gyp
+++ b/grpc.gyp
@@ -184,6 +184,7 @@
'src/core/lib/support/log_windows.cc',
'src/core/lib/support/mpscq.cc',
'src/core/lib/support/murmur_hash.cc',
+ 'src/core/lib/support/reference_counted.cc',
'src/core/lib/support/stack_lockfree.cc',
'src/core/lib/support/string.cc',
'src/core/lib/support/string_posix.cc',
diff --git a/package.xml b/package.xml
index 59d49dd165..c0d9b49828 100644
--- a/package.xml
+++ b/package.xml
@@ -101,11 +101,13 @@
<file baseinstalldir="/" name="src/core/lib/support/atomic.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/atomic_with_atm.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/atomic_with_std.h" role="src" />
+ <file baseinstalldir="/" name="src/core/lib/support/debug_location.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/env.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/manual_constructor.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/memory.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/mpscq.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/murmur_hash.h" role="src" />
+ <file baseinstalldir="/" name="src/core/lib/support/reference_counted.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/spinlock.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/stack_lockfree.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/string.h" role="src" />
@@ -135,6 +137,7 @@
<file baseinstalldir="/" name="src/core/lib/support/log_windows.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/mpscq.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/murmur_hash.cc" role="src" />
+ <file baseinstalldir="/" name="src/core/lib/support/reference_counted.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/stack_lockfree.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/string.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/string_posix.cc" role="src" />
diff --git a/src/core/lib/support/debug_location.h b/src/core/lib/support/debug_location.h
new file mode 100644
index 0000000000..9260752d5e
--- /dev/null
+++ b/src/core/lib/support/debug_location.h
@@ -0,0 +1,48 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
+#define GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
+
+namespace grpc_core {
+
+#ifndef NDEBUG
+class DebugLocation {
+ public:
+ DebugLocation(const char* file, int line) : file_(file), line_(line) {}
+ bool Log() const { return true; }
+ const char* file() const { return file_; }
+ int line() const { return line_; }
+ private:
+ const char* file_;
+ const int line_;
+};
+#define DEBUG_LOCATION DebugLocation(__FILE__, __LINE__)
+#else
+class DebugLocation {
+ public:
+ bool Log() const { return false; }
+ const char* file() const { return nullptr; }
+ int line() const { return -1; }
+};
+#define DEBUG_LOCATION DebugLocation()
+#endif
+
+} // namespace grpc_core
+
+#endif // GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
diff --git a/src/core/lib/support/reference_counted.cc b/src/core/lib/support/reference_counted.cc
new file mode 100644
index 0000000000..853a29da2f
--- /dev/null
+++ b/src/core/lib/support/reference_counted.cc
@@ -0,0 +1,58 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "src/core/lib/support/reference_counted.h"
+
+#include <grpc/support/log.h>
+
+namespace grpc_core {
+
+void ReferenceCounted::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);
+ 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();
+}
+
+void ReferenceCounted::Ref() {
+ gpr_ref(&refs_);
+}
+
+bool ReferenceCounted::Unref(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);
+ gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s",
+ trace_flag_->name(), this, location.file(), location.line(),
+ old_refs, old_refs - 1, reason);
+ }
+ return Unref();
+}
+
+bool ReferenceCounted::Unref() {
+ if (gpr_unref(&refs_)) {
+ delete this;
+ return true;
+ }
+ return false;
+}
+
+} // namespace grpc_core
diff --git a/src/core/lib/support/reference_counted.h b/src/core/lib/support/reference_counted.h
new file mode 100644
index 0000000000..ebd620c553
--- /dev/null
+++ b/src/core/lib/support/reference_counted.h
@@ -0,0 +1,55 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H
+#define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H
+
+#include <grpc/support/sync.h>
+
+#include "src/core/lib/debug/trace.h"
+#include "src/core/lib/support/debug_location.h"
+
+namespace grpc_core {
+
+class ReferenceCounted {
+ public:
+ void Ref();
+ void Ref(const DebugLocation& location, const char* reason);
+
+ bool Unref();
+ bool Unref(const DebugLocation& location, const char* reason);
+
+ // Not copyable nor movable.
+ ReferenceCounted(const ReferenceCounted&) = delete;
+ ReferenceCounted& operator=(const ReferenceCounted&) = delete;
+
+ protected:
+ explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) {
+ gpr_ref_init(&refs_, 1);
+ }
+
+ virtual ~ReferenceCounted() {}
+
+ private:
+ TraceFlag* trace_flag_;
+ gpr_refcount refs_;
+};
+
+} // namespace grpc_core
+
+#endif // GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H
diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py
index 330c4185c6..38f2a40133 100644
--- a/src/python/grpcio/grpc_core_dependencies.py
+++ b/src/python/grpcio/grpc_core_dependencies.py
@@ -38,6 +38,7 @@ CORE_SOURCE_FILES = [
'src/core/lib/support/log_windows.cc',
'src/core/lib/support/mpscq.cc',
'src/core/lib/support/murmur_hash.cc',
+ 'src/core/lib/support/reference_counted.cc',
'src/core/lib/support/stack_lockfree.cc',
'src/core/lib/support/string.cc',
'src/core/lib/support/string_posix.cc',
diff --git a/test/core/support/BUILD b/test/core/support/BUILD
index 69512cd9a9..ee20ef8692 100644
--- a/test/core/support/BUILD
+++ b/test/core/support/BUILD
@@ -233,3 +233,16 @@ grpc_cc_test(
"gtest",
],
)
+
+grpc_cc_test(
+ name = "reference_counted_test",
+ srcs = ["reference_counted_test.cc"],
+ language = "C++",
+ deps = [
+ "//:reference_counted",
+ "//test/core/util:gpr_test_util",
+ ],
+ external_deps = [
+ "gtest",
+ ],
+)
diff --git a/test/core/support/reference_counted_test.cc b/test/core/support/reference_counted_test.cc
new file mode 100644
index 0000000000..119fc1b600
--- /dev/null
+++ b/test/core/support/reference_counted_test.cc
@@ -0,0 +1,60 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "src/core/lib/support/reference_counted.h"
+#include <gtest/gtest.h>
+#include "test/core/util/test_config.h"
+
+namespace grpc_core {
+namespace testing {
+
+class Foo : public ReferenceCounted {
+ public:
+ Foo() : ReferenceCounted(nullptr) {}
+};
+
+TEST(ReferenceCounted, StackAllocated) {
+ Foo foo;
+}
+
+TEST(ReferenceCounted, StackAllocatedWithExtraRef) {
+ Foo foo;
+ foo.Ref();
+ foo.Unref();
+}
+
+TEST(ReferenceCounted, HeapAllocated) {
+ Foo* foo = new Foo();
+ foo->Unref();
+}
+
+TEST(ReferenceCounted, HeapAllocatedWithExtraRef) {
+ Foo* foo = new Foo();
+ foo->Ref();
+ foo->Unref();
+ foo->Unref();
+}
+
+} // namespace testing
+} // namespace grpc_core
+
+int main(int argc, char** argv) {
+ grpc_test_init(argc, argv);
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal
index 3c564e203a..1d84dcba33 100644
--- a/tools/doxygen/Doxyfile.c++.internal
+++ b/tools/doxygen/Doxyfile.c++.internal
@@ -1031,11 +1031,13 @@ src/core/lib/support/arena.h \
src/core/lib/support/atomic.h \
src/core/lib/support/atomic_with_atm.h \
src/core/lib/support/atomic_with_std.h \
+src/core/lib/support/debug_location.h \
src/core/lib/support/env.h \
src/core/lib/support/manual_constructor.h \
src/core/lib/support/memory.h \
src/core/lib/support/mpscq.h \
src/core/lib/support/murmur_hash.h \
+src/core/lib/support/reference_counted.h \
src/core/lib/support/spinlock.h \
src/core/lib/support/stack_lockfree.h \
src/core/lib/support/string.h \
diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal
index 5674124f44..5b289e7bc4 100644
--- a/tools/doxygen/Doxyfile.core.internal
+++ b/tools/doxygen/Doxyfile.core.internal
@@ -1281,6 +1281,7 @@ src/core/lib/support/cpu_iphone.cc \
src/core/lib/support/cpu_linux.cc \
src/core/lib/support/cpu_posix.cc \
src/core/lib/support/cpu_windows.cc \
+src/core/lib/support/debug_location.h \
src/core/lib/support/env.h \
src/core/lib/support/env_linux.cc \
src/core/lib/support/env_posix.cc \
@@ -1298,6 +1299,8 @@ src/core/lib/support/mpscq.cc \
src/core/lib/support/mpscq.h \
src/core/lib/support/murmur_hash.cc \
src/core/lib/support/murmur_hash.h \
+src/core/lib/support/reference_counted.cc \
+src/core/lib/support/reference_counted.h \
src/core/lib/support/spinlock.h \
src/core/lib/support/stack_lockfree.cc \
src/core/lib/support/stack_lockfree.h \
diff --git a/tools/run_tests/generated/sources_and_headers.json b/tools/run_tests/generated/sources_and_headers.json
index 5a3429c81f..8ec9619874 100644
--- a/tools/run_tests/generated/sources_and_headers.json
+++ b/tools/run_tests/generated/sources_and_headers.json
@@ -3923,6 +3923,25 @@
"gpr_test_util",
"grpc",
"grpc++",
+ "grpc++_test",
+ "grpc_test_util"
+ ],
+ "headers": [],
+ "is_filegroup": false,
+ "language": "c++",
+ "name": "reference_counted_test",
+ "src": [
+ "test/core/support/reference_counted_test.cc"
+ ],
+ "third_party": false,
+ "type": "target"
+ },
+ {
+ "deps": [
+ "gpr",
+ "gpr_test_util",
+ "grpc",
+ "grpc++",
"grpc++_test_util",
"grpc_test_util"
],
@@ -7770,6 +7789,7 @@
"src/core/lib/support/log_windows.cc",
"src/core/lib/support/mpscq.cc",
"src/core/lib/support/murmur_hash.cc",
+ "src/core/lib/support/reference_counted.cc",
"src/core/lib/support/stack_lockfree.cc",
"src/core/lib/support/string.cc",
"src/core/lib/support/string_posix.cc",
@@ -7834,11 +7854,13 @@
"src/core/lib/support/atomic.h",
"src/core/lib/support/atomic_with_atm.h",
"src/core/lib/support/atomic_with_std.h",
+ "src/core/lib/support/debug_location.h",
"src/core/lib/support/env.h",
"src/core/lib/support/manual_constructor.h",
"src/core/lib/support/memory.h",
"src/core/lib/support/mpscq.h",
"src/core/lib/support/murmur_hash.h",
+ "src/core/lib/support/reference_counted.h",
"src/core/lib/support/spinlock.h",
"src/core/lib/support/stack_lockfree.h",
"src/core/lib/support/string.h",
@@ -7883,11 +7905,13 @@
"src/core/lib/support/atomic.h",
"src/core/lib/support/atomic_with_atm.h",
"src/core/lib/support/atomic_with_std.h",
+ "src/core/lib/support/debug_location.h",
"src/core/lib/support/env.h",
"src/core/lib/support/manual_constructor.h",
"src/core/lib/support/memory.h",
"src/core/lib/support/mpscq.h",
"src/core/lib/support/murmur_hash.h",
+ "src/core/lib/support/reference_counted.h",
"src/core/lib/support/spinlock.h",
"src/core/lib/support/stack_lockfree.h",
"src/core/lib/support/string.h",
diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json
index dfd7bfafe6..a6b58d8ba6 100644
--- a/tools/run_tests/generated/tests.json
+++ b/tools/run_tests/generated/tests.json
@@ -4138,6 +4138,30 @@
"flaky": false,
"gtest": true,
"language": "c++",
+ "name": "reference_counted_test",
+ "platforms": [
+ "linux",
+ "mac",
+ "posix",
+ "windows"
+ ],
+ "uses_polling": true
+ },
+ {
+ "args": [],
+ "benchmark": false,
+ "ci_platforms": [
+ "linux",
+ "mac",
+ "posix",
+ "windows"
+ ],
+ "cpu_cost": 1.0,
+ "exclude_configs": [],
+ "exclude_iomgrs": [],
+ "flaky": false,
+ "gtest": true,
+ "language": "c++",
"name": "secure_auth_context_test",
"platforms": [
"linux",