diff --git a/STYLE.md b/STYLE.md index 2f4624ad4e22f..b5767ae812949 100644 --- a/STYLE.md +++ b/STYLE.md @@ -63,6 +63,10 @@ NiceMock for mocks whose behavior is not the focus of a test. * There are probably a few other things missing from this list. We will add them as they are brought to our attention. +* [Thread + annotations](https://github.com/abseil/abseil-cpp/blob/master/absl/base/thread_annotations.h), + such as `GUARDED_BY`, should be used for shared state guarded by + locks/mutexes. # Error handling diff --git a/source/common/common/BUILD b/source/common/common/BUILD index 19ec24be5691c..7fc5586f92fd2 100644 --- a/source/common/common/BUILD +++ b/source/common/common/BUILD @@ -109,6 +109,12 @@ envoy_cc_library( hdrs = ["stl_helpers.h"], ) +envoy_cc_library( + name = "thread_annotations", + hdrs = ["thread_annotations.h"], + external_deps = ["abseil_base"], +) + envoy_cc_library( name = "thread_lib", srcs = ["thread.cc"], diff --git a/source/common/common/thread_annotations.h b/source/common/common/thread_annotations.h new file mode 100644 index 0000000000000..bcdfb3ea1c703 --- /dev/null +++ b/source/common/common/thread_annotations.h @@ -0,0 +1,11 @@ +#pragma once + +// NOLINT(namespace-envoy) + +#include "absl/base/thread_annotations.h" + +#ifdef __APPLE__ +#undef THREAD_ANNOTATION_ATTRIBUTE__ +// See #2571, we get problematic warnings on Clang + OS X. +#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op +#endif diff --git a/source/common/grpc/BUILD b/source/common/grpc/BUILD index 46174a1441acc..bbc466cf38f6b 100644 --- a/source/common/grpc/BUILD +++ b/source/common/grpc/BUILD @@ -85,6 +85,7 @@ envoy_cc_library( "//include/envoy/thread_local:thread_local_interface", "//source/common/common:empty_string", "//source/common/common:linked_object", + "//source/common/common:thread_annotations", "//source/common/common:thread_lib", "//source/common/tracing:http_tracer_lib", ], diff --git a/source/common/grpc/google_async_client_impl.h b/source/common/grpc/google_async_client_impl.h index 89ad4ab80dbbb..56939a7f23233 100644 --- a/source/common/grpc/google_async_client_impl.h +++ b/source/common/grpc/google_async_client_impl.h @@ -8,6 +8,7 @@ #include "common/common/linked_object.h" #include "common/common/thread.h" +#include "common/common/thread_annotations.h" #include "common/tracing/http_tracer_impl.h" #include "grpc++/generic/generic_stub.h" @@ -289,7 +290,8 @@ class GoogleAsyncStreamImpl : public AsyncStream, uint32_t inflight_tags_{}; // Queue of completed (op, ok) passed from completionThread() to // handleOpCompletion(). - std::deque> completed_ops_; + std::deque> + completed_ops_ GUARDED_BY(completed_ops_lock_); std::mutex completed_ops_lock_; friend class GoogleAsyncClientImpl;