]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
clang-tidy: replace lock_guard with scoped_lock 15788/head
authorRosen Penev <rosenp@gmail.com>
Fri, 4 Jul 2025 00:53:55 +0000 (17:53 -0700)
committerRosen Penev <rosenp@gmail.com>
Mon, 7 Jul 2025 17:58:47 +0000 (10:58 -0700)
Found with modernize-use-scoped-lock

Signed-off-by: Rosen Penev <rosenp@gmail.com>
21 files changed:
modules/bindbackend/bindbackend2.cc
modules/gmysqlbackend/smysql.cc
modules/lmdbbackend/lmdbbackend.cc
pdns/auth-main.cc
pdns/dnsdistdist/dnsdist-backend.cc
pdns/dnsdistdist/dnsdist-nghttp2.cc
pdns/dnsdistdist/dnsdist-tcp.cc
pdns/dnsdistdist/dolog.cc
pdns/ixfrdist.cc
pdns/lock.hh
pdns/logger.cc
pdns/malloctrace.cc
pdns/malloctrace.hh
pdns/recursordist/logging.cc
pdns/recursordist/nod.cc
pdns/recursordist/nod.hh
pdns/recursordist/rec-main.cc
pdns/recursordist/rec_channel_rec.cc
pdns/rfc2136handler.cc
pdns/speedtest.cc
pdns/ueberbackend.cc

index 6f211491cd50e802d6606dea818a4fef081b867f..c53db4b37bd8483f747bb9043c5c9b56b7870009 100644 (file)
@@ -826,7 +826,7 @@ Bind2Backend::Bind2Backend(const string& suffix, bool loadZones)
   if (!loadZones && d_hybrid)
     return;
 
-  std::lock_guard<std::mutex> l(s_startup_lock);
+  auto lock = std::scoped_lock(s_startup_lock);
 
   setupDNSSEC();
   if (s_first == 0) {
@@ -1499,7 +1499,7 @@ bool Bind2Backend::createSecondaryDomain(const string& ipAddress, const ZoneName
         << "' from autoprimary " << ipAddress << endl;
 
   {
-    std::lock_guard<std::mutex> l2(s_autosecondary_config_lock);
+    auto lock = std::scoped_lock(s_autosecondary_config_lock);
 
     ofstream c_of(getArg("autoprimary-config").c_str(), std::ios::app);
     if (!c_of) {
index ae5add9a4ed5410fdf437ace3389611fedc0eeb7..668b12480778ee6ffe011efd62cb61e5c75ad01d 100644 (file)
@@ -487,7 +487,7 @@ void SMySQL::connect()
   int retry = 1;
 
   {
-    std::lock_guard<std::mutex> l(s_myinitlock);
+    auto lock = std::scoped_lock(s_myinitlock);
     if (d_threadCleanup) {
       threadcloser.enable();
     }
index 300b6bf2c1180883e78dc1a1477f8a21c437aff8..c933f9d7762c98549f09842ebcd44367f58e9b69 100644 (file)
@@ -716,7 +716,7 @@ LMDBBackend::LMDBBackend(const std::string& suffix)
   bool opened = false;
 
   if (s_first) {
-    std::lock_guard<std::mutex> l(s_lmdbStartupLock);
+    auto lock = std::scoped_lock(s_lmdbStartupLock);
     if (s_first) {
       auto filename = getArg("filename");
 
index 7b967f5a309b627e179307771727d5874c275e62..017285ec8f969d665655f5ec26f3374215007944 100644 (file)
@@ -1023,7 +1023,7 @@ static string DLRestHandler(const vector<string>& parts, pid_t /* ppid */)
   }
   line.append(1, '\n');
 
-  std::lock_guard<std::mutex> l(g_guardian_lock);
+  auto lock = std::scoped_lock(g_guardian_lock);
 
   try {
     writen2(g_fd1[1], line.c_str(), line.size() + 1);
index ab32818a2060f3ff328494fc6790d04f61c96238..c93b9e561b33256ce58104c2f6f9e5633cfe6cd5 100644 (file)
@@ -236,7 +236,7 @@ void DownstreamState::stop()
   d_stopped = true;
 
   {
-    std::lock_guard<std::mutex> tl(connectLock);
+    auto tlock = std::scoped_lock(connectLock);
     auto slock = mplexer.lock();
 
     for (auto& fd : sockets) {
index 338858f9138bb4ac0223714383315c0b766296a2..9b9e6f9b70f2220352f186037c9230c8a75fa60e 100644 (file)
@@ -888,7 +888,7 @@ static void dohClientThread(pdns::channel::Receiver<CrossProtocolQuery>&& receiv
           if (g_dohStatesDumpRequested > 0) {
             /* just to keep things clean in the output, debug only */
             static std::mutex s_lock;
-            std::lock_guard<decltype(s_lock)> lck(s_lock);
+            auto lock = std::scoped_lock(s_lock);
             if (g_dohStatesDumpRequested > 0) {
               /* no race here, we took the lock so it can only be increased in the meantime */
               --g_dohStatesDumpRequested;
@@ -983,7 +983,7 @@ void DoHClientCollection::addThread()
     auto [sender, receiver] = pdns::channel::createObjectQueue<CrossProtocolQuery>(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, internalPipeBufferSize);
 
     vinfolog("Adding DoH Client thread");
-    std::lock_guard<std::mutex> lock(d_mutex);
+    auto lock = std::scoped_lock(d_mutex);
 
     if (d_numberOfThreads >= d_clientThreads.size()) {
       vinfolog("Adding a new DoH client thread would exceed the vector size (%d/%d), skipping. Consider increasing the maximum amount of DoH client threads with setMaxDoHClientThreads() in the configuration.", d_numberOfThreads, d_clientThreads.size());
index 7e12e67c233805cfba0279ed34db707ef250213b..4f78f49d89c64565f1653778ea54d3453747b046 100644 (file)
@@ -1632,7 +1632,7 @@ static void dumpTCPStates(const TCPClientThreadData& data)
 {
   /* just to keep things clean in the output, debug only */
   static std::mutex s_lock;
-  std::lock_guard<decltype(s_lock)> lck(s_lock);
+  auto lock = std::scoped_lock(s_lock);
   if (g_tcpStatesDumpRequested > 0) {
     /* no race here, we took the lock so it can only be increased in the meantime */
     --g_tcpStatesDumpRequested;
index fabfdbebccd075de7273710990229af809e32609..74059197db59bc1e0f547911f2a3c35883f662a8 100644 (file)
@@ -73,7 +73,7 @@ void logTime(std::ostream& stream)
     {
       // strftime is not thread safe, it can access locale information
       static std::mutex mutex;
-      auto lock = std::lock_guard(mutex);
+      auto lock = std::scoped_lock(mutex);
 
       if (strftime(buffer.data(), buffer.size(), timeFormat, &localNow) == 0) {
         buffer[0] = '\0';
index d50c5597dba222cef2974271bf2ef37bf71432b8..e167554f212ea91569abf502472d8d6f0ba22637 100644 (file)
@@ -1129,7 +1129,7 @@ static void handleTCPRequest(int fd, boost::any&) {
   }
 
   {
-    std::lock_guard<std::mutex> lg(g_tcpRequestFDsMutex);
+    auto lock = std::scoped_lock(g_tcpRequestFDsMutex);
     g_tcpRequestFDs.push({cfd, saddr});
   }
   g_tcpHandlerCV.notify_one();
index 254c2f87b999e5573efc72ef4801774deb02ce83..13e28d5aaf442dbce9f1e6ff3121a992d7cf5021 100644 (file)
@@ -248,7 +248,7 @@ public:
   }
 
 private:
-  std::lock_guard<std::mutex> d_lock;
+  std::scoped_lock<std::mutex> d_lock;
   T& d_value;
 };
 
@@ -353,7 +353,7 @@ public:
   }
 
 private:
-  std::lock_guard<std::recursive_mutex> d_lock;
+  std::scoped_lock<std::recursive_mutex> d_lock;
   T& d_value;
 };
 
@@ -458,7 +458,7 @@ public:
   }
 
 private:
-  std::lock_guard<std::shared_mutex> d_lock;
+  std::scoped_lock<std::shared_mutex> d_lock;
   T& d_value;
 };
 
index 77a4ce5fd42d737bdbd2bb0b027032048ebba409..483a4d0089e6603a2a1da7e9265daf248e7633ea 100644 (file)
@@ -104,7 +104,7 @@ void Logger::log(const string& msg, Urgency u) noexcept
     }
 
     static std::mutex mutex;
-    std::lock_guard<std::mutex> lock(mutex); // the C++-2011 spec says we need this, and OSX actually does
+    auto lock = std::scoped_lock(mutex); // the C++-2011 spec says we need this, and OSX actually does
 
     // To avoid issuing multiple syscalls, we write the complete line to clog with a single << call.
     // For that we need a buffer allocated, we might want to use writev(2) one day to avoid that.
index 4fe49c544cd5012d6d1883a49679b16bb2e0be56..a6f4546c424f75b0c7a49bbb7bc23ff23e1344d3 100644 (file)
@@ -73,7 +73,7 @@ void* MallocTracer::malloc(size_t size)
     d_totAllocated += size;
 
 
-    std::lock_guard<std::mutex> lock(d_mut);
+    auto lock = std::scoped_lock(d_mut);
     auto& ent=d_stats[makeBacktrace()];
     ent.count++;
     ent.sizes[size]++;
@@ -89,7 +89,7 @@ void MallocTracer::free(void* ptr)
   __libc_free(ptr);
   if(!l_active) {
     l_active=true;
-    std::lock_guard<std::mutex> lock(d_mut);
+    auto lock = std::scoped_lock(d_mut);
     auto f = d_sizes.find(ptr);
     if(f != d_sizes.end()) {
       d_totAllocated -= f->second;
@@ -144,7 +144,7 @@ std::string MallocTracer::topAllocatorsString(int num)
 void MallocTracer::clearAllocators()
 {
   l_active=true;
-  std::lock_guard<std::mutex> lock(d_mut); 
+  auto lock = std::scoped_lock(d_mut); 
   d_stats.clear(); 
   l_active=false;
 }
index 22b531afaa6f716e6673de8e6e1613c994662837..73e3775abca49a23cefdd8b62f1afbf61c90feb7 100644 (file)
@@ -40,7 +40,7 @@ public:
   uint64_t getAllocs(const std::string& = std::string()) const { return d_allocs; }
   uint64_t getAllocFlux(const std::string& = std::string()) const { return d_allocflux; }
   uint64_t getTotAllocated(const std::string& = std::string()) const { return d_totAllocated; }
-  uint64_t getNumOut() { std::lock_guard<std::mutex> lock(d_mut); return d_sizes.size(); }
+  uint64_t getNumOut() { std::scoped_lock<std::mutex> lock(d_mut); return d_sizes.size(); }
   struct AllocStats
   {
     int count;
index 544350406aee6a3e185b1e903d04f2ed62028dd9..af188f45ef3913de41568c1c231143eda4abfc60 100644 (file)
@@ -162,7 +162,7 @@ const char* Logging::toTimestampStringMilli(const struct timeval& tval, std::arr
   if (format != "%s") {
     // strftime is not thread safe, it can access locale information
     static std::mutex mutex;
-    auto lock = std::lock_guard(mutex);
+    auto lock = std::scoped_lock(mutex);
     struct tm theTime // clang-format insists on formatting it like this
       {};
     len = strftime(buf.data(), buf.size(), format.c_str(), localtime_r(&tval.tv_sec, &theTime));
index 9e4c1dfa730e5aa715db230aeb24a3819f2b91f0..6d0f01c44ac5cadc78a63b010221b1ffadd491b4 100644 (file)
@@ -40,7 +40,7 @@ namespace filesystem = boost::filesystem;
 
 std::mutex PersistentSBF::d_cachedir_mutex;
 
-void PersistentSBF::remove_tmp_files(const filesystem::path& path, std::lock_guard<std::mutex>& /* lock */)
+void PersistentSBF::remove_tmp_files(const filesystem::path& path, std::scoped_lock<std::mutex>& /* lock */)
 {
   Regex file_regex(d_prefix + ".*\\." + bf_suffix + "\\..{8}$");
   for (const auto& file : filesystem::directory_iterator(path)) {
@@ -58,7 +58,7 @@ void PersistentSBF::remove_tmp_files(const filesystem::path& path, std::lock_gua
 bool PersistentSBF::init(bool ignore_pid)
 {
   auto log = g_slog->withName("nod");
-  std::lock_guard<std::mutex> lock(d_cachedir_mutex);
+  std::scoped_lock<std::mutex> lock(d_cachedir_mutex);
   if (d_cachedir.length() != 0) {
     filesystem::path path(d_cachedir);
     try {
index 1cc8f456dca3860b88aecf6c373b2707eb42f517..fbefbb9acada5d36ffb0f0cb2fab6b4cc1281e80 100644 (file)
@@ -64,7 +64,7 @@ public:
   }
 
 private:
-  void remove_tmp_files(const boost::filesystem::path&, std::lock_guard<std::mutex>&);
+  void remove_tmp_files(const boost::filesystem::path&, std::scoped_lock<std::mutex>&);
 
   LockGuarded<bf::stableBF> d_sbf; // Stable Bloom Filter
   std::string d_cachedir;
index 009940d50b5dfa119f3da4966d627f688ac6d173..73f111a17e8862505ea001d4be118be53840ab1d 100644 (file)
@@ -1528,7 +1528,7 @@ static std::mutex pipeBroadCastMutex{};
 void broadcastFunction(const pipefunc_t& func)
 {
   // we do not want the handler and web code to use pipes simultaneously
-  std::lock_guard lock(pipeBroadCastMutex);
+  std::scoped_lock lock(pipeBroadCastMutex);
 
   /* This function might be called by the worker with t_id not inited during startup
      for the initialization of ACLs and domain maps. After that it should only
@@ -1620,7 +1620,7 @@ T broadcastAccFunction(const std::function<T*()>& func)
   }
 
   // we do not want the handler and web code to use pipes simultaneously
-  std::lock_guard lock(pipeBroadCastMutex);
+  std::scoped_lock lock(pipeBroadCastMutex);
 
   unsigned int thread = 0;
   T ret = T();
@@ -3363,7 +3363,7 @@ int main(int argc, char** argv)
 
     ret = serviceMain(startupLog);
     {
-      std::lock_guard lock(g_doneRunning.mutex);
+      std::scoped_lock lock(g_doneRunning.mutex);
       g_doneRunning.done = true;
       g_doneRunning.condVar.notify_one();
     }
index e29803f8d0cbaf56114f78ac59956e0f1d9a54dc..6d4f44b11f57b73a3642114a4f0219de552d6b55 100644 (file)
@@ -969,7 +969,7 @@ static uint64_t doGetThreadCPUMsec(int n)
   static time_t last = 0;
   static ThreadTimes tt;
 
-  std::lock_guard<std::mutex> l(s_mut);
+  auto lock = std::scoped_lock(s_mut);
   if (last != time(nullptr)) {
     tt = broadcastAccFunction<ThreadTimes>(pleaseGetThreadCPUMsec);
     last = time(nullptr);
index 379f19983ab19652606c0935f7cc068083bfd7ae..81c869b3176b524a60d3bb334badd16077c055af 100644 (file)
@@ -797,7 +797,7 @@ int PacketHandler::processUpdate(DNSPacket& packet) { // NOLINT(readability-func
   }
 
 
-  std::lock_guard<std::mutex> l(s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything
+  auto lock = std::scoped_lock(s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything
   g_log<<Logger::Info<<msgPrefix<<"starting transaction."<<endl;
   if (!di.backend->startTransaction(packet.qdomainzone, UnknownDomainID)) { // Not giving the domain_id means that we do not delete the existing records.
     g_log<<Logger::Error<<msgPrefix<<"Backend for domain "<<packet.qdomainzone<<" does not support transaction. Can't do Update packet."<<endl;
index 3ac01f46234faf78fb51276f00e7e07129dde13f..5b7a4a14d930aead48d4338cbc4735b1ddfdbbfe 100644 (file)
@@ -146,7 +146,7 @@ struct GetUniqueLockUncontendedTest
   void operator()() const
   {
     for (size_t idx = 0; idx < 1000; idx++) {
-      std::unique_lock<decltype(d_testlock)> lock(d_testlock);
+      auto lock = std::scoped_lock(d_testlock);
       ++d_value;
     }
   }
@@ -165,7 +165,7 @@ struct GetLockGuardUncontendedTest
   void operator()() const
   {
     for (size_t idx = 0; idx < 1000; idx++) {
-      std::lock_guard<decltype(d_testlock)> lock(d_testlock);
+      auto lock = std::scoped_lock(d_testlock);
       ++d_value;
     }
   }
index f686ac59dfe099284484a431f56f52dbdea6f177..0bda4ebfd06498f642c273fcc0a6e25066920284 100644 (file)
@@ -111,7 +111,7 @@ void UeberBackend::go()
   s_backendQueries = S.getPointer("backend-queries");
 
   {
-    std::unique_lock<std::mutex> lock(d_mut);
+    std::scoped_lock<std::mutex> lock(d_mut);
     d_go = true;
   }
   d_cond.notify_all();