From 071880f5cbc2a051f99cd61226f4b63ebcb6997d Mon Sep 17 00:00:00 2001 From: Andy Hung Date: Wed, 21 Feb 2024 17:56:48 -0800 Subject: [PATCH] RefBase: update sp and wp to allow constinit A constexpr ctor is useful for static initialization. We also modernize the unit test for static variable best practices. Test: atest libutils_binder_test Change-Id: If42c0939fb1a2dcb8eb101ed0e10051f67e2150d --- libutils/binder/RefBase_test.cpp | 8 ++++---- libutils/binder/include/utils/RefBase.h | 2 +- libutils/binder/include/utils/StrongPointer.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libutils/binder/RefBase_test.cpp b/libutils/binder/RefBase_test.cpp index d67559805..65d40a2a1 100644 --- a/libutils/binder/RefBase_test.cpp +++ b/libutils/binder/RefBase_test.cpp @@ -300,8 +300,8 @@ private: std::atomic* mDeleteCount; }; -static sp buffer; -static std::atomic bufferFull(false); +[[clang::no_destroy]] static constinit sp buffer; +static constinit std::atomic bufferFull(false); // Wait until bufferFull has value val. static inline void waitFor(bool val) { @@ -380,8 +380,8 @@ TEST(RefBase, RacingDestructors) { } // Otherwise this is slow and probably pointless on a uniprocessor. } -static wp wpBuffer; -static std::atomic wpBufferFull(false); +[[clang::no_destroy]] static constinit wp wpBuffer; +static constinit std::atomic wpBufferFull(false); // Wait until wpBufferFull has value val. static inline void wpWaitFor(bool val) { diff --git a/libutils/binder/include/utils/RefBase.h b/libutils/binder/include/utils/RefBase.h index 5e3fa7d13..f03e1be0a 100644 --- a/libutils/binder/include/utils/RefBase.h +++ b/libutils/binder/include/utils/RefBase.h @@ -404,7 +404,7 @@ class wp public: typedef typename RefBase::weakref_type weakref_type; - inline wp() : m_ptr(nullptr), m_refs(nullptr) { } + inline constexpr wp() : m_ptr(nullptr), m_refs(nullptr) { } // if nullptr, returns nullptr // diff --git a/libutils/binder/include/utils/StrongPointer.h b/libutils/binder/include/utils/StrongPointer.h index 43c00c95a..fb9b8e899 100644 --- a/libutils/binder/include/utils/StrongPointer.h +++ b/libutils/binder/include/utils/StrongPointer.h @@ -30,7 +30,7 @@ template class wp; template class sp { public: - inline sp() : m_ptr(nullptr) { } + inline constexpr sp() : m_ptr(nullptr) { } // The old way of using sp<> was like this. This is bad because it relies // on implicit conversion to sp<>, which we would like to remove (if an