summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2019-03-17 11:05:38 +0100
committerMichael Lutz <michi@icosahedron.de>2019-04-06 11:27:39 +0200
commit94c5269fa772304112b7c40d2c84794056f87717 (patch)
tree0f1eebc855cf74cbedad734e02f19f7b4903567b /src/network
parent05bc2ed7cbe07cb4cd535932f10778b35f72e944 (diff)
downloadopenttd-94c5269fa772304112b7c40d2c84794056f87717.tar.xz
Codechange: Use atomic variables for thread synchronization where useful.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/network_gamelist.cpp19
1 files changed, 8 insertions, 11 deletions
diff --git a/src/network/network_gamelist.cpp b/src/network/network_gamelist.cpp
index c67fba5ec..e9fc5f943 100644
--- a/src/network/network_gamelist.cpp
+++ b/src/network/network_gamelist.cpp
@@ -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_delayed_insertion_list = NULL;
*/
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);