OSDN Git Service

Work around C11 const atomic restrictions.
authorHans Boehm <hboehm@google.com>
Thu, 11 Sep 2014 21:49:09 +0000 (14:49 -0700)
committerHans Boehm <hboehm@google.com>
Thu, 11 Sep 2014 21:49:09 +0000 (14:49 -0700)
Cast away the const qualifier in BBinder::findObject.  C11, unlike C++11,
does not allow loads from const atomics.  This is widely regarded as
a bug (see WG14 DR 459).  This is a hack to work around it until it's
officially fixed in the spec.

load_const_atomic was adapted from commit
1e8587a479fd8b1ce9b594298a93f517816e8f15

I don't think we want to dignify this by putting it into a header file.

Bug:17067219
Change-Id: Icbfcbda2722e6f80d2bb065a0bb3ec7634bcacb2

libs/binder/Binder.cpp

index 2554351..296a98b 100644 (file)
@@ -160,10 +160,18 @@ 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*>(
-                    atomic_load_explicit(&mExtras, memory_order_acquire));
+                    load_const_atomic(&mExtras, memory_order_acquire));
     if (!e) return NULL;
 
     AutoMutex _l(e->mLock);