]> git.seodisparate.com - UDPConnection/commitdiff
Use mutex when enabling/disabling threaded-update
authorStephen Seo <seo.disparate@gmail.com>
Thu, 11 Jan 2024 10:00:27 +0000 (19:00 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 11 Jan 2024 10:00:27 +0000 (19:00 +0900)
src/UDPC_Defines.hpp
src/UDPConnection.cpp

index 010482202cc968728000398db8980366b182e92f..4a6df9f6c38f3ca475e18ba765f3e5962c7e50e9 100644 (file)
@@ -276,6 +276,8 @@ public:
     std::mutex atostrBufIndexMutex;
     std::uint32_t atostrBufIndex;
 
+    std::mutex setThreadedUpdateMutex;
+
 }; // struct Context
 
 Context *verifyContext(UDPC_HContext ctx);
index d5c358b853c7963561ac1ef3eee036acaa44c2bb..1c049b70e869a8ad35445c6a3aa2d060ff9dc49e 100644 (file)
@@ -257,7 +257,8 @@ peerPKWhitelistMutex(),
 threadedSleepTime(std::chrono::milliseconds(UDPC_UPDATE_MS_DEFAULT)),
 keysSet(),
 atostrBufIndexMutex(),
-atostrBufIndex(0)
+atostrBufIndex(0),
+setThreadedUpdateMutex()
 {
     std::memset(atostrBuf, 0, UDPC_ATOSTR_SIZE);
 
@@ -2207,7 +2208,13 @@ UDPC_HContext UDPC_init_threaded_update_ms(
 
 int UDPC_enable_threaded_update(UDPC_HContext ctx) {
     UDPC::Context *c = UDPC::verifyContext(ctx);
-    if(!c || c->isAutoUpdating.load() || c->thread.joinable()) {
+    if (!c) {
+        return 0;
+    }
+
+    std::lock_guard<std::mutex> setThreadedLock(c->setThreadedUpdateMutex);
+
+    if(c->isAutoUpdating.load() || c->thread.joinable()) {
         return 0;
     }
 
@@ -2222,7 +2229,13 @@ int UDPC_enable_threaded_update(UDPC_HContext ctx) {
 
 int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) {
     UDPC::Context *c = UDPC::verifyContext(ctx);
-    if(!c || c->isAutoUpdating.load() || c->thread.joinable()) {
+    if (!c) {
+        return 0;
+    }
+
+    std::lock_guard<std::mutex> setThreadedLock(c->setThreadedUpdateMutex);
+
+    if(c->isAutoUpdating.load() || c->thread.joinable()) {
         return 0;
     }
 
@@ -2243,7 +2256,13 @@ int UDPC_enable_threaded_update_ms(UDPC_HContext ctx, int updateMS) {
 
 int UDPC_disable_threaded_update(UDPC_HContext ctx) {
     UDPC::Context *c = UDPC::verifyContext(ctx);
-    if(!c || !c->isAutoUpdating.load() || !c->thread.joinable()) {
+    if (!c) {
+        return 0;
+    }
+
+    std::lock_guard<std::mutex> setThreadedLock(c->setThreadedUpdateMutex);
+
+    if(!c->isAutoUpdating.load() || !c->thread.joinable()) {
         return 0;
     }