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
static constexpr std::chrono::seconds announce_time_interval(15);
AnnounceMultiplayerSession::AnnounceMultiplayerSession() : announce(false) {
AnnounceMultiplayerSession::AnnounceMultiplayerSession() {
#ifdef ENABLE_WEB_SERVICE
backend = std::make_unique<WebService::RoomJson>(
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<std::thread>(&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<Common::WebResult> future;
while (announce) {
std::unique_lock<std::mutex> lock(cv_m);
cv.wait_for(lock, announce_time_interval);
while (!shutdown_event.WaitUntil(update_time)) {
update_time += announce_time_interval;
std::shared_ptr<Network::Room> 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<Network::Room::Member> memberlist = room->GetRoomMemberList();

View file

@ -4,8 +4,6 @@
#pragma once
#include <atomic>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
@ -13,6 +11,7 @@
#include <thread>
#include "common/announce_multiplayer_room.h"
#include "common/common_types.h"
#include "common/thread.h"
namespace Core {
@ -58,11 +57,7 @@ public:
std::future<AnnounceMultiplayerRoom::RoomList> GetRoomList(std::function<void()> func);
private:
std::atomic<bool> 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<CallbackHandle> error_callbacks;
std::unique_ptr<std::thread> announce_multiplayer_thread;