mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-14 12:04:29 +00:00
common taskScheduler: fix a synchronization data race.
std mutex shouldn't be used among threads. behavior is unexpected by implmentation. This resolves it by introducing conditional value.
This commit is contained in:
parent
d1d54e8b8f
commit
c2e1583e94
1 changed files with 19 additions and 9 deletions
|
@ -23,6 +23,7 @@
|
|||
#define _TVG_TASK_SCHEDULER_H_
|
||||
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include "tvgCommon.h"
|
||||
|
||||
namespace tvg
|
||||
|
@ -41,21 +42,24 @@ struct TaskScheduler
|
|||
struct Task
|
||||
{
|
||||
private:
|
||||
mutex mtx;
|
||||
bool working = false;
|
||||
mutex mtx;
|
||||
condition_variable cv;
|
||||
bool ready{true};
|
||||
bool pending{false};
|
||||
|
||||
public:
|
||||
virtual ~Task() = default;
|
||||
|
||||
void done()
|
||||
{
|
||||
if (!working) return;
|
||||
if (!pending) return;
|
||||
|
||||
if (TaskScheduler::threads() > 0) {
|
||||
mtx.lock();
|
||||
working = false;
|
||||
mtx.unlock();
|
||||
unique_lock<mutex> lock(mtx);
|
||||
while (!ready) cv.wait(lock);
|
||||
}
|
||||
|
||||
pending = false;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
@ -66,14 +70,20 @@ private:
|
|||
{
|
||||
run(tid);
|
||||
|
||||
if (TaskScheduler::threads() > 0) mtx.unlock();
|
||||
if (TaskScheduler::threads() > 0) {
|
||||
{
|
||||
lock_guard<mutex> lock(mtx);
|
||||
ready = true;
|
||||
}
|
||||
cv.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
void prepare()
|
||||
{
|
||||
if (TaskScheduler::threads() > 0) {
|
||||
working = true;
|
||||
mtx.lock();
|
||||
ready = false;
|
||||
pending = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue