From 77de62068ccfc2e1480888c1173258c5c71d7750 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 21 Aug 2023 16:19:46 +0900 Subject: [PATCH] renderer taskscheduler: code refactoring. removed an unnecessary internal variable. --- src/lib/tvgTaskScheduler.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/lib/tvgTaskScheduler.cpp b/src/lib/tvgTaskScheduler.cpp index 7c9b4ea1..e3af26df 100644 --- a/src/lib/tvgTaskScheduler.cpp +++ b/src/lib/tvgTaskScheduler.cpp @@ -102,16 +102,17 @@ struct TaskQueue { struct TaskSchedulerImpl { - uint32_t threadCnt; vector threads; vector taskQueues; atomic idx{0}; thread::id tid; - TaskSchedulerImpl(unsigned threadCnt) : threadCnt(threadCnt), taskQueues(threadCnt) + TaskSchedulerImpl(unsigned threadCnt) : taskQueues(threadCnt) { tid = this_thread::get_id(); + threads.reserve(threadCnt); + for (unsigned i = 0; i < threadCnt; ++i) { threads.emplace_back([&, i] { run(i); }); } @@ -130,8 +131,8 @@ struct TaskSchedulerImpl //Thread Loop while (true) { auto success = false; - for (unsigned x = 0; x < threadCnt * 2; ++x) { - if (taskQueues[(i + x) % threadCnt].tryPop(&task)) { + for (unsigned x = 0; x < threads.size() * 2; ++x) { + if (taskQueues[(i + x) % threads.size()].tryPop(&task)) { success = true; break; } @@ -145,18 +146,18 @@ struct TaskSchedulerImpl void request(Task* task) { //Async - if (threadCnt > 0) { + if (threads.size() > 0) { auto tid = this_thread::get_id(); if (tid == this->tid) { task->prepare(); auto i = idx++; - for (unsigned n = 0; n < threadCnt; ++n) { - if (taskQueues[(i + n) % threadCnt].tryPush(task)) return; + for (unsigned n = 0; n < threads.size(); ++n) { + if (taskQueues[(i + n) % threads.size()].tryPush(task)) return; } - taskQueues[i % threadCnt].push(task); + taskQueues[i % threads.size()].push(task); //Not thread-safety now, it's requested from a worker-thread } else { - for (unsigned i = 0; i < threadCnt; ++i) { + for (unsigned i = 0; i < threads.size(); ++i) { if (tid == threads[i].get_id()) { task->prepare(); (*task)(i + 1); @@ -201,6 +202,6 @@ void TaskScheduler::request(Task* task) unsigned TaskScheduler::threads() { - if (inst) return inst->threadCnt; + if (inst) return inst->threads.size(); return 0; }