From 17d05b4a19bda299b7bb2ba90e0ec18c22067e1d Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 16 Sep 2019 12:00:25 +0900 Subject: [PATCH] Use mutex for thread safety --- cpp_impl/src/UDPC_Defines.hpp | 2 ++ cpp_impl/src/UDPConnection.cpp | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cpp_impl/src/UDPC_Defines.hpp b/cpp_impl/src/UDPC_Defines.hpp index d03d09a..073eba9 100644 --- a/cpp_impl/src/UDPC_Defines.hpp +++ b/cpp_impl/src/UDPC_Defines.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include "TSQueue.hpp" @@ -256,6 +257,7 @@ public: std::thread thread; std::atomic_bool threadRunning; + std::mutex mutex; }; // struct Context diff --git a/cpp_impl/src/UDPConnection.cpp b/cpp_impl/src/UDPConnection.cpp index 1b9106e..a52dddd 100644 --- a/cpp_impl/src/UDPConnection.cpp +++ b/cpp_impl/src/UDPConnection.cpp @@ -97,7 +97,8 @@ loggingType(INFO), loggingType(WARNING), #endif atostrBufIndex(0), -rng_engine() +rng_engine(), +mutex() { if(isThreaded) { flags.set(0); @@ -796,7 +797,9 @@ void UDPC::threadedUpdate(Context *ctx) { decltype(now) nextNow; while(ctx->threadRunning.load()) { now = std::chrono::steady_clock::now(); + ctx->mutex.lock(); ctx->update_impl(); + ctx->mutex.unlock(); nextNow = std::chrono::steady_clock::now(); std::this_thread::sleep_for(std::chrono::milliseconds(33) - (nextNow - now)); } @@ -907,9 +910,10 @@ void UDPC_client_initiate_connection(UDPC_HContext ctx, UDPC_ConnectionId connec return; } + std::lock_guard lock(c->mutex); + UDPC::ConnectionData newCon(false, c); - // TODO make thread safe by using mutex c->conMap.insert(std::make_pair(connectionId, std::move(newCon))); auto addrConIter = c->addrConMap.find(connectionId.addr); if(addrConIter == c->addrConMap.end()) { @@ -929,6 +933,8 @@ int UDPC_get_queue_send_available(UDPC_HContext ctx, UDPC_ConnectionId connectio return 0; } + std::lock_guard lock(c->mutex); + auto iter = c->conMap.find(connectionId); if(iter != c->conMap.end()) { return iter->second.sendPkts.capacity() - iter->second.sendPkts.size(); @@ -948,6 +954,8 @@ void UDPC_queue_send(UDPC_HContext ctx, UDPC_ConnectionId destinationId, return; } + std::lock_guard lock(c->mutex); + auto iter = c->conMap.find(destinationId); if(iter == c->conMap.end()) { c->log( @@ -974,6 +982,7 @@ int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting) { if(!c) { return 0; } + std::lock_guard lock(c->mutex); return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true); } @@ -983,6 +992,8 @@ int UDPC_drop_connection(UDPC_HContext ctx, UDPC_ConnectionId connectionId, bool return 0; } + std::lock_guard lock(c->mutex); + if(dropAllWithAddr) { auto addrConIter = c->addrConMap.find(connectionId.addr); if(addrConIter != c->addrConMap.end()) { @@ -1025,6 +1036,7 @@ uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id) { if(!c) { return 0; } + std::lock_guard lock(c->mutex); return c->protocolID.exchange(id); } @@ -1041,6 +1053,9 @@ UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx) { if(!c) { return UDPC::get_empty_pinfo(); } + + std::lock_guard lock(c->mutex); + // TODO impl return UDPC::get_empty_pinfo(); }