mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-15 12:34:30 +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
|
struct TaskSchedulerImpl
|
||||||
{
|
{
|
||||||
uint32_t threadCnt;
|
|
||||||
vector<thread> threads;
|
vector<thread> threads;
|
||||||
vector<TaskQueue> taskQueues;
|
vector<TaskQueue> taskQueues;
|
||||||
atomic<uint32_t> idx{0};
|
atomic<uint32_t> idx{0};
|
||||||
thread::id tid;
|
thread::id tid;
|
||||||
|
|
||||||
TaskSchedulerImpl(unsigned threadCnt) : threadCnt(threadCnt), taskQueues(threadCnt)
|
TaskSchedulerImpl(unsigned threadCnt) : taskQueues(threadCnt)
|
||||||
{
|
{
|
||||||
tid = this_thread::get_id();
|
tid = this_thread::get_id();
|
||||||
|
|
||||||
|
threads.reserve(threadCnt);
|
||||||
|
|
||||||
for (unsigned i = 0; i < threadCnt; ++i) {
|
for (unsigned i = 0; i < threadCnt; ++i) {
|
||||||
threads.emplace_back([&, i] { run(i); });
|
threads.emplace_back([&, i] { run(i); });
|
||||||
}
|
}
|
||||||
|
@ -130,8 +131,8 @@ struct TaskSchedulerImpl
|
||||||
//Thread Loop
|
//Thread Loop
|
||||||
while (true) {
|
while (true) {
|
||||||
auto success = false;
|
auto success = false;
|
||||||
for (unsigned x = 0; x < threadCnt * 2; ++x) {
|
for (unsigned x = 0; x < threads.size() * 2; ++x) {
|
||||||
if (taskQueues[(i + x) % threadCnt].tryPop(&task)) {
|
if (taskQueues[(i + x) % threads.size()].tryPop(&task)) {
|
||||||
success = true;
|
success = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -145,18 +146,18 @@ struct TaskSchedulerImpl
|
||||||
void request(Task* task)
|
void request(Task* task)
|
||||||
{
|
{
|
||||||
//Async
|
//Async
|
||||||
if (threadCnt > 0) {
|
if (threads.size() > 0) {
|
||||||
auto tid = this_thread::get_id();
|
auto tid = this_thread::get_id();
|
||||||
if (tid == this->tid) {
|
if (tid == this->tid) {
|
||||||
task->prepare();
|
task->prepare();
|
||||||
auto i = idx++;
|
auto i = idx++;
|
||||||
for (unsigned n = 0; n < threadCnt; ++n) {
|
for (unsigned n = 0; n < threads.size(); ++n) {
|
||||||
if (taskQueues[(i + n) % threadCnt].tryPush(task)) return;
|
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
|
//Not thread-safety now, it's requested from a worker-thread
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < threadCnt; ++i) {
|
for (unsigned i = 0; i < threads.size(); ++i) {
|
||||||
if (tid == threads[i].get_id()) {
|
if (tid == threads[i].get_id()) {
|
||||||
task->prepare();
|
task->prepare();
|
||||||
(*task)(i + 1);
|
(*task)(i + 1);
|
||||||
|
@ -201,6 +202,6 @@ void TaskScheduler::request(Task* task)
|
||||||
|
|
||||||
unsigned TaskScheduler::threads()
|
unsigned TaskScheduler::threads()
|
||||||
{
|
{
|
||||||
if (inst) return inst->threadCnt;
|
if (inst) return inst->threads.size();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue