| 1 | /* |
| 2 | * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved. |
| 3 | |
| 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: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 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 |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "tvgArray.h" |
| 24 | #include "tvgInlist.h" |
| 25 | #include "tvgTaskScheduler.h" |
| 26 | |
| 27 | #ifdef THORVG_THREAD_SUPPORT |
| 28 | #include <thread> |
| 29 | #include <atomic> |
| 30 | #endif |
| 31 | |
| 32 | /************************************************************************/ |
| 33 | /* Internal Class Implementation */ |
| 34 | /************************************************************************/ |
| 35 | |
| 36 | namespace tvg { |
| 37 | |
| 38 | struct TaskSchedulerImpl; |
| 39 | static TaskSchedulerImpl* inst = nullptr; |
| 40 | |
| 41 | #ifdef THORVG_THREAD_SUPPORT |
| 42 | |
| 43 | struct TaskQueue { |
| 44 | Inlist<Task> taskDeque; |
| 45 | mutex mtx; |
| 46 | condition_variable ready; |
| 47 | bool done = false; |
| 48 | |
| 49 | bool tryPop(Task** task) |
| 50 | { |
| 51 | unique_lock<mutex> lock{mtx, try_to_lock}; |
| 52 | if (!lock || taskDeque.empty()) return false; |
| 53 | *task = taskDeque.front(); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool tryPush(Task* task) |
| 58 | { |
| 59 | { |
| 60 | unique_lock<mutex> lock{mtx, try_to_lock}; |
| 61 | if (!lock) return false; |
| 62 | taskDeque.back(task); |
| 63 | } |
| 64 | ready.notify_one(); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | void complete() |
| 69 | { |
| 70 | { |
| 71 | lock_guard<mutex> lock{mtx}; |
| 72 | done = true; |
| 73 | } |
| 74 | ready.notify_all(); |
| 75 | } |
| 76 | |
| 77 | bool pop(Task** task) |
| 78 | { |
| 79 | unique_lock<mutex> lock{mtx}; |
| 80 | |
| 81 | while (taskDeque.empty() && !done) { |
| 82 | ready.wait(lock); |
| 83 | } |
| 84 | |
| 85 | if (taskDeque.empty()) return false; |
| 86 | |
| 87 | *task = taskDeque.front(); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | void push(Task* task) |
| 92 | { |
| 93 | { |
| 94 | lock_guard<mutex> lock{mtx}; |
| 95 | taskDeque.back(task); |
| 96 | } |
| 97 | ready.notify_one(); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | struct TaskSchedulerImpl |
| 103 | { |
| 104 | Array<thread*> threads; |
| 105 | Array<TaskQueue*> taskQueues; |
| 106 | atomic<uint32_t> idx{0}; |
| 107 | |
| 108 | TaskSchedulerImpl(uint32_t threadCnt) |
| 109 | { |
| 110 | threads.reserve(threadCnt); |
| 111 | taskQueues.reserve(threadCnt); |
| 112 | |
| 113 | for (uint32_t i = 0; i < threadCnt; ++i) { |
| 114 | taskQueues.push(new TaskQueue); |
| 115 | threads.push(new thread); |
| 116 | } |
| 117 | for (uint32_t i = 0; i < threadCnt; ++i) { |
| 118 | *threads.data[i] = thread([&, i] { run(i); }); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | ~TaskSchedulerImpl() |
| 123 | { |
| 124 | for (auto tq = taskQueues.begin(); tq < taskQueues.end(); ++tq) { |
| 125 | (*tq)->complete(); |
| 126 | } |
| 127 | for (auto thread = threads.begin(); thread < threads.end(); ++thread) { |
| 128 | (*thread)->join(); |
| 129 | delete(*thread); |
| 130 | } |
| 131 | for (auto tq = taskQueues.begin(); tq < taskQueues.end(); ++tq) { |
| 132 | delete(*tq); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void run(unsigned i) |
| 137 | { |
| 138 | Task* task; |
| 139 | |
| 140 | //Thread Loop |
| 141 | while (true) { |
| 142 | auto success = false; |
| 143 | for (uint32_t x = 0; x < threads.count * 2; ++x) { |
| 144 | if (taskQueues[(i + x) % threads.count]->tryPop(&task)) { |
| 145 | success = true; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (!success && !taskQueues[i]->pop(&task)) break; |
| 151 | (*task)(i + 1); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void request(Task* task) |
| 156 | { |
| 157 | //Async |
| 158 | if (threads.count > 0) { |
| 159 | task->prepare(); |
| 160 | auto i = idx++; |
| 161 | for (uint32_t n = 0; n < threads.count; ++n) { |
| 162 | if (taskQueues[(i + n) % threads.count]->tryPush(task)) return; |
| 163 | } |
| 164 | taskQueues[i % threads.count]->push(task); |
| 165 | //Sync |
| 166 | } else { |
| 167 | task->run(0); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | uint32_t threadCnt() |
| 172 | { |
| 173 | return threads.count; |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | #else //THORVG_THREAD_SUPPORT |
| 178 | |
| 179 | struct TaskSchedulerImpl |
| 180 | { |
| 181 | TaskSchedulerImpl(TVG_UNUSED uint32_t threadCnt) {} |
| 182 | void request(Task* task) { task->run(0); } |
| 183 | uint32_t threadCnt() { return 0; } |
| 184 | }; |
| 185 | |
| 186 | #endif //THORVG_THREAD_SUPPORT |
| 187 | |
| 188 | } //namespace |
| 189 | |
| 190 | /************************************************************************/ |
| 191 | /* External Class Implementation */ |
| 192 | /************************************************************************/ |
| 193 | |
| 194 | void TaskScheduler::init(uint32_t threads) |
| 195 | { |
| 196 | if (inst) return; |
| 197 | inst = new TaskSchedulerImpl(threads); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | void TaskScheduler::term() |
| 202 | { |
| 203 | delete(inst); |
| 204 | inst = nullptr; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | void TaskScheduler::request(Task* task) |
| 209 | { |
| 210 | if (inst) inst->request(task); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | uint32_t TaskScheduler::threads() |
| 215 | { |
| 216 | if (inst) return inst->threadCnt(); |
| 217 | return 0; |
| 218 | } |