0.2.0-dev Download ZIP
122 lines ·2.7 KB Raw
| 1 | /* |
| 2 | * Copyright (c) 2020 - 2026 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 | #ifndef _TVG_TASK_SCHEDULER_H_ |
| 24 | #define _TVG_TASK_SCHEDULER_H_ |
| 25 | |
| 26 | #include "tvgCommon.h" |
| 27 | #include "tvgInlist.h" |
| 28 | |
| 29 | #ifdef THORVG_THREAD_SUPPORT |
| 30 | #include <atomic> |
| 31 | #include <thread> |
| 32 | #include <mutex> |
| 33 | #include <condition_variable> |
| 34 | #endif |
| 35 | |
| 36 | namespace tvg { |
| 37 | |
| 38 | |
| 39 | #ifdef THORVG_THREAD_SUPPORT |
| 40 | |
| 41 | using ThreadID = std::thread::id; |
| 42 | |
| 43 | struct Task |
| 44 | { |
| 45 | private: |
| 46 | mutex mtx; |
| 47 | condition_variable cv; |
| 48 | bool ready = true; |
| 49 | bool pending = false; |
| 50 | |
| 51 | public: |
| 52 | INLIST_ITEM(Task); |
| 53 | |
| 54 | virtual ~Task() = default; |
| 55 | |
| 56 | void done() |
| 57 | { |
| 58 | if (!pending) return; |
| 59 | |
| 60 | unique_lock<mutex> lock(mtx); |
| 61 | while (!ready) cv.wait(lock); |
| 62 | pending = false; |
| 63 | } |
| 64 | |
| 65 | protected: |
| 66 | virtual void run(unsigned tid) = 0; |
| 67 | |
| 68 | private: |
| 69 | void operator()(unsigned tid) |
| 70 | { |
| 71 | run(tid); |
| 72 | |
| 73 | lock_guard<mutex> lock(mtx); |
| 74 | ready = true; |
| 75 | cv.notify_one(); |
| 76 | } |
| 77 | |
| 78 | void prepare() |
| 79 | { |
| 80 | ready = false; |
| 81 | pending = true; |
| 82 | } |
| 83 | |
| 84 | friend struct TaskSchedulerImpl; |
| 85 | }; |
| 86 | |
| 87 | #else //THORVG_THREAD_SUPPORT |
| 88 | |
| 89 | using ThreadID = uint8_t; |
| 90 | |
| 91 | struct Task |
| 92 | { |
| 93 | public: |
| 94 | INLIST_ITEM(Task); |
| 95 | |
| 96 | virtual ~Task() = default; |
| 97 | void done() {} |
| 98 | |
| 99 | protected: |
| 100 | virtual void run(unsigned tid) = 0; |
| 101 | |
| 102 | private: |
| 103 | friend struct TaskSchedulerImpl; |
| 104 | }; |
| 105 | |
| 106 | #endif //THORVG_THREAD_SUPPORT |
| 107 | |
| 108 | |
| 109 | struct TaskScheduler |
| 110 | { |
| 111 | static uint32_t threads(); |
| 112 | static void init(uint32_t threads); |
| 113 | static void term(); |
| 114 | static void request(Task* task); |
| 115 | static bool onthread(); //figure out whether on worker thread or not |
| 116 | static ThreadID tid(); |
| 117 | }; |
| 118 | |
| 119 | } //namespace |
| 120 | |
| 121 | #endif //_TVG_TASK_SCHEDULER_H_ |
| 122 |