diff options
-rw-r--r-- | Binder.cpp | 41 | ||||
-rw-r--r-- | include/hwbinder/Binder.h | 6 |
2 files changed, 16 insertions, 31 deletions
@@ -16,7 +16,7 @@ #include <binder/Binder.h> -#include <stdatomic.h> +#include <atomic> #include <utils/misc.h> #include <binder/BpBinder.h> #include <binder/IInterface.h> @@ -70,9 +70,8 @@ public: // --------------------------------------------------------------------------- -BBinder::BBinder() +BBinder::BBinder() : mExtras(nullptr) { - atomic_init(&mExtras, static_cast<uintptr_t>(0)); } bool BBinder::isBinderAlive() const @@ -139,19 +138,16 @@ void BBinder::attachObject( const void* objectID, void* object, void* cleanupCookie, object_cleanup_func func) { - Extras* e = reinterpret_cast<Extras*>( - atomic_load_explicit(&mExtras, memory_order_acquire)); + Extras* e = mExtras.load(std::memory_order_acquire); if (!e) { e = new Extras; - uintptr_t expected = 0; - if (!atomic_compare_exchange_strong_explicit( - &mExtras, &expected, - reinterpret_cast<uintptr_t>(e), - memory_order_release, - memory_order_acquire)) { + Extras* expected = nullptr; + if (!mExtras.compare_exchange_strong(expected, e, + std::memory_order_release, + std::memory_order_acquire)) { delete e; - e = reinterpret_cast<Extras*>(expected); // Filled in by CAS + e = expected; // Filled in by CAS } if (e == 0) return; // out of memory } @@ -160,18 +156,9 @@ void BBinder::attachObject( 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 { - Extras* e = reinterpret_cast<Extras*>( - load_const_atomic(&mExtras, memory_order_acquire)); + Extras* e = mExtras.load(std::memory_order_acquire); if (!e) return NULL; AutoMutex _l(e->mLock); @@ -180,8 +167,7 @@ void* BBinder::findObject(const void* objectID) const void BBinder::detachObject(const void* objectID) { - Extras* e = reinterpret_cast<Extras*>( - atomic_load_explicit(&mExtras, memory_order_acquire)); + Extras* e = mExtras.load(std::memory_order_acquire); if (!e) return; AutoMutex _l(e->mLock); @@ -195,8 +181,7 @@ BBinder* BBinder::localBinder() BBinder::~BBinder() { - Extras* e = reinterpret_cast<Extras*>( - atomic_load_explicit(&mExtras, memory_order_relaxed)); + Extras* e = mExtras.load(std::memory_order_relaxed); if (e) delete e; } @@ -252,7 +237,7 @@ BpRefBase::BpRefBase(const sp<IBinder>& o) BpRefBase::~BpRefBase() { if (mRemote) { - if (!(mState&kRemoteAcquired)) { + if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { mRemote->decStrong(this); } mRefs->decWeak(this); @@ -261,7 +246,7 @@ BpRefBase::~BpRefBase() void BpRefBase::onFirstRef() { - android_atomic_or(kRemoteAcquired, &mState); + mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); } void BpRefBase::onLastStrongRef(const void* /*id*/) diff --git a/include/hwbinder/Binder.h b/include/hwbinder/Binder.h index 86628a0..f849fd4 100644 --- a/include/hwbinder/Binder.h +++ b/include/hwbinder/Binder.h @@ -17,7 +17,7 @@ #ifndef ANDROID_BINDER_H #define ANDROID_BINDER_H -#include <stdatomic.h> +#include <atomic> #include <stdint.h> #include <binder/IBinder.h> @@ -71,7 +71,7 @@ private: class Extras; - atomic_uintptr_t mExtras; // should be atomic<Extras *> + std::atomic<Extras*> mExtras; void* mReserved0; }; @@ -95,7 +95,7 @@ private: IBinder* const mRemote; RefBase::weakref_type* mRefs; - volatile int32_t mState; + std::atomic<int32_t> mState; }; }; // namespace android |