Changeset - r23588:660d91a03749
[Not reviewed]
master
0 2 0
Michael Lutz - 6 years ago 2019-03-17 10:05:38
michi@icosahedron.de
Codechange: Use atomic variables for thread synchronization where useful.
2 files changed with 19 insertions and 22 deletions:
0 comments (0 inline, 0 general)
src/network/network_gamelist.cpp
Show inline comments
 
@@ -18,16 +18,14 @@
 
#include "network_internal.h"
 
#include "network_udp.h"
 
#include "network_gamelist.h"
 
#include <mutex>
 
#include <atomic>
 

	
 
#include "../safeguards.h"
 

	
 
NetworkGameList *_network_game_list = NULL;
 

	
 
/** Mutex for handling delayed insertion/querying of servers. */
 
static std::mutex _network_game_list_mutex;
 
/** The games to insert when the GUI thread has time for us. */
 
static NetworkGameList *_network_game_delayed_insertion_list = NULL;
 
static std::atomic<NetworkGameList *> _network_game_delayed_insertion_list(NULL);
 

	
 
/**
 
 * Add a new item to the linked gamelist, but do it delayed in the next tick
 
@@ -36,18 +34,17 @@ static NetworkGameList *_network_game_de
 
 */
 
void NetworkGameListAddItemDelayed(NetworkGameList *item)
 
{
 
	std::lock_guard<std::mutex> lock(_network_game_list_mutex);
 
	item->next = _network_game_delayed_insertion_list;
 
	_network_game_delayed_insertion_list = item;
 
	item->next = _network_game_delayed_insertion_list.load(std::memory_order_relaxed);
 
	while (!_network_game_delayed_insertion_list.compare_exchange_weak(item->next, item, std::memory_order_acq_rel)) {}
 
}
 

	
 
/** Perform the delayed (thread safe) insertion into the game list */
 
static void NetworkGameListHandleDelayedInsert()
 
{
 
	std::lock_guard<std::mutex> lock(_network_game_list_mutex);
 
	while (_network_game_delayed_insertion_list != NULL) {
 
		NetworkGameList *ins_item = _network_game_delayed_insertion_list;
 
		_network_game_delayed_insertion_list = ins_item->next;
 
	while (true) {
 
		NetworkGameList *ins_item = _network_game_delayed_insertion_list.load(std::memory_order_relaxed);
 
		while (ins_item != NULL && !_network_game_delayed_insertion_list.compare_exchange_weak(ins_item, ins_item->next, std::memory_order_acq_rel)) {}
 
		if (ins_item == NULL) break; // No item left.
 

	
 
		NetworkGameList *item = NetworkGameListAddItem(ins_item->address);
 

	
src/saveload/saveload.cpp
Show inline comments
 
@@ -45,6 +45,7 @@
 
#include "../string_func.h"
 
#include "../fios.h"
 
#include "../error.h"
 
#include <atomic>
 

	
 
#include "table/strings.h"
 

	
 
@@ -370,9 +371,9 @@ void NORETURN SlErrorCorruptFmt(const ch
 
}
 

	
 

	
 
typedef void (*AsyncSaveFinishProc)();                ///< Callback for when the savegame loading is finished.
 
static AsyncSaveFinishProc _async_save_finish = NULL; ///< Callback to call when the savegame loading is finished.
 
static std::thread _save_thread;                      ///< The thread we're using to compress and write a savegame
 
typedef void (*AsyncSaveFinishProc)();                      ///< Callback for when the savegame loading is finished.
 
static std::atomic<AsyncSaveFinishProc> _async_save_finish; ///< Callback to call when the savegame loading is finished.
 
static std::thread _save_thread;                            ///< The thread we're using to compress and write a savegame
 

	
 
/**
 
 * Called by save thread to tell we finished saving.
 
@@ -381,9 +382,9 @@ static std::thread _save_thread;        
 
static void SetAsyncSaveFinish(AsyncSaveFinishProc proc)
 
{
 
	if (_exit_game) return;
 
	while (_async_save_finish != NULL) CSleep(10);
 

	
 
	_async_save_finish = proc;
 
	while (_async_save_finish.load(std::memory_order_acquire) != NULL) CSleep(10);
 

	
 
	_async_save_finish.store(proc, std::memory_order_release);
 
}
 

	
 
/**
 
@@ -391,11 +392,10 @@ static void SetAsyncSaveFinish(AsyncSave
 
 */
 
void ProcessAsyncSaveFinish()
 
{
 
	if (_async_save_finish == NULL) return;
 

	
 
	_async_save_finish();
 

	
 
	_async_save_finish = NULL;
 
	AsyncSaveFinishProc proc = _async_save_finish.exchange(NULL, std::memory_order_acq_rel);
 
	if (proc == NULL) return;
 

	
 
	proc();
 

	
 
	if (_save_thread.joinable()) {
 
		_save_thread.join();
0 comments (0 inline, 0 general)