From c0df68bb804d8fc4bc75aa077232ea74f0d53b28 Mon Sep 17 00:00:00 2001 From: Hans Boehm Date: Thu, 11 Sep 2014 14:49:09 -0700 Subject: [PATCH] Work around C11 const atomic restrictions. 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 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp index 2554351cc..296a98b0b 100644 --- a/libs/binder/Binder.cpp +++ b/libs/binder/Binder.cpp @@ -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(p); + return atomic_load_explicit(non_const_p, mo); +} + void* BBinder::findObject(const void* objectID) const { Extras* e = reinterpret_cast( - atomic_load_explicit(&mExtras, memory_order_acquire)); + load_const_atomic(&mExtras, memory_order_acquire)); if (!e) return NULL; AutoMutex _l(e->mLock);