Repository: google/filament
File: filament/backend/src/vulkan/VulkanAsyncHandles.h (C/C++)

Code before:
            wait(std::chrono::steady_clock::time_point const until) {
        // hold a reference so that our state doesn't disappear while we wait
        std::shared_ptr state{ mState };
        std::unique_lock lock(state->lock);
        state->cond.wait_until(lock, until, [&state] {
            return bool(state->sharedFence) || state->status != FenceStatus::TIMEOUT_EXPIRED;
        });
        // here mSharedFence will be null if we timed out
        return { state->sharedFence, state->status };
    }

    void cancel() const {
        std::shared_ptr const state{ mState };
        std::unique_lock const lock(state->lock);
        if (state->sharedFence) {
            state->sharedFence->cancel();
        }
        state->status = FenceStatus::ERROR;
        state->cond.notify_all();
    }

private:
    struct State {
        std::mutex lock;
        std::condition_variable cond;
        FenceStatus status = FenceStatus::TIMEOUT_EXPIRED;
        std::shared_ptr<VulkanCmdFence> sharedFence;
    };
    std::shared_ptr<State> mState{ std::make_shared<State>() };
};

struct VulkanSync : fvkmemory::ThreadSafeResource, public HwSync {
    struct CallbackData {
        CallbackHandler* handler;
        Platform::SyncCallback cb;
        Platform::Sync* sync;
        void* userData;
    };

    VulkanSync() {}
    std::mutex lock;
    std::vector<std::unique_ptr<CallbackData>> conversionCallbacks;
};

struct VulkanTimerQuery : public HwTimerQuery, fvkmemory::ThreadSafeResource {
    VulkanTimerQuery(uint32_t startingIndex, uint32_t stoppingIndex)
        : mStartingQueryIndex(startingIndex),
          mStoppingQueryIndex(stoppingIndex) {}

    void setFence(std::shared_ptr<VulkanCmdFence> fence) noexcept {
        std::unique_lock const lock(mFenceMutex);

Review comment (suggestion):
We don't really need the additional semantics of FenceStatus. It'll be easier to parse with a well-named boolean like `hasCancelled` IMO.

Code after:
            wait(std::chrono::steady_clock::time_point const until) {
        // hold a reference so that our state doesn't disappear while we wait
        std::shared_ptr state{ mState };
        std::unique_lock lock(state->lock);
        state->cond.wait_until(lock, until, [&state] {
            return bool(state->sharedFence) || state->canceled;
        });
        // here mSharedFence will be null if we timed out
        return { state->sharedFence, state->canceled };
    }

    void cancel() const {
        std::shared_ptr const state{ mState };
        std::unique_lock const lock(state->lock);
        if (state->sharedFence) {
            state->sharedFence->cancel();
        }
        state->canceled = true;
        state->cond.notify_all();
    }

private:
    struct State {
        std::mutex lock;
        std::condition_variable cond;
        bool canceled = false;
        std::shared_ptr<VulkanCmdFence> sharedFence;
    };
    std::shared_ptr<State> mState{ std::make_shared<State>() };
};

struct VulkanSync : fvkmemory::ThreadSafeResource, public HwSync {
    struct CallbackData {
        CallbackHandler* handler;
        Platform::SyncCallback cb;
        Platform::Sync* sync;
        void* userData;
    };

    VulkanSync() {}
    std::mutex lock;
    std::vector<std::unique_ptr<CallbackData>> conversionCallbacks;
};

struct VulkanTimerQuery : public HwTimerQuery, fvkmemory::ThreadSafeResource {
    VulkanTimerQuery(uint32_t startingIndex, uint32_t stoppingIndex)
        : mStartingQueryIndex(startingIndex),
          mStoppingQueryIndex(stoppingIndex) {}

    void setFence(std::shared_ptr<VulkanCmdFence> fence) noexcept {
        std::unique_lock const lock(mFenceMutex);