From fa134692b0ae2980323d1a478a182cdd9a98bf68 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Mon, 4 Sep 2023 14:33:51 +0900 Subject: [PATCH] Revert "renderer taskscheduler: code refactoring." This reverts commit 77de62068ccfc2e1480888c1173258c5c71d7750. This fixes the sanitizer thread report (data race) issue. @Issue: https://github.com/thorvg/thorvg/issues/1623 --- src/renderer/tvgTaskScheduler.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/renderer/tvgTaskScheduler.cpp b/src/renderer/tvgTaskScheduler.cpp index e3af26df..9dec68c0 100644 --- a/src/renderer/tvgTaskScheduler.cpp +++ b/src/renderer/tvgTaskScheduler.cpp @@ -102,15 +102,15 @@ struct TaskQueue { struct TaskSchedulerImpl { + uint32_t threadCnt; vector threads; vector taskQueues; atomic idx{0}; thread::id tid; - TaskSchedulerImpl(unsigned threadCnt) : taskQueues(threadCnt) + TaskSchedulerImpl(unsigned threadCnt) : threadCnt(threadCnt), taskQueues(threadCnt) { tid = this_thread::get_id(); - threads.reserve(threadCnt); for (unsigned i = 0; i < threadCnt; ++i) { @@ -131,8 +131,8 @@ struct TaskSchedulerImpl //Thread Loop while (true) { auto success = false; - for (unsigned x = 0; x < threads.size() * 2; ++x) { - if (taskQueues[(i + x) % threads.size()].tryPop(&task)) { + for (unsigned x = 0; x < threadCnt * 2; ++x) { + if (taskQueues[(i + x) % threadCnt].tryPop(&task)) { success = true; break; } @@ -146,18 +146,18 @@ struct TaskSchedulerImpl void request(Task* task) { //Async - if (threads.size() > 0) { + if (threadCnt > 0) { auto tid = this_thread::get_id(); if (tid == this->tid) { task->prepare(); auto i = idx++; - for (unsigned n = 0; n < threads.size(); ++n) { - if (taskQueues[(i + n) % threads.size()].tryPush(task)) return; + for (unsigned n = 0; n < threadCnt; ++n) { + if (taskQueues[(i + n) % threadCnt].tryPush(task)) return; } - taskQueues[i % threads.size()].push(task); + taskQueues[i % threadCnt].push(task); //Not thread-safety now, it's requested from a worker-thread } else { - for (unsigned i = 0; i < threads.size(); ++i) { + for (unsigned i = 0; i < threadCnt; ++i) { if (tid == threads[i].get_id()) { task->prepare(); (*task)(i + 1); @@ -202,6 +202,6 @@ void TaskScheduler::request(Task* task) unsigned TaskScheduler::threads() { - if (inst) return inst->threads.size(); + if (inst) return inst->threadCnt; return 0; }