From 6da7d48d25277a2d828539817751c2004c99da24 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 4 Jan 2018 08:15:20 +0100 Subject: [PATCH] Apply clang-tidy fix 'modernize-loop-convert' --- lib/base/process.cpp | 8 ++--- lib/base/threadpool.cpp | 52 ++++++++++++++++----------------- lib/db_ido/dbconnection.cpp | 8 ++--- tools/mkclass/classcompiler.hpp | 6 ++-- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/lib/base/process.cpp b/lib/base/process.cpp index d8c381c7f..fef953dde 100644 --- a/lib/base/process.cpp +++ b/lib/base/process.cpp @@ -506,7 +506,7 @@ void Process::InitializeSpawnHelper() static void InitializeProcess() { - for (int tid = 0; tid < IOTHREADS; tid++) { + for (auto& l_EventFD : l_EventFDs) { #ifdef _WIN32 l_Events[tid] = CreateEvent(nullptr, TRUE, FALSE, nullptr); #else /* _WIN32 */ @@ -514,14 +514,14 @@ static void InitializeProcess() if (pipe2(l_EventFDs[tid], O_CLOEXEC) < 0) { if (errno == ENOSYS) { # endif /* HAVE_PIPE2 */ - if (pipe(l_EventFDs[tid]) < 0) { + if (pipe(l_EventFD) < 0) { BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("pipe") << boost::errinfo_errno(errno)); } - Utility::SetCloExec(l_EventFDs[tid][0]); - Utility::SetCloExec(l_EventFDs[tid][1]); + Utility::SetCloExec(l_EventFD[0]); + Utility::SetCloExec(l_EventFD[1]); # ifdef HAVE_PIPE2 } else { BOOST_THROW_EXCEPTION(posix_error() diff --git a/lib/base/threadpool.cpp b/lib/base/threadpool.cpp index cca32652d..4872652ec 100644 --- a/lib/base/threadpool.cpp +++ b/lib/base/threadpool.cpp @@ -50,8 +50,8 @@ void ThreadPool::Start() m_Stopped = false; - for (size_t i = 0; i < sizeof(m_Queues) / sizeof(m_Queues[0]); i++) - m_Queues[i].SpawnWorker(m_ThreadGroup); + for (auto& queue : m_Queues) + queue.SpawnWorker(m_ThreadGroup); m_MgmtThread = std::thread(std::bind(&ThreadPool::ManagerThreadProc, this)); } @@ -70,18 +70,18 @@ void ThreadPool::Stop() if (m_MgmtThread.joinable()) m_MgmtThread.join(); - for (size_t i = 0; i < sizeof(m_Queues) / sizeof(m_Queues[0]); i++) { - boost::mutex::scoped_lock lock(m_Queues[i].Mutex); - m_Queues[i].Stopped = true; - m_Queues[i].CV.notify_all(); + for (auto& queue : m_Queues) { + boost::mutex::scoped_lock lock(queue.Mutex); + queue.Stopped = true; + queue.CV.notify_all(); } m_ThreadGroup.join_all(); m_ThreadGroup.~thread_group(); new (&m_ThreadGroup) boost::thread_group(); - for (size_t i = 0; i < sizeof(m_Queues) / sizeof(m_Queues[0]); i++) - m_Queues[i].Stopped = false; + for (auto& queue : m_Queues) + queue.Stopped = false; m_Stopped = true; } @@ -242,24 +242,22 @@ void ThreadPool::ManagerThreadProc() break; } - for (size_t i = 0; i < sizeof(m_Queues) / sizeof(m_Queues[0]); i++) { + for (auto& queue : m_Queues) { size_t pending, alive = 0; double avg_latency; double utilization = 0; - Queue& queue = m_Queues[i]; - - boost::mutex::scoped_lock lock(queue.Mutex); + boost::mutex::scoped_lock lock(queue.Mutex); - for (size_t i = 0; i < sizeof(queue.Threads) / sizeof(queue.Threads[0]); i++) - queue.Threads[i].UpdateUtilization(); + for (auto& thread : queue.Threads) + thread.UpdateUtilization(); pending = queue.Items.size(); - for (size_t i = 0; i < sizeof(queue.Threads) / sizeof(queue.Threads[0]); i++) { - if (queue.Threads[i].State != ThreadDead && !queue.Threads[i].Zombie) { + for (auto& thread : queue.Threads) { + if (thread.State != ThreadDead && !thread.Zombie) { alive++; - utilization += queue.Threads[i].Utilization * 100; + utilization += thread.Utilization * 100; } } @@ -331,12 +329,12 @@ void ThreadPool::ManagerThreadProc() */ void ThreadPool::Queue::SpawnWorker(boost::thread_group& group) { - for (size_t i = 0; i < sizeof(Threads) / sizeof(Threads[0]); i++) { - if (Threads[i].State == ThreadDead) { + for (auto& thread : Threads) { + if (thread.State == ThreadDead) { Log(LogDebug, "ThreadPool", "Spawning worker thread."); - Threads[i] = WorkerThread(ThreadIdle); - Threads[i].Thread = group.create_thread(std::bind(&ThreadPool::WorkerThread::ThreadProc, std::ref(Threads[i]), std::ref(*this))); + thread = WorkerThread(ThreadIdle); + thread.Thread = group.create_thread(std::bind(&ThreadPool::WorkerThread::ThreadProc, std::ref(thread), std::ref(*this))); break; } @@ -348,15 +346,15 @@ void ThreadPool::Queue::SpawnWorker(boost::thread_group& group) */ void ThreadPool::Queue::KillWorker(boost::thread_group& group) { - for (size_t i = 0; i < sizeof(Threads) / sizeof(Threads[0]); i++) { - if (Threads[i].State == ThreadIdle && !Threads[i].Zombie) { + for (auto& thread : Threads) { + if (thread.State == ThreadIdle && !thread.Zombie) { Log(LogDebug, "ThreadPool", "Killing worker thread."); - group.remove_thread(Threads[i].Thread); - Threads[i].Thread->detach(); - delete Threads[i].Thread; + group.remove_thread(thread.Thread); + thread.Thread->detach(); + delete thread.Thread; - Threads[i].Zombie = true; + thread.Zombie = true; CV.notify_all(); break; diff --git a/lib/db_ido/dbconnection.cpp b/lib/db_ido/dbconnection.cpp index fd1581f16..ddbf76ff3 100644 --- a/lib/db_ido/dbconnection.cpp +++ b/lib/db_ido/dbconnection.cpp @@ -229,15 +229,15 @@ void DbConnection::CleanUpHandler() { "systemcommands", "start_time" } }; - for (size_t i = 0; i < sizeof(tables) / sizeof(tables[0]); i++) { - double max_age = GetCleanup()->Get(tables[i].name + "_age"); + for (auto& table : tables) { + double max_age = GetCleanup()->Get(table.name + "_age"); if (max_age == 0) continue; - CleanUpExecuteQuery(tables[i].name, tables[i].time_column, now - max_age); + CleanUpExecuteQuery(table.name, table.time_column, now - max_age); Log(LogNotice, "DbConnection") - << "Cleanup (" << tables[i].name << "): " << max_age + << "Cleanup (" << table.name << "): " << max_age << " now: " << now << " old: " << now - max_age; } diff --git a/tools/mkclass/classcompiler.hpp b/tools/mkclass/classcompiler.hpp index f4b8f0ba9..67a50070d 100644 --- a/tools/mkclass/classcompiler.hpp +++ b/tools/mkclass/classcompiler.hpp @@ -138,14 +138,14 @@ struct Field bool cap = true; std::string name = Name; - for (size_t i = 0; i < name.size(); i++) { - if (name[i] == '_') { + for (char& ch : name) { + if (ch == '_') { cap = true; continue; } if (cap) { - name[i] = toupper(name[i]); + ch = toupper(ch); cap = false; } } -- 2.40.0