mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-14 12:04:29 +00:00
renderer taskscheduler: code refactoring.
removed an unnecessary internal variable.
This commit is contained in:
parent
8f9bdebd35
commit
77de62068c
1 changed files with 11 additions and 10 deletions
|
@ -102,16 +102,17 @@ struct TaskQueue {
|
|||
|
||||
struct TaskSchedulerImpl
|
||||
{
|
||||
uint32_t threadCnt;
|
||||
vector<thread> threads;
|
||||
vector<TaskQueue> taskQueues;
|
||||
atomic<uint32_t> 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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue