From: Rosen Penev Date: Fri, 4 Jul 2025 00:53:55 +0000 (-0700) Subject: clang-tidy: replace lock_guard with scoped_lock X-Git-Tag: rec-5.3.0-alpha2~5^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F15788%2Fhead;p=thirdparty%2Fpdns.git clang-tidy: replace lock_guard with scoped_lock Found with modernize-use-scoped-lock Signed-off-by: Rosen Penev --- diff --git a/modules/bindbackend/bindbackend2.cc b/modules/bindbackend/bindbackend2.cc index 6f211491cd..c53db4b37b 100644 --- a/modules/bindbackend/bindbackend2.cc +++ b/modules/bindbackend/bindbackend2.cc @@ -826,7 +826,7 @@ Bind2Backend::Bind2Backend(const string& suffix, bool loadZones) if (!loadZones && d_hybrid) return; - std::lock_guard 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 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) { diff --git a/modules/gmysqlbackend/smysql.cc b/modules/gmysqlbackend/smysql.cc index ae5add9a4e..668b124807 100644 --- a/modules/gmysqlbackend/smysql.cc +++ b/modules/gmysqlbackend/smysql.cc @@ -487,7 +487,7 @@ void SMySQL::connect() int retry = 1; { - std::lock_guard l(s_myinitlock); + auto lock = std::scoped_lock(s_myinitlock); if (d_threadCleanup) { threadcloser.enable(); } diff --git a/modules/lmdbbackend/lmdbbackend.cc b/modules/lmdbbackend/lmdbbackend.cc index 300b6bf2c1..c933f9d776 100644 --- a/modules/lmdbbackend/lmdbbackend.cc +++ b/modules/lmdbbackend/lmdbbackend.cc @@ -716,7 +716,7 @@ LMDBBackend::LMDBBackend(const std::string& suffix) bool opened = false; if (s_first) { - std::lock_guard l(s_lmdbStartupLock); + auto lock = std::scoped_lock(s_lmdbStartupLock); if (s_first) { auto filename = getArg("filename"); diff --git a/pdns/auth-main.cc b/pdns/auth-main.cc index 7b967f5a30..017285ec8f 100644 --- a/pdns/auth-main.cc +++ b/pdns/auth-main.cc @@ -1023,7 +1023,7 @@ static string DLRestHandler(const vector& parts, pid_t /* ppid */) } line.append(1, '\n'); - std::lock_guard l(g_guardian_lock); + auto lock = std::scoped_lock(g_guardian_lock); try { writen2(g_fd1[1], line.c_str(), line.size() + 1); diff --git a/pdns/dnsdistdist/dnsdist-backend.cc b/pdns/dnsdistdist/dnsdist-backend.cc index ab32818a20..c93b9e561b 100644 --- a/pdns/dnsdistdist/dnsdist-backend.cc +++ b/pdns/dnsdistdist/dnsdist-backend.cc @@ -236,7 +236,7 @@ void DownstreamState::stop() d_stopped = true; { - std::lock_guard tl(connectLock); + auto tlock = std::scoped_lock(connectLock); auto slock = mplexer.lock(); for (auto& fd : sockets) { diff --git a/pdns/dnsdistdist/dnsdist-nghttp2.cc b/pdns/dnsdistdist/dnsdist-nghttp2.cc index 338858f913..9b9e6f9b70 100644 --- a/pdns/dnsdistdist/dnsdist-nghttp2.cc +++ b/pdns/dnsdistdist/dnsdist-nghttp2.cc @@ -888,7 +888,7 @@ static void dohClientThread(pdns::channel::Receiver&& receiv if (g_dohStatesDumpRequested > 0) { /* just to keep things clean in the output, debug only */ static std::mutex s_lock; - std::lock_guard 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(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, internalPipeBufferSize); vinfolog("Adding DoH Client thread"); - std::lock_guard 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()); diff --git a/pdns/dnsdistdist/dnsdist-tcp.cc b/pdns/dnsdistdist/dnsdist-tcp.cc index 7e12e67c23..4f78f49d89 100644 --- a/pdns/dnsdistdist/dnsdist-tcp.cc +++ b/pdns/dnsdistdist/dnsdist-tcp.cc @@ -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 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; diff --git a/pdns/dnsdistdist/dolog.cc b/pdns/dnsdistdist/dolog.cc index fabfdbebcc..74059197db 100644 --- a/pdns/dnsdistdist/dolog.cc +++ b/pdns/dnsdistdist/dolog.cc @@ -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'; diff --git a/pdns/ixfrdist.cc b/pdns/ixfrdist.cc index d50c5597db..e167554f21 100644 --- a/pdns/ixfrdist.cc +++ b/pdns/ixfrdist.cc @@ -1129,7 +1129,7 @@ static void handleTCPRequest(int fd, boost::any&) { } { - std::lock_guard lg(g_tcpRequestFDsMutex); + auto lock = std::scoped_lock(g_tcpRequestFDsMutex); g_tcpRequestFDs.push({cfd, saddr}); } g_tcpHandlerCV.notify_one(); diff --git a/pdns/lock.hh b/pdns/lock.hh index 254c2f87b9..13e28d5aaf 100644 --- a/pdns/lock.hh +++ b/pdns/lock.hh @@ -248,7 +248,7 @@ public: } private: - std::lock_guard d_lock; + std::scoped_lock d_lock; T& d_value; }; @@ -353,7 +353,7 @@ public: } private: - std::lock_guard d_lock; + std::scoped_lock d_lock; T& d_value; }; @@ -458,7 +458,7 @@ public: } private: - std::lock_guard d_lock; + std::scoped_lock d_lock; T& d_value; }; diff --git a/pdns/logger.cc b/pdns/logger.cc index 77a4ce5fd4..483a4d0089 100644 --- a/pdns/logger.cc +++ b/pdns/logger.cc @@ -104,7 +104,7 @@ void Logger::log(const string& msg, Urgency u) noexcept } static std::mutex mutex; - std::lock_guard 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. diff --git a/pdns/malloctrace.cc b/pdns/malloctrace.cc index 4fe49c544c..a6f4546c42 100644 --- a/pdns/malloctrace.cc +++ b/pdns/malloctrace.cc @@ -73,7 +73,7 @@ void* MallocTracer::malloc(size_t size) d_totAllocated += size; - std::lock_guard 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 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 lock(d_mut); + auto lock = std::scoped_lock(d_mut); d_stats.clear(); l_active=false; } diff --git a/pdns/malloctrace.hh b/pdns/malloctrace.hh index 22b531afaa..73e3775abc 100644 --- a/pdns/malloctrace.hh +++ b/pdns/malloctrace.hh @@ -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 lock(d_mut); return d_sizes.size(); } + uint64_t getNumOut() { std::scoped_lock lock(d_mut); return d_sizes.size(); } struct AllocStats { int count; diff --git a/pdns/recursordist/logging.cc b/pdns/recursordist/logging.cc index 544350406a..af188f45ef 100644 --- a/pdns/recursordist/logging.cc +++ b/pdns/recursordist/logging.cc @@ -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)); diff --git a/pdns/recursordist/nod.cc b/pdns/recursordist/nod.cc index 9e4c1dfa73..6d0f01c44a 100644 --- a/pdns/recursordist/nod.cc +++ b/pdns/recursordist/nod.cc @@ -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& /* lock */) +void PersistentSBF::remove_tmp_files(const filesystem::path& path, std::scoped_lock& /* 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 lock(d_cachedir_mutex); + std::scoped_lock lock(d_cachedir_mutex); if (d_cachedir.length() != 0) { filesystem::path path(d_cachedir); try { diff --git a/pdns/recursordist/nod.hh b/pdns/recursordist/nod.hh index 1cc8f456dc..fbefbb9aca 100644 --- a/pdns/recursordist/nod.hh +++ b/pdns/recursordist/nod.hh @@ -64,7 +64,7 @@ public: } private: - void remove_tmp_files(const boost::filesystem::path&, std::lock_guard&); + void remove_tmp_files(const boost::filesystem::path&, std::scoped_lock&); LockGuarded d_sbf; // Stable Bloom Filter std::string d_cachedir; diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index 009940d50b..73f111a17e 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -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& 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(); } diff --git a/pdns/recursordist/rec_channel_rec.cc b/pdns/recursordist/rec_channel_rec.cc index e29803f8d0..6d4f44b11f 100644 --- a/pdns/recursordist/rec_channel_rec.cc +++ b/pdns/recursordist/rec_channel_rec.cc @@ -969,7 +969,7 @@ static uint64_t doGetThreadCPUMsec(int n) static time_t last = 0; static ThreadTimes tt; - std::lock_guard l(s_mut); + auto lock = std::scoped_lock(s_mut); if (last != time(nullptr)) { tt = broadcastAccFunction(pleaseGetThreadCPUMsec); last = time(nullptr); diff --git a/pdns/rfc2136handler.cc b/pdns/rfc2136handler.cc index 379f19983a..81c869b317 100644 --- a/pdns/rfc2136handler.cc +++ b/pdns/rfc2136handler.cc @@ -797,7 +797,7 @@ int PacketHandler::processUpdate(DNSPacket& packet) { // NOLINT(readability-func } - std::lock_guard 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<startTransaction(packet.qdomainzone, UnknownDomainID)) { // Not giving the domain_id means that we do not delete the existing records. g_log< 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 lock(d_testlock); + auto lock = std::scoped_lock(d_testlock); ++d_value; } } diff --git a/pdns/ueberbackend.cc b/pdns/ueberbackend.cc index f686ac59df..0bda4ebfd0 100644 --- a/pdns/ueberbackend.cc +++ b/pdns/ueberbackend.cc @@ -111,7 +111,7 @@ void UeberBackend::go() s_backendQueries = S.getPointer("backend-queries"); { - std::unique_lock lock(d_mut); + std::scoped_lock lock(d_mut); d_go = true; } d_cond.notify_all();