eden/src/common/polyfill_thread.h

31 lines
783 B
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
2022-11-21 11:31:18 -05:00
// SPDX-FileCopyrightText: 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
//
// TODO: remove this file when jthread is supported by all compilation targets
//
#pragma once
#include <chrono>
#include <condition_variable>
2022-11-21 11:31:18 -05:00
#include <thread>
#include <utility>
2022-11-21 11:31:18 -05:00
namespace Common {
template <typename Rep, typename Period>
bool StoppableTimedWait(std::stop_token token, const std::chrono::duration<Rep, Period>& rel_time) {
std::condition_variable_any cv;
std::mutex m;
// Perform the timed wait.
std::unique_lock lk{m};
return !cv.wait_for(lk, token, rel_time, [&] { return token.stop_requested(); });
}
2022-11-21 11:31:18 -05:00
} // namespace Common