citra-emu
/
citra
Archived
1
0
Fork 0

Use Common::Event

This commit is contained in:
B3n30 2017-11-10 19:37:26 +01:00
parent 423df498d9
commit a5e63a8c35
2 changed files with 10 additions and 22 deletions

View File

@ -19,7 +19,7 @@ namespace Core {
// Time between room is announced to web_service // Time between room is announced to web_service
static constexpr std::chrono::seconds announce_time_interval(15); static constexpr std::chrono::seconds announce_time_interval(15);
AnnounceMultiplayerSession::AnnounceMultiplayerSession() : announce(false) { AnnounceMultiplayerSession::AnnounceMultiplayerSession() {
#ifdef ENABLE_WEB_SERVICE #ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>( backend = std::make_unique<WebService::RoomJson>(
Settings::values.announce_multiplayer_room_endpoint_url, Settings::values.citra_username, Settings::values.announce_multiplayer_room_endpoint_url, Settings::values.citra_username,
@ -33,18 +33,14 @@ void AnnounceMultiplayerSession::Start() {
if (announce_multiplayer_thread) { if (announce_multiplayer_thread) {
Stop(); Stop();
} }
shutdown_event.Reset();
announce_multiplayer_thread = announce_multiplayer_thread =
std::make_unique<std::thread>(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this); std::make_unique<std::thread>(&AnnounceMultiplayerSession::AnnounceMultiplayerLoop, this);
} }
void AnnounceMultiplayerSession::Stop() { 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) { if (announce_multiplayer_thread) {
cv.notify_all(); shutdown_event.Set();
announce_multiplayer_thread->join(); announce_multiplayer_thread->join();
announce_multiplayer_thread.reset(); announce_multiplayer_thread.reset();
backend->Delete(); backend->Delete();
@ -69,19 +65,16 @@ AnnounceMultiplayerSession::~AnnounceMultiplayerSession() {
} }
void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() {
announce = true; auto update_time = std::chrono::steady_clock::now();
std::future<Common::WebResult> future; std::future<Common::WebResult> future;
while (announce) { while (!shutdown_event.WaitUntil(update_time)) {
std::unique_lock<std::mutex> lock(cv_m); update_time += announce_time_interval;
cv.wait_for(lock, announce_time_interval);
std::shared_ptr<Network::Room> room = Network::GetRoom().lock(); std::shared_ptr<Network::Room> room = Network::GetRoom().lock();
if (!room) { if (!room) {
announce = false; break;
continue;
} }
if (room->GetState() != Network::Room::State::Open) { if (room->GetState() != Network::Room::State::Open) {
announce = false; break;
continue;
} }
Network::RoomInformation room_information = room->GetRoomInformation(); Network::RoomInformation room_information = room->GetRoomInformation();
std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList(); std::vector<Network::Room::Member> memberlist = room->GetRoomMemberList();

View File

@ -4,8 +4,6 @@
#pragma once #pragma once
#include <atomic>
#include <condition_variable>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
@ -13,6 +11,7 @@
#include <thread> #include <thread>
#include "common/announce_multiplayer_room.h" #include "common/announce_multiplayer_room.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/thread.h"
namespace Core { namespace Core {
@ -58,11 +57,7 @@ public:
std::future<AnnounceMultiplayerRoom::RoomList> GetRoomList(std::function<void()> func); std::future<AnnounceMultiplayerRoom::RoomList> GetRoomList(std::function<void()> func);
private: private:
std::atomic<bool> announce{false}; Common::Event shutdown_event;
/// conditional variable to notify the announce thread to end early
std::condition_variable cv;
std::mutex cv_m; ///< mutex for cv
std::mutex callback_mutex; std::mutex callback_mutex;
std::set<CallbackHandle> error_callbacks; std::set<CallbackHandle> error_callbacks;
std::unique_ptr<std::thread> announce_multiplayer_thread; std::unique_ptr<std::thread> announce_multiplayer_thread;