Loading include/binder/Binder.h +3 −3 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,7 @@ #ifndef ANDROID_BINDER_H #ifndef ANDROID_BINDER_H #define ANDROID_BINDER_H #define ANDROID_BINDER_H #include <stdatomic.h> #include <atomic> #include <stdint.h> #include <stdint.h> #include <binder/IBinder.h> #include <binder/IBinder.h> Loading Loading @@ -71,7 +71,7 @@ private: class Extras; class Extras; atomic_uintptr_t mExtras; // should be atomic<Extras *> std::atomic<Extras*> mExtras; void* mReserved0; void* mReserved0; }; }; Loading @@ -95,7 +95,7 @@ private: IBinder* const mRemote; IBinder* const mRemote; RefBase::weakref_type* mRefs; RefBase::weakref_type* mRefs; volatile int32_t mState; std::atomic<int32_t> mState; }; }; }; // namespace android }; // namespace android Loading libs/binder/Binder.cpp +13 −28 Original line number Original line Diff line number Diff line Loading @@ -16,7 +16,7 @@ #include <binder/Binder.h> #include <binder/Binder.h> #include <stdatomic.h> #include <atomic> #include <utils/misc.h> #include <utils/misc.h> #include <binder/BpBinder.h> #include <binder/BpBinder.h> #include <binder/IInterface.h> #include <binder/IInterface.h> Loading Loading @@ -70,9 +70,8 @@ public: // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- BBinder::BBinder() BBinder::BBinder() : mExtras(nullptr) { { atomic_init(&mExtras, static_cast<uintptr_t>(0)); } } bool BBinder::isBinderAlive() const bool BBinder::isBinderAlive() const Loading Loading @@ -139,19 +138,16 @@ void BBinder::attachObject( const void* objectID, void* object, void* cleanupCookie, const void* objectID, void* object, void* cleanupCookie, object_cleanup_func func) object_cleanup_func func) { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_acquire); atomic_load_explicit(&mExtras, memory_order_acquire)); if (!e) { if (!e) { e = new Extras; e = new Extras; uintptr_t expected = 0; Extras* expected = nullptr; if (!atomic_compare_exchange_strong_explicit( if (!mExtras.compare_exchange_strong(expected, e, &mExtras, &expected, std::memory_order_release, reinterpret_cast<uintptr_t>(e), std::memory_order_acquire)) { memory_order_release, memory_order_acquire)) { delete e; delete e; e = reinterpret_cast<Extras*>(expected); // Filled in by CAS e = expected; // Filled in by CAS } } if (e == 0) return; // out of memory if (e == 0) return; // out of memory } } Loading @@ -160,18 +156,9 @@ void BBinder::attachObject( e->mObjects.attach(objectID, object, cleanupCookie, func); e->mObjects.attach(objectID, object, cleanupCookie, func); } } // The C11 standard doesn't allow atomic loads from const fields, // though C++11 does. Fudge it until standards get straightened out. static inline uintptr_t load_const_atomic(const atomic_uintptr_t* p, memory_order mo) { atomic_uintptr_t* non_const_p = const_cast<atomic_uintptr_t*>(p); return atomic_load_explicit(non_const_p, mo); } void* BBinder::findObject(const void* objectID) const void* BBinder::findObject(const void* objectID) const { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_acquire); load_const_atomic(&mExtras, memory_order_acquire)); if (!e) return NULL; if (!e) return NULL; AutoMutex _l(e->mLock); AutoMutex _l(e->mLock); Loading @@ -180,8 +167,7 @@ void* BBinder::findObject(const void* objectID) const void BBinder::detachObject(const void* objectID) void BBinder::detachObject(const void* objectID) { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_acquire); atomic_load_explicit(&mExtras, memory_order_acquire)); if (!e) return; if (!e) return; AutoMutex _l(e->mLock); AutoMutex _l(e->mLock); Loading @@ -195,8 +181,7 @@ BBinder* BBinder::localBinder() BBinder::~BBinder() BBinder::~BBinder() { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_relaxed); atomic_load_explicit(&mExtras, memory_order_relaxed)); if (e) delete e; if (e) delete e; } } Loading Loading @@ -252,7 +237,7 @@ BpRefBase::BpRefBase(const sp<IBinder>& o) BpRefBase::~BpRefBase() BpRefBase::~BpRefBase() { { if (mRemote) { if (mRemote) { if (!(mState&kRemoteAcquired)) { if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { mRemote->decStrong(this); mRemote->decStrong(this); } } mRefs->decWeak(this); mRefs->decWeak(this); Loading @@ -261,7 +246,7 @@ BpRefBase::~BpRefBase() void BpRefBase::onFirstRef() void BpRefBase::onFirstRef() { { android_atomic_or(kRemoteAcquired, &mState); mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); } } void BpRefBase::onLastStrongRef(const void* /*id*/) void BpRefBase::onLastStrongRef(const void* /*id*/) Loading Loading
include/binder/Binder.h +3 −3 Original line number Original line Diff line number Diff line Loading @@ -17,7 +17,7 @@ #ifndef ANDROID_BINDER_H #ifndef ANDROID_BINDER_H #define ANDROID_BINDER_H #define ANDROID_BINDER_H #include <stdatomic.h> #include <atomic> #include <stdint.h> #include <stdint.h> #include <binder/IBinder.h> #include <binder/IBinder.h> Loading Loading @@ -71,7 +71,7 @@ private: class Extras; class Extras; atomic_uintptr_t mExtras; // should be atomic<Extras *> std::atomic<Extras*> mExtras; void* mReserved0; void* mReserved0; }; }; Loading @@ -95,7 +95,7 @@ private: IBinder* const mRemote; IBinder* const mRemote; RefBase::weakref_type* mRefs; RefBase::weakref_type* mRefs; volatile int32_t mState; std::atomic<int32_t> mState; }; }; }; // namespace android }; // namespace android Loading
libs/binder/Binder.cpp +13 −28 Original line number Original line Diff line number Diff line Loading @@ -16,7 +16,7 @@ #include <binder/Binder.h> #include <binder/Binder.h> #include <stdatomic.h> #include <atomic> #include <utils/misc.h> #include <utils/misc.h> #include <binder/BpBinder.h> #include <binder/BpBinder.h> #include <binder/IInterface.h> #include <binder/IInterface.h> Loading Loading @@ -70,9 +70,8 @@ public: // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- BBinder::BBinder() BBinder::BBinder() : mExtras(nullptr) { { atomic_init(&mExtras, static_cast<uintptr_t>(0)); } } bool BBinder::isBinderAlive() const bool BBinder::isBinderAlive() const Loading Loading @@ -139,19 +138,16 @@ void BBinder::attachObject( const void* objectID, void* object, void* cleanupCookie, const void* objectID, void* object, void* cleanupCookie, object_cleanup_func func) object_cleanup_func func) { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_acquire); atomic_load_explicit(&mExtras, memory_order_acquire)); if (!e) { if (!e) { e = new Extras; e = new Extras; uintptr_t expected = 0; Extras* expected = nullptr; if (!atomic_compare_exchange_strong_explicit( if (!mExtras.compare_exchange_strong(expected, e, &mExtras, &expected, std::memory_order_release, reinterpret_cast<uintptr_t>(e), std::memory_order_acquire)) { memory_order_release, memory_order_acquire)) { delete e; delete e; e = reinterpret_cast<Extras*>(expected); // Filled in by CAS e = expected; // Filled in by CAS } } if (e == 0) return; // out of memory if (e == 0) return; // out of memory } } Loading @@ -160,18 +156,9 @@ void BBinder::attachObject( e->mObjects.attach(objectID, object, cleanupCookie, func); e->mObjects.attach(objectID, object, cleanupCookie, func); } } // The C11 standard doesn't allow atomic loads from const fields, // though C++11 does. Fudge it until standards get straightened out. static inline uintptr_t load_const_atomic(const atomic_uintptr_t* p, memory_order mo) { atomic_uintptr_t* non_const_p = const_cast<atomic_uintptr_t*>(p); return atomic_load_explicit(non_const_p, mo); } void* BBinder::findObject(const void* objectID) const void* BBinder::findObject(const void* objectID) const { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_acquire); load_const_atomic(&mExtras, memory_order_acquire)); if (!e) return NULL; if (!e) return NULL; AutoMutex _l(e->mLock); AutoMutex _l(e->mLock); Loading @@ -180,8 +167,7 @@ void* BBinder::findObject(const void* objectID) const void BBinder::detachObject(const void* objectID) void BBinder::detachObject(const void* objectID) { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_acquire); atomic_load_explicit(&mExtras, memory_order_acquire)); if (!e) return; if (!e) return; AutoMutex _l(e->mLock); AutoMutex _l(e->mLock); Loading @@ -195,8 +181,7 @@ BBinder* BBinder::localBinder() BBinder::~BBinder() BBinder::~BBinder() { { Extras* e = reinterpret_cast<Extras*>( Extras* e = mExtras.load(std::memory_order_relaxed); atomic_load_explicit(&mExtras, memory_order_relaxed)); if (e) delete e; if (e) delete e; } } Loading Loading @@ -252,7 +237,7 @@ BpRefBase::BpRefBase(const sp<IBinder>& o) BpRefBase::~BpRefBase() BpRefBase::~BpRefBase() { { if (mRemote) { if (mRemote) { if (!(mState&kRemoteAcquired)) { if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { mRemote->decStrong(this); mRemote->decStrong(this); } } mRefs->decWeak(this); mRefs->decWeak(this); Loading @@ -261,7 +246,7 @@ BpRefBase::~BpRefBase() void BpRefBase::onFirstRef() void BpRefBase::onFirstRef() { { android_atomic_or(kRemoteAcquired, &mState); mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); } } void BpRefBase::onLastStrongRef(const void* /*id*/) void BpRefBase::onLastStrongRef(const void* /*id*/) Loading