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

Commit 6b85354e authored by Greg Kaiser's avatar Greg Kaiser Committed by Android (Google) Code Review
Browse files

Merge "SharedBuffer: Refactor release() logic"

parents e9039889 476dbc48
Loading
Loading
Loading
Loading
+17 −7
Original line number Diff line number Diff line
@@ -111,16 +111,26 @@ void SharedBuffer::acquire() const {

int32_t SharedBuffer::release(uint32_t flags) const
{
    int32_t prev = 1;
    if (onlyOwner()
            || (((prev = mRefs.fetch_sub(1, std::memory_order_release)) == 1)
                && (atomic_thread_fence(std::memory_order_acquire), true))) {
    const bool useDealloc = ((flags & eKeepStorage) == 0);
    if (onlyOwner()) {
        // Since we're the only owner, our reference count goes to zero.
        mRefs.store(0, std::memory_order_relaxed);
        if ((flags & eKeepStorage) == 0) {
            free(const_cast<SharedBuffer*>(this));
        if (useDealloc) {
            dealloc(this);
        }
        // As the only owner, our previous reference count was 1.
        return 1;
    }
    // There's multiple owners, we need to use an atomic decrement.
    int32_t prevRefCount = mRefs.fetch_sub(1, std::memory_order_release);
    if (prevRefCount == 1) {
        // We're the last reference, we need the acquire fence.
        atomic_thread_fence(std::memory_order_acquire);
        if (useDealloc) {
            dealloc(this);
        }
    }
    return prev;
    return prevRefCount;
}