]> git.seodisparate.com - UDPConnection/commitdiff
Use mutex for thread safety
authorStephen Seo <seo.disparate@gmail.com>
Mon, 16 Sep 2019 03:00:25 +0000 (12:00 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 16 Sep 2019 03:00:25 +0000 (12:00 +0900)
cpp_impl/src/UDPC_Defines.hpp
cpp_impl/src/UDPConnection.cpp

index d03d09a4dc815ba0bab6516d87ededea057d845e..073eba9fc07af2a2f9bbd3289f1e89249e702c8f 100644 (file)
@@ -27,6 +27,7 @@
 #include <random>
 #include <memory>
 #include <thread>
+#include <mutex>
 #include <iostream>
 
 #include "TSQueue.hpp"
@@ -256,6 +257,7 @@ public:
 
     std::thread thread;
     std::atomic_bool threadRunning;
+    std::mutex mutex;
 
 }; // struct Context
 
index 1b9106e8aa9c8ff46b1121bbb7c8f1f1b7978e4a..a52dddddbf2fcbd80f07487fda0770a67e0a28b9 100644 (file)
@@ -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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(c->mutex);
+
     // TODO impl
     return UDPC::get_empty_pinfo();
 }