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

Commit d3bba948 authored by Charles Munger's avatar Charles Munger
Browse files

Use relaxed atomic instead of volatile for racy boolean.

This compiles to the same code in practice, but won't invoke undefined
behavior.

Change-Id: I58bb1d51a48d5c3e9029e0834882d666b214bebe
parent 1fc14317
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <utils/Looper.h>

#include <atomic>
#include <sys/eventfd.h>
#include <cinttypes>

@@ -216,13 +217,13 @@ int Looper::pollInner(int timeoutMillis) {
    mResponseIndex = 0;

    // We are about to idle.
    mPolling = true;
    std::atomic_store_explicit(&mPolling, true, std::memory_order_relaxed);

    struct epoll_event eventItems[EPOLL_MAX_EVENTS];
    int eventCount = epoll_wait(mEpollFd.get(), eventItems, EPOLL_MAX_EVENTS, timeoutMillis);

    // No longer idling.
    mPolling = false;
    std::atomic_store_explicit(&mPolling, false, std::memory_order_relaxed);

    // Acquire lock.
    mLock.lock();
@@ -673,7 +674,7 @@ void Looper::removeMessages(const sp<MessageHandler>& handler, int what) {
}

bool Looper::isPolling() const {
    return mPolling;
    return std::atomic_load_explicit(&mPolling, std::memory_order_relaxed);
}

uint32_t Looper::Request::getEpollEvents() const {
+2 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@

#include <android-base/unique_fd.h>

#include <atomic>
#include <unordered_map>
#include <utility>

@@ -479,7 +480,7 @@ private:

    // Whether we are currently waiting for work.  Not protected by a lock,
    // any use of it is racy anyway.
    volatile bool mPolling;
    std::atomic<bool> mPolling;

    android::base::unique_fd mEpollFd;  // guarded by mLock but only modified on the looper thread
    bool mEpollRebuildRequired; // guarded by mLock