diff options
author | Elliott Hughes | 2015-08-20 14:31:35 -0500 |
---|---|---|
committer | Android Git Automerger | 2015-08-20 14:31:35 -0500 |
commit | b1fff84f676d44af8277d68f8122f220538db345 (patch) | |
tree | 16efb60c62402371b8df5ff3e0df91f2d995959e /libs | |
parent | 43ab57ccbf2c0adfad5298b662675b59e52b2e01 (diff) | |
parent | a6d2ff29c4920b440b16bc6ceaa90710361b9be9 (diff) | |
download | frameworks-native-b1fff84f676d44af8277d68f8122f220538db345.tar.gz frameworks-native-b1fff84f676d44af8277d68f8122f220538db345.tar.xz frameworks-native-b1fff84f676d44af8277d68f8122f220538db345.zip |
am a6d2ff29: am 110ddef1: Merge "binder: Change from C11 <stdatomic.h> to C++11 <atomic>."
* commit 'a6d2ff29c4920b440b16bc6ceaa90710361b9be9':
binder: Change from C11 <stdatomic.h> to C++11 <atomic>.
Diffstat (limited to 'libs')
-rw-r--r-- | libs/binder/Binder.cpp | 41 |
1 files changed, 13 insertions, 28 deletions
diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp index 9d200fb21..e39093d52 100644 --- a/libs/binder/Binder.cpp +++ b/libs/binder/Binder.cpp | |||
@@ -16,7 +16,7 @@ | |||
16 | 16 | ||
17 | #include <binder/Binder.h> | 17 | #include <binder/Binder.h> |
18 | 18 | ||
19 | #include <stdatomic.h> | 19 | #include <atomic> |
20 | #include <utils/misc.h> | 20 | #include <utils/misc.h> |
21 | #include <binder/BpBinder.h> | 21 | #include <binder/BpBinder.h> |
22 | #include <binder/IInterface.h> | 22 | #include <binder/IInterface.h> |
@@ -70,9 +70,8 @@ public: | |||
70 | 70 | ||
71 | // --------------------------------------------------------------------------- | 71 | // --------------------------------------------------------------------------- |
72 | 72 | ||
73 | BBinder::BBinder() | 73 | BBinder::BBinder() : mExtras(nullptr) |
74 | { | 74 | { |
75 | atomic_init(&mExtras, static_cast<uintptr_t>(0)); | ||
76 | } | 75 | } |
77 | 76 | ||
78 | bool BBinder::isBinderAlive() const | 77 | bool BBinder::isBinderAlive() const |
@@ -139,19 +138,16 @@ void BBinder::attachObject( | |||
139 | const void* objectID, void* object, void* cleanupCookie, | 138 | const void* objectID, void* object, void* cleanupCookie, |
140 | object_cleanup_func func) | 139 | object_cleanup_func func) |
141 | { | 140 | { |
142 | Extras* e = reinterpret_cast<Extras*>( | 141 | Extras* e = mExtras.load(std::memory_order_acquire); |
143 | atomic_load_explicit(&mExtras, memory_order_acquire)); | ||
144 | 142 | ||
145 | if (!e) { | 143 | if (!e) { |
146 | e = new Extras; | 144 | e = new Extras; |
147 | uintptr_t expected = 0; | 145 | Extras* expected = nullptr; |
148 | if (!atomic_compare_exchange_strong_explicit( | 146 | if (!mExtras.compare_exchange_strong(expected, e, |
149 | &mExtras, &expected, | 147 | std::memory_order_release, |
150 | reinterpret_cast<uintptr_t>(e), | 148 | std::memory_order_acquire)) { |
151 | memory_order_release, | ||
152 | memory_order_acquire)) { | ||
153 | delete e; | 149 | delete e; |
154 | e = reinterpret_cast<Extras*>(expected); // Filled in by CAS | 150 | e = expected; // Filled in by CAS |
155 | } | 151 | } |
156 | if (e == 0) return; // out of memory | 152 | if (e == 0) return; // out of memory |
157 | } | 153 | } |
@@ -160,18 +156,9 @@ void BBinder::attachObject( | |||
160 | e->mObjects.attach(objectID, object, cleanupCookie, func); | 156 | e->mObjects.attach(objectID, object, cleanupCookie, func); |
161 | } | 157 | } |
162 | 158 | ||
163 | // The C11 standard doesn't allow atomic loads from const fields, | ||
164 | // though C++11 does. Fudge it until standards get straightened out. | ||
165 | static inline uintptr_t load_const_atomic(const atomic_uintptr_t* p, | ||
166 | memory_order mo) { | ||
167 | atomic_uintptr_t* non_const_p = const_cast<atomic_uintptr_t*>(p); | ||
168 | return atomic_load_explicit(non_const_p, mo); | ||
169 | } | ||
170 | |||
171 | void* BBinder::findObject(const void* objectID) const | 159 | void* BBinder::findObject(const void* objectID) const |
172 | { | 160 | { |
173 | Extras* e = reinterpret_cast<Extras*>( | 161 | Extras* e = mExtras.load(std::memory_order_acquire); |
174 | load_const_atomic(&mExtras, memory_order_acquire)); | ||
175 | if (!e) return NULL; | 162 | if (!e) return NULL; |
176 | 163 | ||
177 | AutoMutex _l(e->mLock); | 164 | AutoMutex _l(e->mLock); |
@@ -180,8 +167,7 @@ void* BBinder::findObject(const void* objectID) const | |||
180 | 167 | ||
181 | void BBinder::detachObject(const void* objectID) | 168 | void BBinder::detachObject(const void* objectID) |
182 | { | 169 | { |
183 | Extras* e = reinterpret_cast<Extras*>( | 170 | Extras* e = mExtras.load(std::memory_order_acquire); |
184 | atomic_load_explicit(&mExtras, memory_order_acquire)); | ||
185 | if (!e) return; | 171 | if (!e) return; |
186 | 172 | ||
187 | AutoMutex _l(e->mLock); | 173 | AutoMutex _l(e->mLock); |
@@ -195,8 +181,7 @@ BBinder* BBinder::localBinder() | |||
195 | 181 | ||
196 | BBinder::~BBinder() | 182 | BBinder::~BBinder() |
197 | { | 183 | { |
198 | Extras* e = reinterpret_cast<Extras*>( | 184 | Extras* e = mExtras.load(std::memory_order_relaxed); |
199 | atomic_load_explicit(&mExtras, memory_order_relaxed)); | ||
200 | if (e) delete e; | 185 | if (e) delete e; |
201 | } | 186 | } |
202 | 187 | ||
@@ -252,7 +237,7 @@ BpRefBase::BpRefBase(const sp<IBinder>& o) | |||
252 | BpRefBase::~BpRefBase() | 237 | BpRefBase::~BpRefBase() |
253 | { | 238 | { |
254 | if (mRemote) { | 239 | if (mRemote) { |
255 | if (!(mState&kRemoteAcquired)) { | 240 | if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { |
256 | mRemote->decStrong(this); | 241 | mRemote->decStrong(this); |
257 | } | 242 | } |
258 | mRefs->decWeak(this); | 243 | mRefs->decWeak(this); |
@@ -261,7 +246,7 @@ BpRefBase::~BpRefBase() | |||
261 | 246 | ||
262 | void BpRefBase::onFirstRef() | 247 | void BpRefBase::onFirstRef() |
263 | { | 248 | { |
264 | android_atomic_or(kRemoteAcquired, &mState); | 249 | mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); |
265 | } | 250 | } |
266 | 251 | ||
267 | void BpRefBase::onLastStrongRef(const void* /*id*/) | 252 | void BpRefBase::onLastStrongRef(const void* /*id*/) |