2 * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <condition_variable>
27 #include "tvgTaskScheduler.h"
29 /************************************************************************/
30 /* Internal Class Implementation */
31 /************************************************************************/
36 deque<Task*> taskDeque;
38 condition_variable ready;
41 bool tryPop(Task** task)
43 unique_lock<mutex> lock{mtx, try_to_lock};
44 if (!lock || taskDeque.empty()) return false;
45 *task = taskDeque.front();
46 taskDeque.pop_front();
51 bool tryPush(Task* task)
54 unique_lock<mutex> lock{mtx, try_to_lock};
55 if (!lock) return false;
56 taskDeque.push_back(task);
67 unique_lock<mutex> lock{mtx};
75 unique_lock<mutex> lock{mtx};
77 while (taskDeque.empty() && !done) {
81 if (taskDeque.empty()) return false;
83 *task = taskDeque.front();
84 taskDeque.pop_front();
92 unique_lock<mutex> lock{mtx};
93 taskDeque.push_back(task);
102 class TaskSchedulerImpl
106 vector<thread> threads;
107 vector<TaskQueue> taskQueues;
108 atomic<unsigned> idx{0};
110 TaskSchedulerImpl(unsigned threadCnt) : threadCnt(threadCnt), taskQueues(threadCnt)
112 for (unsigned i = 0; i < threadCnt; ++i) {
113 threads.emplace_back([&, i] { run(i); });
119 for (auto& queue : taskQueues) queue.complete();
120 for (auto& thread : threads) thread.join();
129 auto success = false;
130 for (unsigned x = 0; x < threadCnt * 2; ++x) {
131 if (taskQueues[(i + x) % threadCnt].tryPop(&task)) {
137 if (!success && !taskQueues[i].pop(&task)) break;
142 void request(Task* task)
148 for (unsigned n = 0; n < threadCnt; ++n) {
149 if (taskQueues[(i + n) % threadCnt].tryPush(task)) return;
151 taskQueues[i % threadCnt].push(task);
161 static TaskSchedulerImpl* inst = nullptr;
163 /************************************************************************/
164 /* External Class Implementation */
165 /************************************************************************/
167 void TaskScheduler::init(unsigned threads)
170 inst = new TaskSchedulerImpl(threads);
174 void TaskScheduler::term()
182 void TaskScheduler::request(Task* task)
184 if (inst) inst->request(task);
188 unsigned TaskScheduler::threads()
190 if (inst) return inst->threadCnt;