mirror of
				https://github.com/PabloMK7/citra.git
				synced 2025-10-31 13:50:03 +00:00 
			
		
		
		
	Announce-Service: Add conditional variable for the wait in the announce thread
This commit is contained in:
		
							parent
							
								
									93742f17b3
								
							
						
					
					
						commit
						eba2351f9e
					
				
					 4 changed files with 63 additions and 25 deletions
				
			
		|  | @ -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), finished(true) { | ||||
| AnnounceMultiplayerSession::AnnounceMultiplayerSession() : announce(false) { | ||||
| #ifdef ENABLE_WEB_SERVICE | ||||
|     backend = std::make_unique<WebService::RoomJson>( | ||||
|         Settings::values.announce_multiplayer_room_endpoint_url, Settings::values.citra_username, | ||||
|  | @ -39,19 +39,19 @@ void AnnounceMultiplayerSession::Start() { | |||
| } | ||||
| 
 | ||||
| void AnnounceMultiplayerSession::Stop() { | ||||
|     if (!announce && finished) | ||||
|     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) { | ||||
|         announce_multiplayer_thread->detach(); | ||||
|         cv.notify_all(); | ||||
|         announce_multiplayer_thread->join(); | ||||
|         announce_multiplayer_thread.reset(); | ||||
|         backend->Delete(); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| std::shared_ptr<std::function<void(const Common::WebResult&)>> | ||||
| AnnounceMultiplayerSession::BindErrorCallback( | ||||
| AnnounceMultiplayerSession::CallbackHandle AnnounceMultiplayerSession::BindErrorCallback( | ||||
|     std::function<void(const Common::WebResult&)> function) { | ||||
|     std::lock_guard<std::mutex> lock(callback_mutex); | ||||
|     auto handle = std::make_shared<std::function<void(const Common::WebResult&)>>(function); | ||||
|  | @ -59,8 +59,7 @@ AnnounceMultiplayerSession::BindErrorCallback( | |||
|     return handle; | ||||
| } | ||||
| 
 | ||||
| void AnnounceMultiplayerSession::UnbindErrorCallback( | ||||
|     std::shared_ptr<std::function<void(const Common::WebResult&)>> handle) { | ||||
| void AnnounceMultiplayerSession::UnbindErrorCallback(CallbackHandle handle) { | ||||
|     std::lock_guard<std::mutex> lock(callback_mutex); | ||||
|     error_callbacks.erase(handle); | ||||
| } | ||||
|  | @ -70,13 +69,11 @@ AnnounceMultiplayerSession::~AnnounceMultiplayerSession() { | |||
| } | ||||
| 
 | ||||
| void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { | ||||
|     while (!finished) { | ||||
|         std::this_thread::sleep_for(announce_time_interval / 10); | ||||
|     } | ||||
|     announce = true; | ||||
|     finished = false; | ||||
|     std::future<Common::WebResult> future; | ||||
|     while (announce) { | ||||
|         std::unique_lock<std::mutex> lock(cv_m); | ||||
|         cv.wait_for(lock, announce_time_interval); | ||||
|         std::shared_ptr<Network::Room> room = Network::GetRoom().lock(); | ||||
|         if (!room) { | ||||
|             announce = false; | ||||
|  | @ -107,9 +104,7 @@ void AnnounceMultiplayerSession::AnnounceMultiplayerLoop() { | |||
|                 } | ||||
|             } | ||||
|         } | ||||
|         std::this_thread::sleep_for(announce_time_interval); | ||||
|     } | ||||
|     finished = true; | ||||
| } | ||||
| 
 | ||||
| std::future<AnnounceMultiplayerRoom::RoomList> AnnounceMultiplayerSession::GetRoomList( | ||||
|  |  | |||
|  | @ -5,8 +5,10 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <atomic> | ||||
| #include <condition_variable> | ||||
| #include <functional> | ||||
| #include <memory> | ||||
| #include <mutex> | ||||
| #include <set> | ||||
| #include <thread> | ||||
| #include "common/announce_multiplayer_room.h" | ||||
|  | @ -21,6 +23,7 @@ namespace Core { | |||
|  */ | ||||
| class AnnounceMultiplayerSession : NonCopyable { | ||||
| public: | ||||
|     using CallbackHandle = std::shared_ptr<std::function<void(const Common::WebResult&)>>; | ||||
|     AnnounceMultiplayerSession(); | ||||
|     ~AnnounceMultiplayerSession(); | ||||
| 
 | ||||
|  | @ -29,14 +32,13 @@ public: | |||
|      * @param function The function that gets called | ||||
|      * @return A handle that can be used the unbind the function | ||||
|      */ | ||||
|     std::shared_ptr<std::function<void(const Common::WebResult&)>> BindErrorCallback( | ||||
|         std::function<void(const Common::WebResult&)> function); | ||||
|     CallbackHandle BindErrorCallback(std::function<void(const Common::WebResult&)> function); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Unbind a function from the error callbacks | ||||
|      * @param handle The handle for the function that should get unbind | ||||
|      */ | ||||
|     void UnbindErrorCallback(std::shared_ptr<std::function<void(const Common::WebResult&)>> handle); | ||||
|     void UnbindErrorCallback(CallbackHandle handle); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Starts the announce of a room to web services | ||||
|  | @ -57,13 +59,16 @@ public: | |||
| 
 | ||||
| private: | ||||
|     std::atomic<bool> announce{false}; | ||||
|     std::atomic<bool> finished{true}; | ||||
| 
 | ||||
|     /// 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::set<std::shared_ptr<std::function<void(const Common::WebResult&)>>> error_callbacks; | ||||
|     std::set<CallbackHandle> error_callbacks; | ||||
|     std::unique_ptr<std::thread> announce_multiplayer_thread; | ||||
| 
 | ||||
|     std::unique_ptr<AnnounceMultiplayerRoom::Backend> | ||||
|         backend; ///< Backend interface that logs fields
 | ||||
|     /// Backend interface that logs fields
 | ||||
|     std::unique_ptr<AnnounceMultiplayerRoom::Backend> backend; | ||||
| 
 | ||||
|     void AnnounceMultiplayerLoop(); | ||||
| }; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue