From f11b77cb459ee14b9c59dab567ab4f80da68752e Mon Sep 17 00:00:00 2001 From: "joogab.yun" Date: Thu, 13 Jul 2023 15:46:18 +0900 Subject: [PATCH] common taskscheduler: There is a report of the thread sanitizer. It could be a false-positive as far as I reviewed, but we hate to be bothered by it. So let's revert it. @Issue: https://github.com/thorvg/thorvg/issues/1409 Change-Id: I74d0968a8a9682def132569770e588f7c2ac64bf --- src/lib/tvgTaskScheduler.h | 66 ++++++++++------------------------------------ 1 file changed, 14 insertions(+), 52 deletions(-) diff --git a/src/lib/tvgTaskScheduler.h b/src/lib/tvgTaskScheduler.h index 72c17fb..ce2016f 100644 --- a/src/lib/tvgTaskScheduler.h +++ b/src/lib/tvgTaskScheduler.h @@ -43,77 +43,40 @@ struct TaskScheduler struct Task { private: - mutex finishedMtx; - mutex preparedMtx; + mutex mtx; condition_variable cv; - bool finished = true; //if run() finished - bool prepared = false; //the task is requested + bool ready = true; + bool pending = false; public: - virtual ~Task() - { - if (!prepared) return; - - //Guarantee the task is finished by TaskScheduler. - unique_lock lock(preparedMtx); + virtual ~Task() = default; - while (prepared) { - cv.wait(lock); - } - } - - void done(unsigned tid = 0) + void done() { - if (finished) return; - - lock_guard lock(finishedMtx); + if (!pending) return; - if (finished) return; - - //the job hasn't been launched yet. - - //set finished so that operator() quickly returns. - finished = true; - - run(tid); + unique_lock lock(mtx); + while (!ready) cv.wait(lock); + pending = false; } protected: virtual void run(unsigned tid) = 0; private: - void finish() - { - lock_guard lock(preparedMtx); - prepared = false; - cv.notify_one(); - } - void operator()(unsigned tid) { - if (finished) { - finish(); - return; - } - - lock_guard lock(finishedMtx); - - if (finished) { - finish(); - return; - } - run(tid); - finished = true; - - finish(); + lock_guard lock(mtx); + ready = true; + cv.notify_one(); } void prepare() { - finished = false; - prepared = true; + ready = false; + pending = true; } friend struct TaskSchedulerImpl; @@ -122,4 +85,3 @@ private: } #endif //_TVG_TASK_SCHEDULER_H_ - -- 2.7.4