From a5e63a8c35f02022f2c393c5941f53b2518641fd Mon Sep 17 00:00:00 2001 From: B3n30 Date: Fri, 10 Nov 2017 19:37:26 +0100 Subject: [PATCH] Use Common::Event --- src/core/announce_multiplayer_session.cpp | 23 ++++++++--------------- src/core/announce_multiplayer_session.h | 9 ++------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/core/announce_multiplayer_session.cpp b/src/core/announce_multiplayer_session.cpp index af7d4fe58..b6a224ad6 100644 --- a/src/core/announce_multiplayer_session.cpp +++ b/src/core/announce_multiplayer_session.cpp @@ -19,7 +19,7 @@ namespace Core { // Time between room is announced to web_service static constexpr std::chrono::seconds announce_time_interval(15); -AnnounceMultiplayerSession::AnnounceMultiplayerSession() : announce(false) { +AnnounceMultiplayerSession::AnnounceMultiplayerSession() { #ifdef ENABLE_WEB_SERVICE backend = std::make_unique( Settings::values.announce_multiplayer_room_endpoint_url, Settings::values.citra_username, @@ -33,18 +33,14 @@ void AnnounceMultiplayerSession::Start() { if (announce_multiplayer_thread) { Stop(); } - + shutdown_event.Reset(); announce_multiplayer_thread = std::make_unique(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this); } void AnnounceMultiplayerSession::Stop() { - if (!announce) - return; - announce = false; - // Detaching the loop, to not wait for the sleep to finish. The loop thread will finish soon. if (announce_multiplayer_thread) { - cv.notify_all(); + shutdown_event.Set(); announce_multiplayer_thread->join(); announce_multiplayer_thread.reset(); backend->Delete(); @@ -69,19 +65,16 @@ AnnounceMultiplayerSession::~AnnounceMultiplayerSession() { } void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { - announce = true; + auto update_time = std::chrono::steady_clock::now(); std::future future; - while (announce) { - std::unique_lock lock(cv_m); - cv.wait_for(lock, announce_time_interval); + while (!shutdown_event.WaitUntil(update_time)) { + update_time += announce_time_interval; std::shared_ptr room = Network::GetRoom().lock(); if (!room) { - announce = false; - continue; + break; } if (room->GetState() != Network::Room::State::Open) { - announce = false; - continue; + break; } Network::RoomInformation room_information = room->GetRoomInformation(); std::vector memberlist = room->GetRoomMemberList(); diff --git a/src/core/announce_multiplayer_session.h b/src/core/announce_multiplayer_session.h index 6faea3ae3..0ea357e3a 100644 --- a/src/core/announce_multiplayer_session.h +++ b/src/core/announce_multiplayer_session.h @@ -4,8 +4,6 @@ #pragma once -#include -#include #include #include #include @@ -13,6 +11,7 @@ #include #include "common/announce_multiplayer_room.h" #include "common/common_types.h" +#include "common/thread.h" namespace Core { @@ -58,11 +57,7 @@ public: std::future GetRoomList(std::function func); private: - std::atomic announce{false}; - - /// conditional variable to notify the announce thread to end early - std::condition_variable cv; - std::mutex cv_m; ///< mutex for cv + Common::Event shutdown_event; std::mutex callback_mutex; std::set error_callbacks; std::unique_ptr announce_multiplayer_thread;