]> git.seodisparate.com - UDPConnection/commitdiff
Use std::shared_mutex for peerPKWhitelist
authorStephen Seo <seo.disparate@gmail.com>
Wed, 12 Mar 2025 06:20:01 +0000 (15:20 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 12 Mar 2025 06:20:01 +0000 (15:20 +0900)
Unlike most mutex usages in UDPC, this mutex sometimes does read-only
operations, so it makes sense to use a std::shared_mutex here.

src/UDPC_Defines.hpp
src/UDPConnection.cpp

index 8da68fdca306c7b301a3304d7def6f51bb5b0155..a9cca9787903354c79a03bdb17622031acb990e2 100644 (file)
 #include <deque>
 #include <unordered_map>
 #include <unordered_set>
-#include <queue>
 #include <random>
 #include <memory>
 #include <thread>
 #include <mutex>
 #include <iostream>
+#include <shared_mutex>
 
 #include "TSLQueue.hpp"
 #include "UDPC.h"
@@ -266,7 +266,7 @@ public:
     std::thread thread;
     std::atomic_bool threadRunning;
     std::mutex conMapMutex;
-    std::mutex peerPKWhitelistMutex;
+    std::shared_mutex peerPKWhitelistMutex;
 
     std::chrono::milliseconds threadedSleepTime;
     unsigned char sk[crypto_sign_SECRETKEYBYTES];
index 617778fb115cc69a1bf01b3f29218986c32ca706..b3edfc71ad9cd5b76e6e4109dcf5cb7f07346eed 100644 (file)
@@ -1351,7 +1351,7 @@ void UDPC::Context::update_impl() {
                         recvBuf + UDPC_MIN_HEADER_SIZE + 4,
                         crypto_sign_PUBLICKEYBYTES);
                     {
-                        std::lock_guard<std::mutex>
+                        std::shared_lock<std::shared_mutex>
                             pkWhitelistLock(peerPKWhitelistMutex);
                         if(!peerPKWhitelist.empty()
                                 && peerPKWhitelist.find(
@@ -1484,7 +1484,7 @@ void UDPC::Context::update_impl() {
                         recvBuf + UDPC_MIN_HEADER_SIZE + 4,
                         crypto_sign_PUBLICKEYBYTES);
                     {
-                        std::lock_guard<std::mutex>
+                        std::shared_lock<std::shared_mutex>
                             pkWhitelistLock(peerPKWhitelistMutex);
                         if(!peerPKWhitelist.empty()
                                 && peerPKWhitelist.find(
@@ -2635,7 +2635,7 @@ int UDPC_add_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk) {
         return 0;
     }
 
-    std::lock_guard<std::mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
+    std::unique_lock<std::shared_mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
     auto result = c->peerPKWhitelist.insert(UDPC::PKContainer(pk));
     if(result.second) {
         return c->peerPKWhitelist.size();
@@ -2649,7 +2649,7 @@ int UDPC_has_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk) {
         return 0;
     }
 
-    std::lock_guard<std::mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
+    std::shared_lock<std::shared_mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
     if(c->peerPKWhitelist.find(UDPC::PKContainer(pk)) != c->peerPKWhitelist.end()) {
         return 1;
     }
@@ -2662,7 +2662,7 @@ int UDPC_remove_whitelist_pk(UDPC_HContext ctx, const unsigned char *pk) {
         return 0;
     }
 
-    std::lock_guard<std::mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
+    std::unique_lock<std::shared_mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
     if(c->peerPKWhitelist.erase(UDPC::PKContainer(pk)) != 0) {
         return 1;
     }
@@ -2675,7 +2675,7 @@ int UDPC_clear_whitelist(UDPC_HContext ctx) {
         return 0;
     }
 
-    std::lock_guard<std::mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
+    std::unique_lock<std::shared_mutex> pkWhitelistLock(c->peerPKWhitelistMutex);
     c->peerPKWhitelist.clear();
     return 1;
 }