Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b1fff84f authored by Elliott Hughes's avatar Elliott Hughes Committed by Android Git Automerger
Browse files

am a6d2ff29: am 110ddef1: Merge "binder: Change from C11 <stdatomic.h> to C++11 <atomic>."

* commit 'a6d2ff29':
  binder: Change from C11 <stdatomic.h> to C++11 <atomic>.
parents 43ab57cc a6d2ff29
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -17,7 +17,7 @@
#ifndef ANDROID_BINDER_H
#ifndef ANDROID_BINDER_H
#define ANDROID_BINDER_H
#define ANDROID_BINDER_H


#include <stdatomic.h>
#include <atomic>
#include <stdint.h>
#include <stdint.h>
#include <binder/IBinder.h>
#include <binder/IBinder.h>


@@ -71,7 +71,7 @@ private:


    class Extras;
    class Extras;


    atomic_uintptr_t    mExtras;  // should be atomic<Extras *>
    std::atomic<Extras*> mExtras;
            void*       mReserved0;
            void*       mReserved0;
};
};


@@ -95,7 +95,7 @@ private:


    IBinder* const          mRemote;
    IBinder* const          mRemote;
    RefBase::weakref_type*  mRefs;
    RefBase::weakref_type*  mRefs;
    volatile int32_t        mState;
    std::atomic<int32_t>    mState;
};
};


}; // namespace android
}; // namespace android
+13 −28
Original line number Original line Diff line number Diff line
@@ -16,7 +16,7 @@


#include <binder/Binder.h>
#include <binder/Binder.h>


#include <stdatomic.h>
#include <atomic>
#include <utils/misc.h>
#include <utils/misc.h>
#include <binder/BpBinder.h>
#include <binder/BpBinder.h>
#include <binder/IInterface.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
bool BBinder::isBinderAlive() const
@@ -139,19 +138,16 @@ void BBinder::attachObject(
    const void* objectID, void* object, void* cleanupCookie,
    const void* objectID, void* object, void* cleanupCookie,
    object_cleanup_func func)
    object_cleanup_func func)
{
{
    Extras* e = reinterpret_cast<Extras*>(
    Extras* e = mExtras.load(std::memory_order_acquire);
                    atomic_load_explicit(&mExtras, memory_order_acquire));


    if (!e) {
    if (!e) {
        e = new Extras;
        e = new Extras;
        uintptr_t expected = 0;
        Extras* expected = nullptr;
        if (!atomic_compare_exchange_strong_explicit(
        if (!mExtras.compare_exchange_strong(expected, e,
                                        &mExtras, &expected,
                                             std::memory_order_release,
                                        reinterpret_cast<uintptr_t>(e),
                                             std::memory_order_acquire)) {
                                        memory_order_release,
                                        memory_order_acquire)) {
            delete e;
            delete e;
            e = reinterpret_cast<Extras*>(expected);  // Filled in by CAS
            e = expected;  // Filled in by CAS
        }
        }
        if (e == 0) return; // out of memory
        if (e == 0) return; // out of memory
    }
    }
@@ -160,18 +156,9 @@ void BBinder::attachObject(
    e->mObjects.attach(objectID, object, cleanupCookie, func);
    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
void* BBinder::findObject(const void* objectID) const
{
{
    Extras* e = reinterpret_cast<Extras*>(
    Extras* e = mExtras.load(std::memory_order_acquire);
                    load_const_atomic(&mExtras, memory_order_acquire));
    if (!e) return NULL;
    if (!e) return NULL;


    AutoMutex _l(e->mLock);
    AutoMutex _l(e->mLock);
@@ -180,8 +167,7 @@ void* BBinder::findObject(const void* objectID) const


void BBinder::detachObject(const void* objectID)
void BBinder::detachObject(const void* objectID)
{
{
    Extras* e = reinterpret_cast<Extras*>(
    Extras* e = mExtras.load(std::memory_order_acquire);
                    atomic_load_explicit(&mExtras, memory_order_acquire));
    if (!e) return;
    if (!e) return;


    AutoMutex _l(e->mLock);
    AutoMutex _l(e->mLock);
@@ -195,8 +181,7 @@ BBinder* BBinder::localBinder()


BBinder::~BBinder()
BBinder::~BBinder()
{
{
    Extras* e = reinterpret_cast<Extras*>(
    Extras* e = mExtras.load(std::memory_order_relaxed);
                    atomic_load_explicit(&mExtras, memory_order_relaxed));
    if (e) delete e;
    if (e) delete e;
}
}


@@ -252,7 +237,7 @@ BpRefBase::BpRefBase(const sp<IBinder>& o)
BpRefBase::~BpRefBase()
BpRefBase::~BpRefBase()
{
{
    if (mRemote) {
    if (mRemote) {
        if (!(mState&kRemoteAcquired)) {
        if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
            mRemote->decStrong(this);
            mRemote->decStrong(this);
        }
        }
        mRefs->decWeak(this);
        mRefs->decWeak(this);
@@ -261,7 +246,7 @@ BpRefBase::~BpRefBase()


void BpRefBase::onFirstRef()
void BpRefBase::onFirstRef()
{
{
    android_atomic_or(kRemoteAcquired, &mState);
    mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
}
}


void BpRefBase::onLastStrongRef(const void* /*id*/)
void BpRefBase::onLastStrongRef(const void* /*id*/)