[qt] use std::jthread instead of playing around with std::thread
All checks were successful
eden-license / license-header (pull_request) Successful in 26s

Signed-off-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
lizzie 2025-09-06 08:01:34 +00:00 committed by crueter
parent c6ba8e5c39
commit 1ec60c5613
7 changed files with 89 additions and 149 deletions

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -18,13 +21,9 @@ public:
: socket(io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0)) {} : socket(io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0)) {}
~FakeCemuhookServer() { ~FakeCemuhookServer() {
is_running = false;
boost::system::error_code error_code; boost::system::error_code error_code;
socket.shutdown(boost::asio::socket_base::shutdown_both, error_code); socket.shutdown(boost::asio::socket_base::shutdown_both, error_code);
socket.close(); socket.close();
if (handler.joinable()) {
handler.join();
}
} }
u16 GetPort() { u16 GetPort() {
@ -41,10 +40,9 @@ public:
sizeof(InputCommon::CemuhookUDP::Message<InputCommon::CemuhookUDP::Response::PadData>); sizeof(InputCommon::CemuhookUDP::Message<InputCommon::CemuhookUDP::Response::PadData>);
REQUIRE(touch_movement_path.size() > 0); REQUIRE(touch_movement_path.size() > 0);
is_running = true; handler = std::jthread([touch_movement_path, this](std::stop_token stoken) {
handler = std::thread([touch_movement_path, this]() {
auto current_touch_position = touch_movement_path.begin(); auto current_touch_position = touch_movement_path.begin();
while (is_running) { while (!stoken.stop_requested()) {
boost::asio::ip::udp::endpoint sender_endpoint; boost::asio::ip::udp::endpoint sender_endpoint;
boost::system::error_code error_code; boost::system::error_code error_code;
auto received_size = socket.receive_from(boost::asio::buffer(receive_buffer), auto received_size = socket.receive_from(boost::asio::buffer(receive_buffer),
@ -87,8 +85,7 @@ private:
boost::asio::ip::udp::socket socket; boost::asio::ip::udp::socket socket;
std::array<u8, InputCommon::CemuhookUDP::MAX_PACKET_SIZE> send_buffer; std::array<u8, InputCommon::CemuhookUDP::MAX_PACKET_SIZE> send_buffer;
std::array<u8, InputCommon::CemuhookUDP::MAX_PACKET_SIZE> receive_buffer; std::array<u8, InputCommon::CemuhookUDP::MAX_PACKET_SIZE> receive_buffer;
bool is_running = false; std::jthread handler;
std::thread handler;
}; };
TEST_CASE("CalibrationConfigurationJob completed", "[input_common]") { TEST_CASE("CalibrationConfigurationJob completed", "[input_common]") {

View file

@ -1,3 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -1492,53 +1494,36 @@ void QtSoftwareKeyboardDialog::MoveTextCursorDirection(Direction direction) {
} }
void QtSoftwareKeyboardDialog::StartInputThread() { void QtSoftwareKeyboardDialog::StartInputThread() {
if (input_thread_running) { input_thread = std::jthread([&](std::stop_token stoken) {
return; while (!stoken.stop_requested()) {
} input_interpreter->PollInput();
HandleButtonPressedOnce<
input_thread_running = true; Core::HID::NpadButton::A, Core::HID::NpadButton::B, Core::HID::NpadButton::X,
Core::HID::NpadButton::Y, Core::HID::NpadButton::StickL, Core::HID::NpadButton::StickR,
input_thread = std::thread(&QtSoftwareKeyboardDialog::InputThread, this); Core::HID::NpadButton::L, Core::HID::NpadButton::R, Core::HID::NpadButton::Plus,
Core::HID::NpadButton::Left, Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown, Core::HID::NpadButton::StickRLeft,
Core::HID::NpadButton::StickRUp, Core::HID::NpadButton::StickRRight,
Core::HID::NpadButton::StickRDown>();
HandleButtonHold<Core::HID::NpadButton::B, Core::HID::NpadButton::L,
Core::HID::NpadButton::R, Core::HID::NpadButton::Left,
Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown, Core::HID::NpadButton::StickRLeft,
Core::HID::NpadButton::StickRUp, Core::HID::NpadButton::StickRRight,
Core::HID::NpadButton::StickRDown>();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});
} }
void QtSoftwareKeyboardDialog::StopInputThread() { void QtSoftwareKeyboardDialog::StopInputThread() {
input_thread_running = false; input_thread.request_stop();
if (input_interpreter)
if (input_thread.joinable()) {
input_thread.join();
}
if (input_interpreter) {
input_interpreter->ResetButtonStates(); input_interpreter->ResetButtonStates();
}
}
void QtSoftwareKeyboardDialog::InputThread() {
while (input_thread_running) {
input_interpreter->PollInput();
HandleButtonPressedOnce<
Core::HID::NpadButton::A, Core::HID::NpadButton::B, Core::HID::NpadButton::X,
Core::HID::NpadButton::Y, Core::HID::NpadButton::StickL, Core::HID::NpadButton::StickR,
Core::HID::NpadButton::L, Core::HID::NpadButton::R, Core::HID::NpadButton::Plus,
Core::HID::NpadButton::Left, Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown, Core::HID::NpadButton::StickRLeft,
Core::HID::NpadButton::StickRUp, Core::HID::NpadButton::StickRRight,
Core::HID::NpadButton::StickRDown>();
HandleButtonHold<Core::HID::NpadButton::B, Core::HID::NpadButton::L,
Core::HID::NpadButton::R, Core::HID::NpadButton::Left,
Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown, Core::HID::NpadButton::StickRLeft,
Core::HID::NpadButton::StickRUp, Core::HID::NpadButton::StickRRight,
Core::HID::NpadButton::StickRDown>();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
} }
QtSoftwareKeyboard::QtSoftwareKeyboard(GMainWindow& main_window) { QtSoftwareKeyboard::QtSoftwareKeyboard(GMainWindow& main_window) {

View file

@ -1,3 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -182,9 +184,6 @@ private:
void StartInputThread(); void StartInputThread();
void StopInputThread(); void StopInputThread();
/// The thread where input is being polled and processed.
void InputThread();
std::unique_ptr<Ui::QtSoftwareKeyboardDialog> ui; std::unique_ptr<Ui::QtSoftwareKeyboardDialog> ui;
Core::System& system; Core::System& system;
@ -220,10 +219,7 @@ private:
std::atomic<bool> caps_lock_enabled{false}; std::atomic<bool> caps_lock_enabled{false};
std::unique_ptr<InputInterpreter> input_interpreter; std::unique_ptr<InputInterpreter> input_interpreter;
std::jthread input_thread;
std::thread input_thread;
std::atomic<bool> input_thread_running{};
}; };
class QtSoftwareKeyboard final : public QObject, public Core::Frontend::SoftwareKeyboardApplet { class QtSoftwareKeyboard final : public QObject, public Core::Frontend::SoftwareKeyboardApplet {

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -282,54 +285,41 @@ void QtNXWebEngineView::SendKeyPressEvent(int key) {
} }
void QtNXWebEngineView::StartInputThread() { void QtNXWebEngineView::StartInputThread() {
if (input_thread_running) { input_thread = std::jthread([&](std::stop_token stoken) {
return; // Wait for 1 second before allowing any inputs to be processed.
} std::this_thread::sleep_for(std::chrono::seconds(1));
if (is_local) {
QWidget::grabKeyboard();
}
while (!stoken.stop_requested()) {
input_interpreter->PollInput();
input_thread_running = true; HandleWindowFooterButtonPressedOnce<Core::HID::NpadButton::A, Core::HID::NpadButton::B,
input_thread = std::thread(&QtNXWebEngineView::InputThread, this); Core::HID::NpadButton::X, Core::HID::NpadButton::Y,
Core::HID::NpadButton::L, Core::HID::NpadButton::R>();
HandleWindowKeyButtonPressedOnce<
Core::HID::NpadButton::Left, Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown>();
HandleWindowKeyButtonHold<
Core::HID::NpadButton::Left, Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown>();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});
} }
void QtNXWebEngineView::StopInputThread() { void QtNXWebEngineView::StopInputThread() {
if (is_local) { if (is_local) {
QWidget::releaseKeyboard(); QWidget::releaseKeyboard();
} }
input_thread.request_stop();
input_thread_running = false;
if (input_thread.joinable()) {
input_thread.join();
}
}
void QtNXWebEngineView::InputThread() {
// Wait for 1 second before allowing any inputs to be processed.
std::this_thread::sleep_for(std::chrono::seconds(1));
if (is_local) {
QWidget::grabKeyboard();
}
while (input_thread_running) {
input_interpreter->PollInput();
HandleWindowFooterButtonPressedOnce<Core::HID::NpadButton::A, Core::HID::NpadButton::B,
Core::HID::NpadButton::X, Core::HID::NpadButton::Y,
Core::HID::NpadButton::L, Core::HID::NpadButton::R>();
HandleWindowKeyButtonPressedOnce<
Core::HID::NpadButton::Left, Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown>();
HandleWindowKeyButtonHold<
Core::HID::NpadButton::Left, Core::HID::NpadButton::Up, Core::HID::NpadButton::Right,
Core::HID::NpadButton::Down, Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLUp, Core::HID::NpadButton::StickLRight,
Core::HID::NpadButton::StickLDown>();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
} }
void QtNXWebEngineView::LoadExtractedFonts() { void QtNXWebEngineView::LoadExtractedFonts() {

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -155,9 +158,6 @@ private:
void StartInputThread(); void StartInputThread();
void StopInputThread(); void StopInputThread();
/// The thread where input is being polled and processed.
void InputThread();
/// Loads the extracted fonts using JavaScript. /// Loads the extracted fonts using JavaScript.
void LoadExtractedFonts(); void LoadExtractedFonts();
@ -165,24 +165,13 @@ private:
void FocusFirstLinkElement(); void FocusFirstLinkElement();
InputCommon::InputSubsystem* input_subsystem; InputCommon::InputSubsystem* input_subsystem;
std::unique_ptr<UrlRequestInterceptor> url_interceptor; std::unique_ptr<UrlRequestInterceptor> url_interceptor;
std::unique_ptr<InputInterpreter> input_interpreter; std::unique_ptr<InputInterpreter> input_interpreter;
std::jthread input_thread;
std::thread input_thread;
std::atomic<bool> input_thread_running{};
std::atomic<bool> finished{}; std::atomic<bool> finished{};
Service::AM::Frontend::WebExitReason exit_reason{Service::AM::Frontend::WebExitReason::EndButtonPressed};
Service::AM::Frontend::WebExitReason exit_reason{
Service::AM::Frontend::WebExitReason::EndButtonPressed};
std::string last_url{"http://localhost/"}; std::string last_url{"http://localhost/"};
bool is_local{}; bool is_local{};
QWebEngineProfile* default_profile; QWebEngineProfile* default_profile;
QWebEngineSettings* global_settings; QWebEngineSettings* global_settings;
}; };

View file

@ -1,3 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -231,34 +233,20 @@ void OverlayDialog::TranslateButtonPress(Core::HID::NpadButton button) {
} }
void OverlayDialog::StartInputThread() { void OverlayDialog::StartInputThread() {
if (input_thread_running) { input_thread = std::jthread([&](std::stop_token stoken) {
return; while (!stoken.stop_requested()) {
} input_interpreter->PollInput();
HandleButtonPressedOnce<Core::HID::NpadButton::A, Core::HID::NpadButton::B,
input_thread_running = true; Core::HID::NpadButton::Left, Core::HID::NpadButton::Right,
Core::HID::NpadButton::StickLLeft,
input_thread = std::thread(&OverlayDialog::InputThread, this); Core::HID::NpadButton::StickLRight>();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});
} }
void OverlayDialog::StopInputThread() { void OverlayDialog::StopInputThread() {
input_thread_running = false; input_thread.request_stop();
if (input_thread.joinable()) {
input_thread.join();
}
}
void OverlayDialog::InputThread() {
while (input_thread_running) {
input_interpreter->PollInput();
HandleButtonPressedOnce<Core::HID::NpadButton::A, Core::HID::NpadButton::B,
Core::HID::NpadButton::Left, Core::HID::NpadButton::Right,
Core::HID::NpadButton::StickLLeft,
Core::HID::NpadButton::StickLRight>();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
} }
void OverlayDialog::keyPressEvent(QKeyEvent* e) { void OverlayDialog::keyPressEvent(QKeyEvent* e) {

View file

@ -1,9 +1,10 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include <atomic>
#include <memory> #include <memory>
#include <thread> #include <thread>
@ -91,9 +92,6 @@ private:
void StartInputThread(); void StartInputThread();
void StopInputThread(); void StopInputThread();
/// The thread where input is being polled and processed.
void InputThread();
void keyPressEvent(QKeyEvent* e) override; void keyPressEvent(QKeyEvent* e) override;
std::unique_ptr<Ui::OverlayDialog> ui; std::unique_ptr<Ui::OverlayDialog> ui;
@ -101,8 +99,5 @@ private:
bool use_rich_text; bool use_rich_text;
std::unique_ptr<InputInterpreter> input_interpreter; std::unique_ptr<InputInterpreter> input_interpreter;
std::jthread input_thread;
std::thread input_thread;
std::atomic<bool> input_thread_running{};
}; };