From 70db663ae8773dc45464c904603a3e73045b45c5 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Mon, 27 Nov 2017 14:53:26 -0800 Subject: [PATCH] Add ReferenceCounted base class. --- BUILD | 17 ++++++ CMakeLists.txt | 42 +++++++++++++ Makefile | 49 +++++++++++++++ build.yaml | 17 ++++++ config.m4 | 1 + config.w32 | 1 + gRPC-Core.podspec | 5 ++ grpc.gemspec | 3 + grpc.gyp | 1 + package.xml | 3 + src/core/lib/support/debug_location.h | 48 +++++++++++++++ src/core/lib/support/reference_counted.cc | 58 ++++++++++++++++++ src/core/lib/support/reference_counted.h | 55 +++++++++++++++++ src/python/grpcio/grpc_core_dependencies.py | 1 + test/core/support/BUILD | 13 ++++ test/core/support/reference_counted_test.cc | 60 +++++++++++++++++++ tools/doxygen/Doxyfile.c++.internal | 2 + tools/doxygen/Doxyfile.core.internal | 3 + .../generated/sources_and_headers.json | 24 ++++++++ tools/run_tests/generated/tests.json | 24 ++++++++ 20 files changed, 427 insertions(+) create mode 100644 src/core/lib/support/debug_location.h create mode 100644 src/core/lib/support/reference_counted.cc create mode 100644 src/core/lib/support/reference_counted.h create mode 100644 test/core/support/reference_counted_test.cc diff --git a/BUILD b/BUILD index 25daba8e1e9..5289023ef5a 100644 --- a/BUILD +++ b/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( name = "grpc_base_c", srcs = [ diff --git a/CMakeLists.txt b/CMakeLists.txt index 77b60e898ab..ca59c84e500 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 c85b1bca733..0e8437a05cd 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 4f6194ecc41..2903b866dcf 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 d2f2520feac..9d514c8552b 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 8a713751dcd..2f24fcd8b01 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 2f97565f1b4..71e13633ae8 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 0dd7ceb3506..900f4afe7a4 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 fb153915ff0..0ee6529013b 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 59d49dd1657..c0d9b498286 100644 --- a/package.xml +++ b/package.xml @@ -101,11 +101,13 @@ + + @@ -135,6 +137,7 @@ + diff --git a/src/core/lib/support/debug_location.h b/src/core/lib/support/debug_location.h new file mode 100644 index 00000000000..9260752d5e8 --- /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 00000000000..853a29da2f3 --- /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 + +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 00000000000..ebd620c5536 --- /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 + +#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 330c4185c61..38f2a401330 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 69512cd9a92..ee20ef86925 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 00000000000..119fc1b6002 --- /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 +#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 3c564e203af..1d84dcba336 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 5674124f446..5b289e7bc4d 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 5a3429c81f5..8ec96198748 100644 --- a/tools/run_tests/generated/sources_and_headers.json +++ b/tools/run_tests/generated/sources_and_headers.json @@ -3917,6 +3917,25 @@ "third_party": false, "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": [ "gpr", @@ -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 dfd7bfafe6a..a6b58d8ba65 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -4123,6 +4123,30 @@ ], "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": [], "benchmark": false,