From ee288340b039a3b350b8139745814b8eed5c8d09 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Mon, 25 Jan 2021 21:49:16 +0300 Subject: [PATCH] Implement thread_ctrl::scoped_priority RAII priority control (+1, or -1) --- Utilities/Thread.h | 18 ++++++++++++++++++ rpcs3/Emu/Cell/Modules/cellAudio.cpp | 2 +- rpcs3/Emu/Cell/PPUThread.cpp | 2 +- rpcs3/Emu/Cell/SPURecompiler.cpp | 9 +++------ rpcs3/Emu/RSX/RSXThread.cpp | 2 +- rpcs3/rpcs3qt/render_creator.cpp | 2 +- rpcs3/util/logs.cpp | 2 +- 7 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index fb707117dc..6d59291eb3 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -281,6 +281,24 @@ public: // Get current thread stack addr and size static std::pair get_thread_stack(); + // Sets the native thread priority and returns it to zero at destructor + struct scoped_priority + { + explicit scoped_priority(int prio) + { + set_native_priority(prio); + } + + scoped_priority(const scoped_priority&) = delete; + + scoped_priority& operator=(const scoped_priority&) = delete; + + ~scoped_priority() + { + set_native_priority(0); + } + }; + private: // Miscellaneous static const u64 process_affinity_mask; diff --git a/rpcs3/Emu/Cell/Modules/cellAudio.cpp b/rpcs3/Emu/Cell/Modules/cellAudio.cpp index c2d4cdcea7..896c553fed 100644 --- a/rpcs3/Emu/Cell/Modules/cellAudio.cpp +++ b/rpcs3/Emu/Cell/Modules/cellAudio.cpp @@ -600,7 +600,7 @@ void cell_audio_thread::update_config() void cell_audio_thread::operator()() { - thread_ctrl::set_native_priority(1); + thread_ctrl::scoped_priority high_prio(+1); // Allocate ringbuffer ringbuffer.reset(new audio_ringbuffer(cfg)); diff --git a/rpcs3/Emu/Cell/PPUThread.cpp b/rpcs3/Emu/Cell/PPUThread.cpp index f2d3d8579c..d38b71989f 100644 --- a/rpcs3/Emu/Cell/PPUThread.cpp +++ b/rpcs3/Emu/Cell/PPUThread.cpp @@ -2358,7 +2358,7 @@ extern void ppu_initialize(const ppu_module& info) named_thread_group threads(fmt::format("PPUW.%u.", ++g_fxo->get()->index), thread_count, [&]() { // Set low priority - thread_ctrl::set_native_priority(-1); + thread_ctrl::scoped_priority low_prio(-1); for (u32 i = work_cv++; i < workload.size(); i = work_cv++) { diff --git a/rpcs3/Emu/Cell/SPURecompiler.cpp b/rpcs3/Emu/Cell/SPURecompiler.cpp index 6bcc5e1e91..5af3e05c91 100644 --- a/rpcs3/Emu/Cell/SPURecompiler.cpp +++ b/rpcs3/Emu/Cell/SPURecompiler.cpp @@ -434,7 +434,7 @@ void spu_cache::initialize() named_thread_group workers("SPU Worker ", worker_count, [&]() -> uint { // Set low priority - thread_ctrl::set_native_priority(-1); + thread_ctrl::scoped_priority low_prio(-1); // Initialize compiler instances for parallel compilation std::unique_ptr compiler; @@ -521,9 +521,6 @@ void spu_cache::initialize() result++; } - // Restore default priority - thread_ctrl::set_native_priority(0); - return result; }); @@ -6665,7 +6662,7 @@ public: const auto as = byteswap(a); const auto sc = build(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); const auto sh = eval(sc + splat_scalar(b)); - + if (m_use_avx512_icl) { set_vr(op.rt, vpermb(as, sh)); @@ -6678,7 +6675,7 @@ public: const auto sc = build(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); const auto sh = eval(sc - splat_scalar(b)); - + if (m_use_avx512_icl) { set_vr(op.rt, vpermb(a, sh)); diff --git a/rpcs3/Emu/RSX/RSXThread.cpp b/rpcs3/Emu/RSX/RSXThread.cpp index 1eedfbda1e..40165f8b74 100644 --- a/rpcs3/Emu/RSX/RSXThread.cpp +++ b/rpcs3/Emu/RSX/RSXThread.cpp @@ -618,7 +618,7 @@ namespace rsx }); // Raise priority above other threads - thread_ctrl::set_native_priority(1); + thread_ctrl::scoped_priority high_prio(+1); if (g_cfg.core.thread_scheduler_enabled) { diff --git a/rpcs3/rpcs3qt/render_creator.cpp b/rpcs3/rpcs3qt/render_creator.cpp index 172576b025..3f803dd651 100644 --- a/rpcs3/rpcs3qt/render_creator.cpp +++ b/rpcs3/rpcs3qt/render_creator.cpp @@ -38,7 +38,7 @@ render_creator::render_creator(QObject *parent) : QObject(parent) std::thread enum_thread = std::thread([&] { - thread_ctrl::set_native_priority(-1); + thread_ctrl::scoped_priority low_prio(-1); vk::instance device_enum_context; if (device_enum_context.create("RPCS3", true)) diff --git a/rpcs3/util/logs.cpp b/rpcs3/util/logs.cpp index d2361ed1fa..08e5318367 100644 --- a/rpcs3/util/logs.cpp +++ b/rpcs3/util/logs.cpp @@ -421,7 +421,7 @@ logs::file_writer::file_writer(const std::string& name, u64 max_size) m_writer = std::thread([this]() { - thread_ctrl::set_native_priority(-1); + thread_ctrl::scoped_priority low_prio(-1); while (true) {