diff --git a/BUILD b/BUILD index 797cc97adf6..d497dab9e9a 100644 --- a/BUILD +++ b/BUILD @@ -358,6 +358,7 @@ GRPCXX_PUBLIC_HDRS = [ "include/grpcpp/impl/server_initializer.h", "include/grpcpp/impl/service_type.h", "include/grpcpp/impl/status.h", + "include/grpcpp/impl/sync.h", "include/grpcpp/resource_quota.h", "include/grpcpp/security/auth_context.h", "include/grpcpp/security/auth_metadata_processor.h", diff --git a/CMakeLists.txt b/CMakeLists.txt index 11f526fce58..cb6640e37d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3462,6 +3462,7 @@ foreach(_hdr include/grpcpp/impl/server_initializer.h include/grpcpp/impl/service_type.h include/grpcpp/impl/status.h + include/grpcpp/impl/sync.h include/grpcpp/resource_quota.h include/grpcpp/security/auth_context.h include/grpcpp/security/auth_metadata_processor.h @@ -4156,6 +4157,7 @@ foreach(_hdr include/grpcpp/impl/server_initializer.h include/grpcpp/impl/service_type.h include/grpcpp/impl/status.h + include/grpcpp/impl/sync.h include/grpcpp/resource_quota.h include/grpcpp/security/auth_context.h include/grpcpp/security/auth_metadata_processor.h diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 0258a468bd3..dbc9ef7834c 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -2837,6 +2837,7 @@ libs: - include/grpcpp/impl/server_initializer.h - include/grpcpp/impl/service_type.h - include/grpcpp/impl/status.h + - include/grpcpp/impl/sync.h - include/grpcpp/resource_quota.h - include/grpcpp/security/auth_context.h - include/grpcpp/security/auth_metadata_processor.h @@ -3262,6 +3263,7 @@ libs: - include/grpcpp/impl/server_initializer.h - include/grpcpp/impl/service_type.h - include/grpcpp/impl/status.h + - include/grpcpp/impl/sync.h - include/grpcpp/resource_quota.h - include/grpcpp/security/auth_context.h - include/grpcpp/security/auth_metadata_processor.h diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index a8bb02af5ce..bb4ddd1b6cd 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -163,6 +163,7 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/server_initializer.h', 'include/grpcpp/impl/service_type.h', 'include/grpcpp/impl/status.h', + 'include/grpcpp/impl/sync.h', 'include/grpcpp/resource_quota.h', 'include/grpcpp/security/auth_context.h', 'include/grpcpp/security/auth_metadata_processor.h', diff --git a/include/grpcpp/channel.h b/include/grpcpp/channel.h index 29a164341f8..ef05d0747fd 100644 --- a/include/grpcpp/channel.h +++ b/include/grpcpp/channel.h @@ -25,8 +25,8 @@ #include #include #include -#include #include +#include #include #include diff --git a/include/grpcpp/client_context.h b/include/grpcpp/client_context.h index 12161aba543..6c2adf776da 100644 --- a/include/grpcpp/client_context.h +++ b/include/grpcpp/client_context.h @@ -41,10 +41,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include diff --git a/include/grpcpp/completion_queue.h b/include/grpcpp/completion_queue.h index 02416efb789..bf76447702e 100644 --- a/include/grpcpp/completion_queue.h +++ b/include/grpcpp/completion_queue.h @@ -42,6 +42,7 @@ #include #include #include +#include struct grpc_completion_queue; diff --git a/include/grpcpp/ext/call_metric_recorder.h b/include/grpcpp/ext/call_metric_recorder.h index 95a51154d43..57d9202c6c7 100644 --- a/include/grpcpp/ext/call_metric_recorder.h +++ b/include/grpcpp/ext/call_metric_recorder.h @@ -25,7 +25,7 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" -#include +#include #include namespace grpc_core { diff --git a/include/grpcpp/ext/orca_service.h b/include/grpcpp/ext/orca_service.h index dcd02657725..e3942b15d05 100644 --- a/include/grpcpp/ext/orca_service.h +++ b/include/grpcpp/ext/orca_service.h @@ -23,8 +23,8 @@ #include "absl/time/time.h" #include "absl/types/optional.h" -#include #include +#include #include #include #include diff --git a/include/grpcpp/impl/codegen/sync.h b/include/grpcpp/impl/codegen/sync.h index 4cb8133c7c3..614cee625fe 100644 --- a/include/grpcpp/impl/codegen/sync.h +++ b/include/grpcpp/impl/codegen/sync.h @@ -21,138 +21,7 @@ // IWYU pragma: private -#include - -#ifdef GPR_HAS_PTHREAD_H -#include -#endif - -#include - -#include "absl/synchronization/mutex.h" - -#include -#include -#include - -// The core library is not accessible in C++ codegen headers, and vice versa. -// Thus, we need to have duplicate headers with similar functionality. -// Make sure any change to this file is also reflected in -// src/core/lib/gprpp/sync.h too. -// -// Whenever possible, prefer "src/core/lib/gprpp/sync.h" over this file, -// since in core we do not rely on g_core_codegen_interface and hence do not -// pay the costs of virtual function calls. - -namespace grpc { -namespace internal { - -#ifdef GPR_ABSEIL_SYNC - -using Mutex = absl::Mutex; -using MutexLock = absl::MutexLock; -using ReleasableMutexLock = absl::ReleasableMutexLock; -using CondVar = absl::CondVar; - -#else - -class ABSL_LOCKABLE Mutex { - public: - Mutex() { g_core_codegen_interface->gpr_mu_init(&mu_); } - ~Mutex() { g_core_codegen_interface->gpr_mu_destroy(&mu_); } - - Mutex(const Mutex&) = delete; - Mutex& operator=(const Mutex&) = delete; - - void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() { - g_core_codegen_interface->gpr_mu_lock(&mu_); - } - void Unlock() ABSL_UNLOCK_FUNCTION() { - g_core_codegen_interface->gpr_mu_unlock(&mu_); - } - - private: - union { - gpr_mu mu_; - std::mutex do_not_use_sth_; -#ifdef GPR_HAS_PTHREAD_H - pthread_mutex_t do_not_use_pth_; -#endif - }; - - friend class CondVar; -}; - -class ABSL_SCOPED_LOCKABLE MutexLock { - public: - explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { - mu_->Lock(); - } - ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); } - - MutexLock(const MutexLock&) = delete; - MutexLock& operator=(const MutexLock&) = delete; - - private: - Mutex* const mu_; -}; - -class ABSL_SCOPED_LOCKABLE ReleasableMutexLock { - public: - explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) - : mu_(mu) { - mu_->Lock(); - } - ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() { - if (!released_) mu_->Unlock(); - } - - ReleasableMutexLock(const ReleasableMutexLock&) = delete; - ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete; - - void Release() ABSL_UNLOCK_FUNCTION() { - GPR_DEBUG_ASSERT(!released_); - released_ = true; - mu_->Unlock(); - } - - private: - Mutex* const mu_; - bool released_ = false; -}; - -class CondVar { - public: - CondVar() { g_core_codegen_interface->gpr_cv_init(&cv_); } - ~CondVar() { g_core_codegen_interface->gpr_cv_destroy(&cv_); } - - CondVar(const CondVar&) = delete; - CondVar& operator=(const CondVar&) = delete; - - void Signal() { g_core_codegen_interface->gpr_cv_signal(&cv_); } - void SignalAll() { g_core_codegen_interface->gpr_cv_broadcast(&cv_); } - - void Wait(Mutex* mu) { - g_core_codegen_interface->gpr_cv_wait( - &cv_, &mu->mu_, - g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME)); - } - - private: - gpr_cv cv_; -}; - -#endif // GPR_ABSEIL_SYNC - -template -GRPC_DEPRECATED("incompatible with thread safety analysis") -static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) { - while (!pred()) { - cv->Wait(mu); - } -} - -} // namespace internal -} // namespace grpc +/// TODO(chengyuc): Remove this file after solving compatibility. +#include #endif // GRPCPP_IMPL_CODEGEN_SYNC_H diff --git a/include/grpcpp/impl/sync.h b/include/grpcpp/impl/sync.h new file mode 100644 index 00000000000..146359e0d8d --- /dev/null +++ b/include/grpcpp/impl/sync.h @@ -0,0 +1,156 @@ +/* + * + * Copyright 2019 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 GRPCPP_IMPL_SYNC_H +#define GRPCPP_IMPL_SYNC_H + +#include + +#ifdef GPR_HAS_PTHREAD_H +#include +#endif + +#include + +#include "absl/synchronization/mutex.h" + +#include +#include +#include + +// The core library is not accessible in C++ codegen headers, and vice versa. +// Thus, we need to have duplicate headers with similar functionality. +// Make sure any change to this file is also reflected in +// src/core/lib/gprpp/sync.h too. +// +// Whenever possible, prefer "src/core/lib/gprpp/sync.h" over this file, +// since in core we do not rely on g_core_codegen_interface and hence do not +// pay the costs of virtual function calls. + +namespace grpc { +namespace internal { + +#ifdef GPR_ABSEIL_SYNC + +using Mutex = absl::Mutex; +using MutexLock = absl::MutexLock; +using ReleasableMutexLock = absl::ReleasableMutexLock; +using CondVar = absl::CondVar; + +#else + +class ABSL_LOCKABLE Mutex { + public: + Mutex() { g_core_codegen_interface->gpr_mu_init(&mu_); } + ~Mutex() { g_core_codegen_interface->gpr_mu_destroy(&mu_); } + + Mutex(const Mutex&) = delete; + Mutex& operator=(const Mutex&) = delete; + + void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() { + g_core_codegen_interface->gpr_mu_lock(&mu_); + } + void Unlock() ABSL_UNLOCK_FUNCTION() { + g_core_codegen_interface->gpr_mu_unlock(&mu_); + } + + private: + union { + gpr_mu mu_; + std::mutex do_not_use_sth_; +#ifdef GPR_HAS_PTHREAD_H + pthread_mutex_t do_not_use_pth_; +#endif + }; + + friend class CondVar; +}; + +class ABSL_SCOPED_LOCKABLE MutexLock { + public: + explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { + mu_->Lock(); + } + ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); } + + MutexLock(const MutexLock&) = delete; + MutexLock& operator=(const MutexLock&) = delete; + + private: + Mutex* const mu_; +}; + +class ABSL_SCOPED_LOCKABLE ReleasableMutexLock { + public: + explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) + : mu_(mu) { + mu_->Lock(); + } + ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() { + if (!released_) mu_->Unlock(); + } + + ReleasableMutexLock(const ReleasableMutexLock&) = delete; + ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete; + + void Release() ABSL_UNLOCK_FUNCTION() { + GPR_DEBUG_ASSERT(!released_); + released_ = true; + mu_->Unlock(); + } + + private: + Mutex* const mu_; + bool released_ = false; +}; + +class CondVar { + public: + CondVar() { g_core_codegen_interface->gpr_cv_init(&cv_); } + ~CondVar() { g_core_codegen_interface->gpr_cv_destroy(&cv_); } + + CondVar(const CondVar&) = delete; + CondVar& operator=(const CondVar&) = delete; + + void Signal() { g_core_codegen_interface->gpr_cv_signal(&cv_); } + void SignalAll() { g_core_codegen_interface->gpr_cv_broadcast(&cv_); } + + void Wait(Mutex* mu) { + g_core_codegen_interface->gpr_cv_wait( + &cv_, &mu->mu_, + g_core_codegen_interface->gpr_inf_future(GPR_CLOCK_REALTIME)); + } + + private: + gpr_cv cv_; +}; + +#endif // GPR_ABSEIL_SYNC + +template +GRPC_DEPRECATED("incompatible with thread safety analysis") +static void WaitUntil(CondVar* cv, Mutex* mu, Predicate pred) { + while (!pred()) { + cv->Wait(mu); + } +} + +} // namespace internal +} // namespace grpc + +#endif // GRPCPP_IMPL_SYNC_H diff --git a/include/grpcpp/security/tls_certificate_verifier.h b/include/grpcpp/security/tls_certificate_verifier.h index 0c9f35a96a4..e3776541125 100644 --- a/include/grpcpp/security/tls_certificate_verifier.h +++ b/include/grpcpp/security/tls_certificate_verifier.h @@ -26,8 +26,8 @@ #include #include #include -#include #include +#include #include #include diff --git a/include/grpcpp/support/client_callback.h b/include/grpcpp/support/client_callback.h index 708960ba04f..22d15a3b353 100644 --- a/include/grpcpp/support/client_callback.h +++ b/include/grpcpp/support/client_callback.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/grpcpp/support/server_callback.h b/include/grpcpp/support/server_callback.h index 4da80582460..bef0c80a599 100644 --- a/include/grpcpp/support/server_callback.h +++ b/include/grpcpp/support/server_callback.h @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/core/lib/gprpp/sync.h b/src/core/lib/gprpp/sync.h index cf5da02fcec..f54a0728009 100644 --- a/src/core/lib/gprpp/sync.h +++ b/src/core/lib/gprpp/sync.h @@ -34,9 +34,9 @@ // The core library is not accessible in C++ codegen headers, and vice versa. // Thus, we need to have duplicate headers with similar functionality. // Make sure any change to this file is also reflected in -// include/grpcpp/impl/codegen/sync.h. +// include/grpcpp/impl/sync.h. // -// Whenever possible, prefer using this file over +// Whenever possible, prefer using this file over // since this file doesn't rely on g_core_codegen_interface and hence does not // pay the costs of virtual function calls. diff --git a/src/cpp/client/channel_cc.cc b/src/cpp/client/channel_cc.cc index 0a4781d1206..08a91d57d70 100644 --- a/src/cpp/client/channel_cc.cc +++ b/src/cpp/client/channel_cc.cc @@ -36,10 +36,10 @@ #include #include #include -#include #include #include #include +#include #include #include diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc index e536f4b547b..6c63abbf1cf 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -35,9 +35,9 @@ #include #include #include -#include #include #include +#include #include #include #include diff --git a/src/cpp/common/tls_certificate_verifier.cc b/src/cpp/common/tls_certificate_verifier.cc index 3f07ac211d5..f6ff257da8f 100644 --- a/src/cpp/common/tls_certificate_verifier.cc +++ b/src/cpp/common/tls_certificate_verifier.cc @@ -28,8 +28,8 @@ #include #include #include -#include #include +#include #include #include #include diff --git a/src/cpp/server/health/default_health_check_service.h b/src/cpp/server/health/default_health_check_service.h index eb3535cdc40..f23ac5d4733 100644 --- a/src/cpp/server/health/default_health_check_service.h +++ b/src/cpp/server/health/default_health_check_service.h @@ -29,8 +29,8 @@ #include #include -#include #include +#include #include #include #include diff --git a/src/cpp/server/orca/call_metric_recorder.cc b/src/cpp/server/orca/call_metric_recorder.cc index e5c115a368c..63977870385 100644 --- a/src/cpp/server/orca/call_metric_recorder.cc +++ b/src/cpp/server/orca/call_metric_recorder.cc @@ -27,7 +27,7 @@ #include "xds/data/orca/v3/orca_load_report.upb.h" #include -#include +#include #include #include "src/core/ext/filters/client_channel/lb_policy/backend_metric_data.h" diff --git a/src/cpp/server/orca/orca_service.cc b/src/cpp/server/orca/orca_service.cc index 7939f519276..47955a7c612 100644 --- a/src/cpp/server/orca/orca_service.cc +++ b/src/cpp/server/orca/orca_service.cc @@ -33,10 +33,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc index 3931bcd14b5..5e442c38380 100644 --- a/src/cpp/server/server_cc.cc +++ b/src/cpp/server/server_cc.cc @@ -45,7 +45,6 @@ #include #include #include -#include #include #include #include @@ -55,6 +54,7 @@ #include #include #include +#include #include #include #include diff --git a/test/core/tsi/alts/fake_handshaker/fake_handshaker_server.cc b/test/core/tsi/alts/fake_handshaker/fake_handshaker_server.cc index ca041a56906..c621d6c3bcf 100644 --- a/test/core/tsi/alts/fake_handshaker/fake_handshaker_server.cc +++ b/test/core/tsi/alts/fake_handshaker/fake_handshaker_server.cc @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include #include diff --git a/test/cpp/client/client_channel_stress_test.cc b/test/cpp/client/client_channel_stress_test.cc index 36461f56282..3224af691f3 100644 --- a/test/cpp/client/client_channel_stress_test.cc +++ b/test/cpp/client/client_channel_stress_test.cc @@ -34,7 +34,7 @@ #include #include #include -#include +#include #include #include diff --git a/test/cpp/client/destroy_grpclb_channel_with_active_connect_stress_test.cc b/test/cpp/client/destroy_grpclb_channel_with_active_connect_stress_test.cc index 6b1d9b7c191..d81cb11bd70 100644 --- a/test/cpp/client/destroy_grpclb_channel_with_active_connect_stress_test.cc +++ b/test/cpp/client/destroy_grpclb_channel_with_active_connect_stress_test.cc @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include diff --git a/test/cpp/end2end/client_lb_end2end_test.cc b/test/cpp/end2end/client_lb_end2end_test.cc index 495fcb50fc9..4d28d8ce1c3 100644 --- a/test/cpp/end2end/client_lb_end2end_test.cc +++ b/test/cpp/end2end/client_lb_end2end_test.cc @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include diff --git a/test/cpp/end2end/grpclb_end2end_test.cc b/test/cpp/end2end/grpclb_end2end_test.cc index 0e9becc6683..d157a3d61cf 100644 --- a/test/cpp/end2end/grpclb_end2end_test.cc +++ b/test/cpp/end2end/grpclb_end2end_test.cc @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #include diff --git a/test/cpp/end2end/service_config_end2end_test.cc b/test/cpp/end2end/service_config_end2end_test.cc index b4f56791a40..131cf595724 100644 --- a/test/cpp/end2end/service_config_end2end_test.cc +++ b/test/cpp/end2end/service_config_end2end_test.cc @@ -39,7 +39,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/cpp/end2end/thread_stress_test.cc b/test/cpp/end2end/thread_stress_test.cc index 85594ff1483..79a23b33b88 100644 --- a/test/cpp/end2end/thread_stress_test.cc +++ b/test/cpp/end2end/thread_stress_test.cc @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index 2ddc0b4088c..2872a0e760c 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -1027,6 +1027,7 @@ include/grpcpp/impl/server_callback_handlers.h \ include/grpcpp/impl/server_initializer.h \ include/grpcpp/impl/service_type.h \ include/grpcpp/impl/status.h \ +include/grpcpp/impl/sync.h \ include/grpcpp/resource_quota.h \ include/grpcpp/security/auth_context.h \ include/grpcpp/security/auth_metadata_processor.h \ diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index b5c3d5193fe..4d2cb780a31 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1027,6 +1027,7 @@ include/grpcpp/impl/server_callback_handlers.h \ include/grpcpp/impl/server_initializer.h \ include/grpcpp/impl/service_type.h \ include/grpcpp/impl/status.h \ +include/grpcpp/impl/sync.h \ include/grpcpp/resource_quota.h \ include/grpcpp/security/auth_context.h \ include/grpcpp/security/auth_metadata_processor.h \ diff --git a/tools/run_tests/sanity/cpp_banned_constructs.sh b/tools/run_tests/sanity/cpp_banned_constructs.sh index 30852a7f6e5..a9bbcc74ccc 100755 --- a/tools/run_tests/sanity/cpp_banned_constructs.sh +++ b/tools/run_tests/sanity/cpp_banned_constructs.sh @@ -26,7 +26,7 @@ cd "$(dirname "$0")/../../.." grep -EIrn \ 'std::(mutex|condition_variable|lock_guard|unique_lock|thread)' \ include/grpc include/grpcpp src/core src/cpp | \ - grep -Ev include/grpcpp/impl/codegen/sync.h | \ + grep -Ev include/grpcpp/impl/sync.h | \ diff - /dev/null # @@ -36,7 +36,7 @@ grep -EIrn \ grep -EIrn \ '^#include (||||||)' \ include/grpc include/grpcpp src/core src/cpp | \ - grep -Ev include/grpcpp/impl/codegen/sync.h | \ + grep -Ev include/grpcpp/impl/sync.h | \ diff - /dev/null #