renderer taskscheduler: code refactoring.

removed an unnecessary internal variable.
This commit is contained in:
Hermet Park 2023-08-21 16:19:46 +09:00 committed by Hermet Park
parent 8f9bdebd35
commit 77de62068c

View file

@ -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;
}