From 3c2ea47abf4578a1e09a0b0b96bf7dc3e2328527 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 5 Apr 2022 18:09:04 -0700 Subject: [PATCH] Add static thread safety analysis ready synchronization primitives. (flutter/engine#117) --- engine/src/flutter/impeller/base/BUILD.gn | 6 +- .../flutter/impeller/base/base_unittests.cc | 72 ++++++++++++++ .../impeller/base/{base.h => thread.cc} | 10 +- engine/src/flutter/impeller/base/thread.h | 93 +++++++++++++++++++ .../flutter/impeller/base/thread_safety.cc | 11 +++ .../src/flutter/impeller/base/thread_safety.h | 69 ++++++++++++++ .../renderer/backend/metal/render_pass_mtl.mm | 2 +- .../src/flutter/impeller/renderer/pipeline.cc | 2 +- .../impeller/renderer/pipeline_builder.h | 3 +- .../impeller/renderer/render_target.cc | 3 +- .../impeller/renderer/vertex_buffer_builder.h | 2 +- .../src/flutter/impeller/tools/impeller.gni | 5 + 12 files changed, 268 insertions(+), 10 deletions(-) create mode 100644 engine/src/flutter/impeller/base/base_unittests.cc rename engine/src/flutter/impeller/base/{base.h => thread.cc} (57%) create mode 100644 engine/src/flutter/impeller/base/thread.h create mode 100644 engine/src/flutter/impeller/base/thread_safety.cc create mode 100644 engine/src/flutter/impeller/base/thread_safety.h diff --git a/engine/src/flutter/impeller/base/BUILD.gn b/engine/src/flutter/impeller/base/BUILD.gn index 939f50a8a3..1c4387834d 100644 --- a/engine/src/flutter/impeller/base/BUILD.gn +++ b/engine/src/flutter/impeller/base/BUILD.gn @@ -17,6 +17,10 @@ impeller_component("base") { "promise.h", "strings.cc", "strings.h", + "thread.cc", + "thread.h", + "thread_safety.cc", + "thread_safety.h", "validation.cc", "validation.h", ] @@ -26,7 +30,7 @@ impeller_component("base") { impeller_component("base_unittests") { testonly = true - sources = [] + sources = [ "base_unittests.cc" ] deps = [ ":base", "//flutter/testing", diff --git a/engine/src/flutter/impeller/base/base_unittests.cc b/engine/src/flutter/impeller/base/base_unittests.cc new file mode 100644 index 0000000000..4dbff57fb2 --- /dev/null +++ b/engine/src/flutter/impeller/base/base_unittests.cc @@ -0,0 +1,72 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/testing/testing.h" +#include "impeller/base/thread.h" + +namespace impeller { +namespace testing { + +struct Foo { + Mutex mtx; + int a IPLR_GUARDED_BY(mtx); +}; + +struct RWFoo { + RWMutex mtx; + int a IPLR_GUARDED_BY(mtx); +}; + +TEST(ThreadTest, CanCreateMutex) { + Foo f = {}; + + // f.a = 100; <--- Static analysis error. + f.mtx.Lock(); + f.a = 100; + f.mtx.Unlock(); +} + +TEST(ThreadTest, CanCreateMutexLock) { + Foo f = {}; + + // f.a = 100; <--- Static analysis error. + auto a = Lock(f.mtx); + f.a = 100; +} + +TEST(ThreadTest, CanCreateRWMutex) { + RWFoo f = {}; + + // f.a = 100; <--- Static analysis error. + f.mtx.LockWriter(); + f.a = 100; + f.mtx.UnlockWriter(); + // int b = f.a; <--- Static analysis error. + f.mtx.LockReader(); + int b = f.a; + FML_ALLOW_UNUSED_LOCAL(b); + f.mtx.UnlockReader(); +} + +TEST(ThreadTest, CanCreateRWMutexLock) { + RWFoo f = {}; + + // f.a = 100; <--- Static analysis error. + { + auto write_lock = WriterLock{f.mtx}; + f.a = 100; + } + + // int b = f.a; <--- Static analysis error. + { + auto read_lock = ReaderLock(f.mtx); + int b = f.a; + FML_ALLOW_UNUSED_LOCAL(b); + } + + // f.mtx.UnlockReader(); <--- Static analysis error. +} + +} // namespace testing +} // namespace impeller diff --git a/engine/src/flutter/impeller/base/base.h b/engine/src/flutter/impeller/base/thread.cc similarity index 57% rename from engine/src/flutter/impeller/base/base.h rename to engine/src/flutter/impeller/base/thread.cc index a55a765f09..0403b19cd6 100644 --- a/engine/src/flutter/impeller/base/base.h +++ b/engine/src/flutter/impeller/base/thread.cc @@ -2,8 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#pragma once +#include "impeller/base/thread.h" -#include "impeller/base/promise.h" -#include "impeller/base/strings.h" -#include "impeller/base/validation.h" +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/base/thread.h b/engine/src/flutter/impeller/base/thread.h new file mode 100644 index 0000000000..da200bc8d9 --- /dev/null +++ b/engine/src/flutter/impeller/base/thread.h @@ -0,0 +1,93 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#include +#include + +#include "flutter/fml/macros.h" +#include "flutter/fml/synchronization/shared_mutex.h" +#include "impeller/base/thread_safety.h" + +namespace impeller { + +class IPLR_CAPABILITY("mutex") Mutex { + public: + Mutex() = default; + + ~Mutex() = default; + + void Lock() IPLR_ACQUIRE() { mutex_.lock(); } + + void Unlock() IPLR_RELEASE() { mutex_.unlock(); } + + private: + std::mutex mutex_; + + FML_DISALLOW_COPY_ASSIGN_AND_MOVE(Mutex); +}; + +class IPLR_CAPABILITY("mutex") RWMutex { + public: + RWMutex() + : mutex_(std::unique_ptr(fml::SharedMutex::Create())) {} + + ~RWMutex() = default; + + void LockWriter() IPLR_ACQUIRE() { mutex_->Lock(); } + + void UnlockWriter() IPLR_RELEASE() { mutex_->Unlock(); } + + void LockReader() IPLR_ACQUIRE_SHARED() { mutex_->LockShared(); } + + void UnlockReader() IPLR_RELEASE_SHARED() { mutex_->UnlockShared(); } + + private: + std::unique_ptr mutex_; + + FML_DISALLOW_COPY_ASSIGN_AND_MOVE(RWMutex); +}; + +class IPLR_SCOPED_CAPABILITY Lock { + public: + Lock(Mutex& mutex) IPLR_ACQUIRE(mutex) : mutex_(mutex) { mutex_.Lock(); } + + ~Lock() IPLR_RELEASE() { mutex_.Unlock(); } + + private: + Mutex& mutex_; + + FML_DISALLOW_COPY_ASSIGN_AND_MOVE(Lock); +}; + +class IPLR_SCOPED_CAPABILITY ReaderLock { + public: + ReaderLock(RWMutex& mutex) IPLR_ACQUIRE_SHARED(mutex) : mutex_(mutex) { + mutex_.LockReader(); + } + + ~ReaderLock() IPLR_RELEASE() { mutex_.UnlockReader(); } + + private: + RWMutex& mutex_; + + FML_DISALLOW_COPY_ASSIGN_AND_MOVE(ReaderLock); +}; + +class IPLR_SCOPED_CAPABILITY WriterLock { + public: + WriterLock(RWMutex& mutex) IPLR_ACQUIRE(mutex) : mutex_(mutex) { + mutex_.LockWriter(); + } + + ~WriterLock() IPLR_RELEASE() { mutex_.UnlockWriter(); } + + private: + RWMutex& mutex_; + + FML_DISALLOW_COPY_ASSIGN_AND_MOVE(WriterLock); +}; + +} // namespace impeller diff --git a/engine/src/flutter/impeller/base/thread_safety.cc b/engine/src/flutter/impeller/base/thread_safety.cc new file mode 100644 index 0000000000..e4188f669a --- /dev/null +++ b/engine/src/flutter/impeller/base/thread_safety.cc @@ -0,0 +1,11 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/base/thread_safety.h" + +namespace impeller { + +// + +} // namespace impeller diff --git a/engine/src/flutter/impeller/base/thread_safety.h b/engine/src/flutter/impeller/base/thread_safety.h new file mode 100644 index 0000000000..8d1f0aa02c --- /dev/null +++ b/engine/src/flutter/impeller/base/thread_safety.h @@ -0,0 +1,69 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#pragma once + +#if defined(__clang__) +#define IPLR_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#else +#define IPLR_THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op +#endif + +#define IPLR_CAPABILITY(x) IPLR_THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) + +#define IPLR_SCOPED_CAPABILITY \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) + +#define IPLR_GUARDED_BY(x) IPLR_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) + +#define IPLR_PT_GUARDED_BY(x) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) + +#define IPLR_ACQUIRED_BEFORE(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) + +#define IPLR_ACQUIRED_AFTER(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) + +#define IPLR_REQUIRES(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) + +#define IPLR_REQUIRES_SHARED(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) + +#define IPLR_ACQUIRE(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) + +#define IPLR_ACQUIRE_SHARED(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) + +#define IPLR_RELEASE(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) + +#define IPLR_RELEASE_SHARED(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) + +#define IPLR_RELEASE_GENERIC(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__)) + +#define IPLR_TRY_ACQUIRE(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) + +#define IPLR_TRY_ACQUIRE_SHARED(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) + +#define IPLR_EXCLUDES(...) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) + +#define IPLR_ASSERT_CAPABILITY(x) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) + +#define IPLR_ASSERT_SHARED_CAPABILITY(x) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) + +#define IPLR_RETURN_CAPABILITY(x) \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) + +#define IPLR_NO_THREAD_SAFETY_ANALYSIS \ + IPLR_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) diff --git a/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm index 895f1a1508..897c40a72b 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/render_pass_mtl.mm @@ -7,7 +7,7 @@ #include "flutter/fml/closure.h" #include "flutter/fml/logging.h" #include "flutter/fml/trace_event.h" -#include "impeller/base/base.h" +#include "impeller/base/backend_cast.h" #include "impeller/renderer/backend/metal/device_buffer_mtl.h" #include "impeller/renderer/backend/metal/formats_mtl.h" #include "impeller/renderer/backend/metal/pipeline_mtl.h" diff --git a/engine/src/flutter/impeller/renderer/pipeline.cc b/engine/src/flutter/impeller/renderer/pipeline.cc index ee0f574a69..2e6d5657d8 100644 --- a/engine/src/flutter/impeller/renderer/pipeline.cc +++ b/engine/src/flutter/impeller/renderer/pipeline.cc @@ -4,7 +4,7 @@ #include "impeller/renderer/pipeline.h" -#include "impeller/base/base.h" +#include "impeller/base/promise.h" #include "impeller/renderer/context.h" #include "impeller/renderer/pipeline_library.h" diff --git a/engine/src/flutter/impeller/renderer/pipeline_builder.h b/engine/src/flutter/impeller/renderer/pipeline_builder.h index 090cd813b7..1cb8206d94 100644 --- a/engine/src/flutter/impeller/renderer/pipeline_builder.h +++ b/engine/src/flutter/impeller/renderer/pipeline_builder.h @@ -6,7 +6,8 @@ #include "flutter/fml/logging.h" #include "flutter/fml/macros.h" -#include "impeller/base/base.h" +#include "impeller/base/strings.h" +#include "impeller/base/validation.h" #include "impeller/renderer/context.h" #include "impeller/renderer/formats.h" #include "impeller/renderer/pipeline_descriptor.h" diff --git a/engine/src/flutter/impeller/renderer/render_target.cc b/engine/src/flutter/impeller/renderer/render_target.cc index d7087f8cd0..71eff0fb32 100644 --- a/engine/src/flutter/impeller/renderer/render_target.cc +++ b/engine/src/flutter/impeller/renderer/render_target.cc @@ -4,7 +4,8 @@ #include "impeller/renderer/render_target.h" -#include "impeller/base/base.h" +#include "impeller/base/strings.h" +#include "impeller/base/validation.h" #include "impeller/renderer/allocator.h" #include "impeller/renderer/context.h" #include "impeller/renderer/texture.h" diff --git a/engine/src/flutter/impeller/renderer/vertex_buffer_builder.h b/engine/src/flutter/impeller/renderer/vertex_buffer_builder.h index 1caac9032a..7222537b91 100644 --- a/engine/src/flutter/impeller/renderer/vertex_buffer_builder.h +++ b/engine/src/flutter/impeller/renderer/vertex_buffer_builder.h @@ -9,7 +9,7 @@ #include #include "flutter/fml/macros.h" -#include "impeller/base/base.h" +#include "impeller/base/strings.h" #include "impeller/geometry/vector.h" #include "impeller/renderer/allocator.h" #include "impeller/renderer/device_buffer.h" diff --git a/engine/src/flutter/impeller/tools/impeller.gni b/engine/src/flutter/impeller/tools/impeller.gni index 717f6bd19a..eba266c881 100644 --- a/engine/src/flutter/impeller/tools/impeller.gni +++ b/engine/src/flutter/impeller/tools/impeller.gni @@ -37,6 +37,11 @@ template("impeller_component") { public_configs += [ "//flutter/impeller:impeller_public_config" ] + if (!defined(invoker.cflags)) { + cflags = [] + } + cflags += [ "-Wthread-safety-analysis" ] + if (!defined(invoker.cflags_objc)) { cflags_objc = [] }