Add ReferenceCounted base class.

pull/13357/head
Mark D. Roth 7 years ago
parent de93112a3f
commit 70db663ae8
  1. 17
      BUILD
  2. 42
      CMakeLists.txt
  3. 49
      Makefile
  4. 17
      build.yaml
  5. 1
      config.m4
  6. 1
      config.w32
  7. 5
      gRPC-Core.podspec
  8. 3
      grpc.gemspec
  9. 1
      grpc.gyp
  10. 3
      package.xml
  11. 48
      src/core/lib/support/debug_location.h
  12. 58
      src/core/lib/support/reference_counted.cc
  13. 55
      src/core/lib/support/reference_counted.h
  14. 1
      src/python/grpcio/grpc_core_dependencies.py
  15. 13
      test/core/support/BUILD
  16. 60
      test/core/support/reference_counted_test.cc
  17. 2
      tools/doxygen/Doxyfile.c++.internal
  18. 3
      tools/doxygen/Doxyfile.core.internal
  19. 24
      tools/run_tests/generated/sources_and_headers.json
  20. 24
      tools/run_tests/generated/tests.json

17
BUILD

@ -537,6 +537,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( grpc_cc_library(
name = "grpc_base_c", name = "grpc_base_c",
srcs = [ srcs = [

@ -739,6 +739,7 @@ endif()
add_dependencies(buildtests_cxx qps_worker) add_dependencies(buildtests_cxx qps_worker)
add_dependencies(buildtests_cxx reconnect_interop_client) add_dependencies(buildtests_cxx reconnect_interop_client)
add_dependencies(buildtests_cxx reconnect_interop_server) add_dependencies(buildtests_cxx reconnect_interop_server)
add_dependencies(buildtests_cxx reference_counted_test)
add_dependencies(buildtests_cxx secure_auth_context_test) add_dependencies(buildtests_cxx secure_auth_context_test)
if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX) if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
add_dependencies(buildtests_cxx secure_sync_unary_ping_pong_test) 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/log_windows.cc
src/core/lib/support/mpscq.cc src/core/lib/support/mpscq.cc
src/core/lib/support/murmur_hash.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/stack_lockfree.cc
src/core/lib/support/string.cc src/core/lib/support/string.cc
src/core/lib/support/string_posix.cc src/core/lib/support/string_posix.cc
@ -12260,6 +12262,46 @@ target_link_libraries(reconnect_interop_server
endif (gRPC_BUILD_TESTS) endif (gRPC_BUILD_TESTS)
if (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 add_executable(secure_auth_context_test
test/cpp/common/secure_auth_context_test.cc test/cpp/common/secure_auth_context_test.cc
third_party/googletest/googletest/src/gtest-all.cc third_party/googletest/googletest/src/gtest-all.cc

@ -1165,6 +1165,7 @@ qps_openloop_test: $(BINDIR)/$(CONFIG)/qps_openloop_test
qps_worker: $(BINDIR)/$(CONFIG)/qps_worker qps_worker: $(BINDIR)/$(CONFIG)/qps_worker
reconnect_interop_client: $(BINDIR)/$(CONFIG)/reconnect_interop_client reconnect_interop_client: $(BINDIR)/$(CONFIG)/reconnect_interop_client
reconnect_interop_server: $(BINDIR)/$(CONFIG)/reconnect_interop_server 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_auth_context_test: $(BINDIR)/$(CONFIG)/secure_auth_context_test
secure_sync_unary_ping_pong_test: $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_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 server_builder_plugin_test: $(BINDIR)/$(CONFIG)/server_builder_plugin_test
@ -1601,6 +1602,7 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/qps_worker \ $(BINDIR)/$(CONFIG)/qps_worker \
$(BINDIR)/$(CONFIG)/reconnect_interop_client \ $(BINDIR)/$(CONFIG)/reconnect_interop_client \
$(BINDIR)/$(CONFIG)/reconnect_interop_server \ $(BINDIR)/$(CONFIG)/reconnect_interop_server \
$(BINDIR)/$(CONFIG)/reference_counted_test \
$(BINDIR)/$(CONFIG)/secure_auth_context_test \ $(BINDIR)/$(CONFIG)/secure_auth_context_test \
$(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \ $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \
$(BINDIR)/$(CONFIG)/server_builder_plugin_test \ $(BINDIR)/$(CONFIG)/server_builder_plugin_test \
@ -1727,6 +1729,7 @@ buildtests_cxx: privatelibs_cxx \
$(BINDIR)/$(CONFIG)/qps_worker \ $(BINDIR)/$(CONFIG)/qps_worker \
$(BINDIR)/$(CONFIG)/reconnect_interop_client \ $(BINDIR)/$(CONFIG)/reconnect_interop_client \
$(BINDIR)/$(CONFIG)/reconnect_interop_server \ $(BINDIR)/$(CONFIG)/reconnect_interop_server \
$(BINDIR)/$(CONFIG)/reference_counted_test \
$(BINDIR)/$(CONFIG)/secure_auth_context_test \ $(BINDIR)/$(CONFIG)/secure_auth_context_test \
$(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \ $(BINDIR)/$(CONFIG)/secure_sync_unary_ping_pong_test \
$(BINDIR)/$(CONFIG)/server_builder_plugin_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 ) $(Q) $(BINDIR)/$(CONFIG)/proto_utils_test || ( echo test proto_utils_test failed ; exit 1 )
$(E) "[RUN] Testing qps_openloop_test" $(E) "[RUN] Testing qps_openloop_test"
$(Q) $(BINDIR)/$(CONFIG)/qps_openloop_test || ( echo test qps_openloop_test failed ; exit 1 ) $(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" $(E) "[RUN] Testing secure_auth_context_test"
$(Q) $(BINDIR)/$(CONFIG)/secure_auth_context_test || ( echo test secure_auth_context_test failed ; exit 1 ) $(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" $(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/log_windows.cc \
src/core/lib/support/mpscq.cc \ src/core/lib/support/mpscq.cc \
src/core/lib/support/murmur_hash.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/stack_lockfree.cc \
src/core/lib/support/string.cc \ src/core/lib/support/string.cc \
src/core/lib/support/string_posix.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 $(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 = \ SECURE_AUTH_CONTEXT_TEST_SRC = \
test/cpp/common/secure_auth_context_test.cc \ test/cpp/common/secure_auth_context_test.cc \

@ -49,6 +49,7 @@ filegroups:
- src/core/lib/support/log_windows.cc - src/core/lib/support/log_windows.cc
- src/core/lib/support/mpscq.cc - src/core/lib/support/mpscq.cc
- src/core/lib/support/murmur_hash.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/stack_lockfree.cc
- src/core/lib/support/string.cc - src/core/lib/support/string.cc
- src/core/lib/support/string_posix.cc - src/core/lib/support/string_posix.cc
@ -109,11 +110,13 @@ filegroups:
- src/core/lib/support/atomic.h - src/core/lib/support/atomic.h
- src/core/lib/support/atomic_with_atm.h - src/core/lib/support/atomic_with_atm.h
- src/core/lib/support/atomic_with_std.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/env.h
- src/core/lib/support/manual_constructor.h - src/core/lib/support/manual_constructor.h
- src/core/lib/support/memory.h - src/core/lib/support/memory.h
- src/core/lib/support/mpscq.h - src/core/lib/support/mpscq.h
- src/core/lib/support/murmur_hash.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/spinlock.h
- src/core/lib/support/stack_lockfree.h - src/core/lib/support/stack_lockfree.h
- src/core/lib/support/string.h - src/core/lib/support/string.h
@ -4541,6 +4544,20 @@ targets:
- gpr_test_util - gpr_test_util
- gpr - gpr
- grpc++_test_config - 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 - name: secure_auth_context_test
gtest: true gtest: true
build: test build: test

@ -62,6 +62,7 @@ if test "$PHP_GRPC" != "no"; then
src/core/lib/support/log_windows.cc \ src/core/lib/support/log_windows.cc \
src/core/lib/support/mpscq.cc \ src/core/lib/support/mpscq.cc \
src/core/lib/support/murmur_hash.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/stack_lockfree.cc \
src/core/lib/support/string.cc \ src/core/lib/support/string.cc \
src/core/lib/support/string_posix.cc \ src/core/lib/support/string_posix.cc \

@ -39,6 +39,7 @@ if (PHP_GRPC != "no") {
"src\\core\\lib\\support\\log_windows.cc " + "src\\core\\lib\\support\\log_windows.cc " +
"src\\core\\lib\\support\\mpscq.cc " + "src\\core\\lib\\support\\mpscq.cc " +
"src\\core\\lib\\support\\murmur_hash.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\\stack_lockfree.cc " +
"src\\core\\lib\\support\\string.cc " + "src\\core\\lib\\support\\string.cc " +
"src\\core\\lib\\support\\string_posix.cc " + "src\\core\\lib\\support\\string_posix.cc " +

@ -192,11 +192,13 @@ Pod::Spec.new do |s|
'src/core/lib/support/atomic.h', 'src/core/lib/support/atomic.h',
'src/core/lib/support/atomic_with_atm.h', 'src/core/lib/support/atomic_with_atm.h',
'src/core/lib/support/atomic_with_std.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/env.h',
'src/core/lib/support/manual_constructor.h', 'src/core/lib/support/manual_constructor.h',
'src/core/lib/support/memory.h', 'src/core/lib/support/memory.h',
'src/core/lib/support/mpscq.h', 'src/core/lib/support/mpscq.h',
'src/core/lib/support/murmur_hash.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/spinlock.h',
'src/core/lib/support/stack_lockfree.h', 'src/core/lib/support/stack_lockfree.h',
'src/core/lib/support/string.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/log_windows.cc',
'src/core/lib/support/mpscq.cc', 'src/core/lib/support/mpscq.cc',
'src/core/lib/support/murmur_hash.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/stack_lockfree.cc',
'src/core/lib/support/string.cc', 'src/core/lib/support/string.cc',
'src/core/lib/support/string_posix.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.h',
'src/core/lib/support/atomic_with_atm.h', 'src/core/lib/support/atomic_with_atm.h',
'src/core/lib/support/atomic_with_std.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/env.h',
'src/core/lib/support/manual_constructor.h', 'src/core/lib/support/manual_constructor.h',
'src/core/lib/support/memory.h', 'src/core/lib/support/memory.h',
'src/core/lib/support/mpscq.h', 'src/core/lib/support/mpscq.h',
'src/core/lib/support/murmur_hash.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/spinlock.h',
'src/core/lib/support/stack_lockfree.h', 'src/core/lib/support/stack_lockfree.h',
'src/core/lib/support/string.h', 'src/core/lib/support/string.h',

@ -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.h )
s.files += %w( src/core/lib/support/atomic_with_atm.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/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/env.h )
s.files += %w( src/core/lib/support/manual_constructor.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/memory.h )
s.files += %w( src/core/lib/support/mpscq.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/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/spinlock.h )
s.files += %w( src/core/lib/support/stack_lockfree.h ) s.files += %w( src/core/lib/support/stack_lockfree.h )
s.files += %w( src/core/lib/support/string.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/log_windows.cc )
s.files += %w( src/core/lib/support/mpscq.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/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/stack_lockfree.cc )
s.files += %w( src/core/lib/support/string.cc ) s.files += %w( src/core/lib/support/string.cc )
s.files += %w( src/core/lib/support/string_posix.cc ) s.files += %w( src/core/lib/support/string_posix.cc )

@ -184,6 +184,7 @@
'src/core/lib/support/log_windows.cc', 'src/core/lib/support/log_windows.cc',
'src/core/lib/support/mpscq.cc', 'src/core/lib/support/mpscq.cc',
'src/core/lib/support/murmur_hash.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/stack_lockfree.cc',
'src/core/lib/support/string.cc', 'src/core/lib/support/string.cc',
'src/core/lib/support/string_posix.cc', 'src/core/lib/support/string_posix.cc',

@ -101,11 +101,13 @@
<file baseinstalldir="/" name="src/core/lib/support/atomic.h" role="src" /> <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_atm.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/atomic_with_std.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/env.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/manual_constructor.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/memory.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/mpscq.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/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/spinlock.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/stack_lockfree.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" /> <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/log_windows.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/mpscq.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/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/stack_lockfree.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/support/string.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" /> <file baseinstalldir="/" name="src/core/lib/support/string_posix.cc" role="src" />

@ -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

@ -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

@ -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

@ -38,6 +38,7 @@ CORE_SOURCE_FILES = [
'src/core/lib/support/log_windows.cc', 'src/core/lib/support/log_windows.cc',
'src/core/lib/support/mpscq.cc', 'src/core/lib/support/mpscq.cc',
'src/core/lib/support/murmur_hash.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/stack_lockfree.cc',
'src/core/lib/support/string.cc', 'src/core/lib/support/string.cc',
'src/core/lib/support/string_posix.cc', 'src/core/lib/support/string_posix.cc',

@ -233,3 +233,16 @@ grpc_cc_test(
"gtest", "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",
],
)

@ -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();
}

@ -1031,11 +1031,13 @@ src/core/lib/support/arena.h \
src/core/lib/support/atomic.h \ src/core/lib/support/atomic.h \
src/core/lib/support/atomic_with_atm.h \ src/core/lib/support/atomic_with_atm.h \
src/core/lib/support/atomic_with_std.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/env.h \
src/core/lib/support/manual_constructor.h \ src/core/lib/support/manual_constructor.h \
src/core/lib/support/memory.h \ src/core/lib/support/memory.h \
src/core/lib/support/mpscq.h \ src/core/lib/support/mpscq.h \
src/core/lib/support/murmur_hash.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/spinlock.h \
src/core/lib/support/stack_lockfree.h \ src/core/lib/support/stack_lockfree.h \
src/core/lib/support/string.h \ src/core/lib/support/string.h \

@ -1281,6 +1281,7 @@ src/core/lib/support/cpu_iphone.cc \
src/core/lib/support/cpu_linux.cc \ src/core/lib/support/cpu_linux.cc \
src/core/lib/support/cpu_posix.cc \ src/core/lib/support/cpu_posix.cc \
src/core/lib/support/cpu_windows.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.h \
src/core/lib/support/env_linux.cc \ src/core/lib/support/env_linux.cc \
src/core/lib/support/env_posix.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/mpscq.h \
src/core/lib/support/murmur_hash.cc \ src/core/lib/support/murmur_hash.cc \
src/core/lib/support/murmur_hash.h \ 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/spinlock.h \
src/core/lib/support/stack_lockfree.cc \ src/core/lib/support/stack_lockfree.cc \
src/core/lib/support/stack_lockfree.h \ src/core/lib/support/stack_lockfree.h \

@ -3917,6 +3917,25 @@
"third_party": false, "third_party": false,
"type": "target" "type": "target"
}, },
{
"deps": [
"gpr",
"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": [ "deps": [
"gpr", "gpr",
@ -7770,6 +7789,7 @@
"src/core/lib/support/log_windows.cc", "src/core/lib/support/log_windows.cc",
"src/core/lib/support/mpscq.cc", "src/core/lib/support/mpscq.cc",
"src/core/lib/support/murmur_hash.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/stack_lockfree.cc",
"src/core/lib/support/string.cc", "src/core/lib/support/string.cc",
"src/core/lib/support/string_posix.cc", "src/core/lib/support/string_posix.cc",
@ -7834,11 +7854,13 @@
"src/core/lib/support/atomic.h", "src/core/lib/support/atomic.h",
"src/core/lib/support/atomic_with_atm.h", "src/core/lib/support/atomic_with_atm.h",
"src/core/lib/support/atomic_with_std.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/env.h",
"src/core/lib/support/manual_constructor.h", "src/core/lib/support/manual_constructor.h",
"src/core/lib/support/memory.h", "src/core/lib/support/memory.h",
"src/core/lib/support/mpscq.h", "src/core/lib/support/mpscq.h",
"src/core/lib/support/murmur_hash.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/spinlock.h",
"src/core/lib/support/stack_lockfree.h", "src/core/lib/support/stack_lockfree.h",
"src/core/lib/support/string.h", "src/core/lib/support/string.h",
@ -7883,11 +7905,13 @@
"src/core/lib/support/atomic.h", "src/core/lib/support/atomic.h",
"src/core/lib/support/atomic_with_atm.h", "src/core/lib/support/atomic_with_atm.h",
"src/core/lib/support/atomic_with_std.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/env.h",
"src/core/lib/support/manual_constructor.h", "src/core/lib/support/manual_constructor.h",
"src/core/lib/support/memory.h", "src/core/lib/support/memory.h",
"src/core/lib/support/mpscq.h", "src/core/lib/support/mpscq.h",
"src/core/lib/support/murmur_hash.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/spinlock.h",
"src/core/lib/support/stack_lockfree.h", "src/core/lib/support/stack_lockfree.h",
"src/core/lib/support/string.h", "src/core/lib/support/string.h",

@ -4123,6 +4123,30 @@
], ],
"uses_polling": true "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": "reference_counted_test",
"platforms": [
"linux",
"mac",
"posix",
"windows"
],
"uses_polling": true
},
{ {
"args": [], "args": [],
"benchmark": false, "benchmark": false,

Loading…
Cancel
Save