diff options
author | 2023-02-09 13:37:40 +0000 | |
---|---|---|
committer | 2023-02-09 15:39:05 +0000 | |
commit | c137762c96cb8329ea95e1dda27b0a2d9c1f3228 (patch) | |
tree | b68f3e25fdef999c7d2c0966ec025344adcef6d5 | |
parent | a8a815cb3a20ac492e1a9d7025b3224708199f60 (diff) |
Fix mutator lock annotations for `nodes.cc`.
Allow constructing `GcRoot<>` and `ObjPtr<>` from null
without holding the mutator lock.
Mark `Object::GetLockOwnerThreadId()` as requiring the
mutator lock.
This allows compiling `nodes.cc` with a new version of
clang++ with improved `-Wthread-safety-analysis`.
Bug: 265153643
Test: Presubmit
Change-Id: I186120216c7176d918ade0e55d77b481b6c70622
-rw-r--r-- | runtime/gc_root.h | 5 | ||||
-rw-r--r-- | runtime/mirror/object.h | 2 | ||||
-rw-r--r-- | runtime/mirror/object_reference.h | 9 | ||||
-rw-r--r-- | runtime/obj_ptr.h | 11 |
4 files changed, 17 insertions, 10 deletions
diff --git a/runtime/gc_root.h b/runtime/gc_root.h index 3f207ec251..19e2786ae2 100644 --- a/runtime/gc_root.h +++ b/runtime/gc_root.h @@ -214,7 +214,10 @@ class GcRoot { return root_.IsNull(); } - ALWAYS_INLINE GcRoot() {} + ALWAYS_INLINE GcRoot() : GcRoot(nullptr) {} + ALWAYS_INLINE GcRoot(std::nullptr_t) : root_() { + DCHECK(IsNull()); + } explicit ALWAYS_INLINE GcRoot(mirror::CompressedReference<mirror::Object> ref) REQUIRES_SHARED(Locks::mutator_lock_); explicit ALWAYS_INLINE GcRoot(MirrorType* ref) diff --git a/runtime/mirror/object.h b/runtime/mirror/object.h index 0ba545becc..55a30c3a48 100644 --- a/runtime/mirror/object.h +++ b/runtime/mirror/object.h @@ -154,7 +154,7 @@ class MANAGED LOCKABLE Object { void SetLockWord(LockWord new_val, bool as_volatile) REQUIRES_SHARED(Locks::mutator_lock_); bool CasLockWord(LockWord old_val, LockWord new_val, CASMode mode, std::memory_order memory_order) REQUIRES_SHARED(Locks::mutator_lock_); - uint32_t GetLockOwnerThreadId(); + uint32_t GetLockOwnerThreadId() REQUIRES_SHARED(Locks::mutator_lock_); // Try to enter the monitor, returns non null if we succeeded. ObjPtr<mirror::Object> MonitorTryEnter(Thread* self) diff --git a/runtime/mirror/object_reference.h b/runtime/mirror/object_reference.h index ce123fa256..251bcfd89a 100644 --- a/runtime/mirror/object_reference.h +++ b/runtime/mirror/object_reference.h @@ -154,6 +154,9 @@ class MANAGED ObjectReference { explicit ObjectReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_) : reference_(Compression::Compress(mirror_ptr)) { } + ObjectReference() : reference_(0u) { + DCHECK(IsNull()); + } // The encoded reference to a mirror::Object. uint32_t reference_; @@ -219,8 +222,8 @@ static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSiz template<class MirrorType> class MANAGED CompressedReference : public mirror::ObjectReference<false, MirrorType> { public: - CompressedReference<MirrorType>() REQUIRES_SHARED(Locks::mutator_lock_) - : mirror::ObjectReference<false, MirrorType>(nullptr) {} + CompressedReference<MirrorType>() + : mirror::ObjectReference<false, MirrorType>() {} static CompressedReference<MirrorType> FromMirrorPtr(MirrorType* p) REQUIRES_SHARED(Locks::mutator_lock_) { @@ -228,7 +231,7 @@ class MANAGED CompressedReference : public mirror::ObjectReference<false, Mirror } static CompressedReference<MirrorType> FromVRegValue(uint32_t vreg_value) { - CompressedReference<MirrorType> result(nullptr); + CompressedReference<MirrorType> result; result.reference_ = vreg_value; return result; } diff --git a/runtime/obj_ptr.h b/runtime/obj_ptr.h index a03b67bed7..7e52a50582 100644 --- a/runtime/obj_ptr.h +++ b/runtime/obj_ptr.h @@ -48,16 +48,17 @@ class ObjPtr { "must have a least kObjectAlignmentShift bits"); public: - OBJPTR_INLINE ObjPtr() REQUIRES_SHARED(Locks::mutator_lock_) : reference_(0u) {} + OBJPTR_INLINE ObjPtr() : ObjPtr(nullptr) {} + + OBJPTR_INLINE ObjPtr(std::nullptr_t) + : reference_(0u) { + DCHECK(IsNull()); + } // Note: The following constructors allow implicit conversion. This simplifies code that uses // them, e.g., for parameter passing. However, in general, implicit-conversion constructors // are discouraged and detected by clang-tidy. - OBJPTR_INLINE ObjPtr(std::nullptr_t) - REQUIRES_SHARED(Locks::mutator_lock_) - : reference_(0u) {} - template <typename Type, typename = typename std::enable_if_t<std::is_base_of_v<MirrorType, Type>>> OBJPTR_INLINE ObjPtr(Type* ptr) REQUIRES_SHARED(Locks::mutator_lock_); |