From 88b7541d682a6ad402eff15e58c9c792497096d7 Mon Sep 17 00:00:00 2001 From: Hans Boehm Date: Wed, 23 Jul 2014 11:35:13 -0700 Subject: [PATCH] Remove incorrect android_atomic_...64 use. Change the mExtras field in Binder.h to be a stdatomic.h atomic value, and replace references to it with proper stdatomic.h calls. This removes one of a small number of remaining 64 bit android_atomic references. It also replaces the erroneously non-atomic read accesses to mExtras. It would be better if this used the C++11 facility, but we don't quite have that yet. Bug: 16513433 Change-Id: Ibabb88d05025187ee1ce6c7f1aa670b133a547f8 --- include/binder/Binder.h | 3 ++- libs/binder/Binder.cpp | 32 ++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/include/binder/Binder.h b/include/binder/Binder.h index ba3ac4b99..16f2c4a8e 100644 --- a/include/binder/Binder.h +++ b/include/binder/Binder.h @@ -17,6 +17,7 @@ #ifndef ANDROID_BINDER_H #define ANDROID_BINDER_H +#include #include // --------------------------------------------------------------------------- @@ -69,7 +70,7 @@ private: class Extras; - Extras* mExtras; + atomic_uintptr_t mExtras; // should be atomic void* mReserved0; }; diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp index 71e62ab2b..d9d4971db 100644 --- a/libs/binder/Binder.cpp +++ b/libs/binder/Binder.cpp @@ -16,7 +16,7 @@ #include -#include +#include #include #include #include @@ -71,8 +71,8 @@ public: // --------------------------------------------------------------------------- BBinder::BBinder() - : mExtras(NULL) { + atomic_init(&mExtras, 0); } bool BBinder::isBinderAlive() const @@ -139,19 +139,19 @@ void BBinder::attachObject( const void* objectID, void* object, void* cleanupCookie, object_cleanup_func func) { - Extras* e = mExtras; + Extras* e = reinterpret_cast( + atomic_load_explicit(&mExtras, memory_order_acquire)); if (!e) { e = new Extras; -#ifdef __LP64__ - if (android_atomic_release_cas64(0, reinterpret_cast(e), - reinterpret_cast(&mExtras)) != 0) { -#else - if (android_atomic_cmpxchg(0, reinterpret_cast(e), - reinterpret_cast(&mExtras)) != 0) { -#endif + uintptr_t* expected = 0; + if (!atomic_compare_exchange_strong_explicit( + &mExtras, &expected, + reinterpret_cast(e), + memory_order_release, + memory_order_acquire)) { delete e; - e = mExtras; + e = reinterpret_cast(expected); // Filled in by CAS } if (e == 0) return; // out of memory } @@ -162,7 +162,8 @@ void BBinder::attachObject( void* BBinder::findObject(const void* objectID) const { - Extras* e = mExtras; + Extras* e = reinterpret_cast( + atomic_load_explicit(&mExtras, memory_order_acquire)); if (!e) return NULL; AutoMutex _l(e->mLock); @@ -171,7 +172,8 @@ void* BBinder::findObject(const void* objectID) const void BBinder::detachObject(const void* objectID) { - Extras* e = mExtras; + Extras* e = reinterpret_cast( + atomic_load_explicit(&mExtras, memory_order_acquire)); if (!e) return; AutoMutex _l(e->mLock); @@ -185,7 +187,9 @@ BBinder* BBinder::localBinder() BBinder::~BBinder() { - if (mExtras) delete mExtras; + Extras* e = reinterpret_cast( + atomic_load_explicit(&mExtras, memory_order_relaxed)); + if (e) delete e; }