From 6775da34cd195d499d70f80a7ee9aec1df9da09f Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 12 Mar 2025 15:20:01 +0900 Subject: [PATCH] Use std::shared_mutex for peerPKWhitelist 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 | 4 ++-- src/UDPConnection.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/UDPC_Defines.hpp b/src/UDPC_Defines.hpp index 8da68fd..a9cca97 100644 --- a/src/UDPC_Defines.hpp +++ b/src/UDPC_Defines.hpp @@ -25,12 +25,12 @@ #include #include #include -#include #include #include #include #include #include +#include #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]; diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 617778f..b3edfc7 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -1351,7 +1351,7 @@ void UDPC::Context::update_impl() { recvBuf + UDPC_MIN_HEADER_SIZE + 4, crypto_sign_PUBLICKEYBYTES); { - std::lock_guard + std::shared_lock 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::shared_lock 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 pkWhitelistLock(c->peerPKWhitelistMutex); + std::unique_lock 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 pkWhitelistLock(c->peerPKWhitelistMutex); + std::shared_lock 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 pkWhitelistLock(c->peerPKWhitelistMutex); + std::unique_lock 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 pkWhitelistLock(c->peerPKWhitelistMutex); + std::unique_lock pkWhitelistLock(c->peerPKWhitelistMutex); c->peerPKWhitelist.clear(); return 1; } -- 2.49.0