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:
Subhransu Mohanty 2020-11-10 16:43:19 +09:00 committed by Hermet Park
parent d1d54e8b8f
commit c2e1583e94

View file

@ -23,6 +23,7 @@
#define _TVG_TASK_SCHEDULER_H_
#include <mutex>
#include <condition_variable>
#include "tvgCommon.h"
namespace tvg
@ -42,20 +43,23 @@ struct Task
{
private:
mutex mtx;
bool working = false;
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;
}
}