| 1 |
/* |
| 2 |
* Copyright 2017 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkExecutor.h" |
| 9 |
#include "include/private/base/SkMutex.h" |
| 10 |
#include "include/private/base/SkSemaphore.h" |
| 11 |
#include "include/private/base/SkTArray.h" |
| 12 |
#include "include/private/base/SkTPin.h" |
| 13 |
#include "src/base/SkNoDestructor.h" |
| 14 |
|
| 15 |
#include <deque> |
| 16 |
#include <thread> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
using namespace skia_private; |
| 20 |
|
| 21 |
#if defined(SK_BUILD_FOR_WIN) |
| 22 |
#include "src/base/SkLeanWindows.h" |
| 23 |
static int num_cores() { |
| 24 |
SYSTEM_INFO sysinfo; |
| 25 |
GetNativeSystemInfo(&sysinfo); |
| 26 |
return (int)sysinfo.dwNumberOfProcessors; |
| 27 |
} |
| 28 |
#else |
| 29 |
#include <unistd.h> |
| 30 |
static int num_cores() { |
| 31 |
return (int)sysconf(_SC_NPROCESSORS_ONLN); |
| 32 |
} |
| 33 |
#endif |
| 34 |
|
| 35 |
SkExecutor::~SkExecutor() {} |
| 36 |
|
| 37 |
// The default default SkExecutor is an SkTrivialExecutor, which just runs the work right away. |
| 38 |
class SkTrivialExecutor final : public SkExecutor { |
| 39 |
public: |
| 40 |
void add(std::function<void(void)> work, int /* workList */) override { |
| 41 |
work(); |
| 42 |
} |
| 43 |
void add(std::function<void(void)> work) override { |
| 44 |
this->add(std::move(work), /* workList= */ 0); |
| 45 |
} |
| 46 |
int discardAllPendingWork() override { return 0;} |
| 47 |
}; |
| 48 |
|
| 49 |
static SkExecutor& trivial_executor() { |
| 50 |
static SkNoDestructor<SkTrivialExecutor> executor; |
| 51 |
return *executor; |
| 52 |
} |
| 53 |
|
| 54 |
static SkExecutor* gDefaultExecutor = nullptr; |
| 55 |
|
| 56 |
SkExecutor& SkExecutor::GetDefault() { |
| 57 |
if (gDefaultExecutor) { |
| 58 |
return *gDefaultExecutor; |
| 59 |
} |
| 60 |
return trivial_executor(); |
| 61 |
} |
| 62 |
|
| 63 |
void SkExecutor::SetDefault(SkExecutor* executor) { |
| 64 |
gDefaultExecutor = executor; |
| 65 |
} |
| 66 |
|
| 67 |
// We'll always push_back() new work, but pop from the front of deques or the back of SkTArray. |
| 68 |
static inline std::function<void(void)> pop(std::deque<std::function<void(void)>>* list) { |
| 69 |
std::function<void(void)> fn = std::move(list->front()); |
| 70 |
list->pop_front(); |
| 71 |
return fn; |
| 72 |
} |
| 73 |
static inline std::function<void(void)> pop(TArray<std::function<void(void)>>* list) { |
| 74 |
std::function<void(void)> fn = std::move(list->back()); |
| 75 |
list->pop_back(); |
| 76 |
return fn; |
| 77 |
} |
| 78 |
|
| 79 |
// An SkThreadPool is an executor that runs work on a fixed pool of OS threads. |
| 80 |
template <typename WorkList> |
| 81 |
class SkThreadPool final : public SkExecutor { |
| 82 |
public: |
| 83 |
explicit SkThreadPool(int numWorkLists, int threads, bool allowBorrowing) |
| 84 |
: fNumWorkLists(numWorkLists < 1 ? 1 : numWorkLists) |
| 85 |
, fAllowBorrowing(allowBorrowing) { |
| 86 |
|
| 87 |
fWorkLists = std::make_unique<WorkList[]>(fNumWorkLists); |
| 88 |
|
| 89 |
for (int i = 0; i < threads; i++) { |
| 90 |
fThreads.emplace_back(&Loop, this); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
~SkThreadPool() override { |
| 95 |
// Signal each thread that it's time to shut down. |
| 96 |
for (int i = 0; i < fThreads.size(); i++) { |
| 97 |
// Add the notification to the highest priority list |
| 98 |
this->add(nullptr, /* workList= */ 0); |
| 99 |
} |
| 100 |
// Wait for each thread to shut down. |
| 101 |
for (int i = 0; i < fThreads.size(); i++) { |
| 102 |
fThreads[i].join(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void add(std::function<void(void)> work, int workList) override { |
| 107 |
workList = SkTPin(workList, 0, fNumWorkLists-1); |
| 108 |
|
| 109 |
// Add some work to our pile of work to do. |
| 110 |
{ |
| 111 |
SkAutoMutexExclusive lock(fWorkLock); |
| 112 |
|
| 113 |
fWorkLists[workList].emplace_back(std::move(work)); |
| 114 |
} |
| 115 |
// Tell the Loop() threads to pick it up. |
| 116 |
fWorkAvailable.signal(1); |
| 117 |
} |
| 118 |
|
| 119 |
void add(std::function<void(void)> work) override { |
| 120 |
this->add(std::move(work), /* workList= */ 0); |
| 121 |
} |
| 122 |
|
| 123 |
int discardAllPendingWork() override { |
| 124 |
SkAutoMutexExclusive lock(fWorkLock); |
| 125 |
|
| 126 |
int numDiscarded = 0; |
| 127 |
for (int i = 0; i < fNumWorkLists; ++i) { |
| 128 |
numDiscarded += fWorkLists[i].size(); |
| 129 |
fWorkLists[i].clear(); |
| 130 |
} |
| 131 |
|
| 132 |
return numDiscarded; |
| 133 |
} |
| 134 |
|
| 135 |
void borrow() override { |
| 136 |
// If there is work waiting and we're allowed to borrow work, do it. |
| 137 |
if (fAllowBorrowing && fWorkAvailable.try_wait()) { |
| 138 |
SkAssertResult(this->do_work()); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
private: |
| 143 |
// This method should usually be called only when fWorkAvailable indicates there's work to do. |
| 144 |
bool do_work() { |
| 145 |
std::function<void(void)> work; |
| 146 |
bool workAvailable = false; |
| 147 |
{ |
| 148 |
SkAutoMutexExclusive lock(fWorkLock); |
| 149 |
|
| 150 |
for (int i = 0; i < fNumWorkLists; ++i) { |
| 151 |
if (!fWorkLists[i].empty()) { |
| 152 |
workAvailable = true; |
| 153 |
work = pop(&fWorkLists[i]); |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if (!workAvailable) { |
| 160 |
// Because we can discard work asynchronous to Loop() we can sometimes get in this |
| 161 |
// method with no work to do |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
if (!work) { |
| 166 |
return false; // This is Loop()'s signal to shut down. |
| 167 |
} |
| 168 |
|
| 169 |
work(); |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
static void Loop(void* ctx) { |
| 174 |
auto pool = (SkThreadPool*)ctx; |
| 175 |
do { |
| 176 |
pool->fWorkAvailable.wait(); |
| 177 |
} while (pool->do_work()); |
| 178 |
} |
| 179 |
|
| 180 |
// Both SkMutex and SkSpinlock can work here. |
| 181 |
using Lock = SkMutex; |
| 182 |
|
| 183 |
TArray<std::thread> fThreads; |
| 184 |
const int fNumWorkLists; // guaranteed >= 1 |
| 185 |
std::unique_ptr<WorkList[]> fWorkLists SK_GUARDED_BY(fWorkLock); |
| 186 |
Lock fWorkLock; |
| 187 |
SkSemaphore fWorkAvailable; |
| 188 |
const bool fAllowBorrowing; |
| 189 |
}; |
| 190 |
|
| 191 |
std::unique_ptr<SkExecutor> SkExecutor::MakeFIFOThreadPool(int threads, bool allowBorrowing) { |
| 192 |
using WorkList = std::deque<std::function<void(void)>>; |
| 193 |
return std::make_unique<SkThreadPool<WorkList>>(/* numWorkLists= */ 1, |
| 194 |
threads > 0 ? threads : num_cores(), |
| 195 |
allowBorrowing); |
| 196 |
} |
| 197 |
std::unique_ptr<SkExecutor> SkExecutor::MakeLIFOThreadPool(int threads, bool allowBorrowing) { |
| 198 |
using WorkList = TArray<std::function<void(void)>>; |
| 199 |
return std::make_unique<SkThreadPool<WorkList>>(/* numWorkLists= */ 1, |
| 200 |
threads > 0 ? threads : num_cores(), |
| 201 |
allowBorrowing); |
| 202 |
} |
| 203 |
|
| 204 |
std::unique_ptr<SkExecutor> SkExecutor::MakeMultiListFIFOThreadPool(int numWorkLists, |
| 205 |
int threads, |
| 206 |
bool allowBorrowing) { |
| 207 |
using WorkList = std::deque<std::function<void(void)>>; |
| 208 |
return std::make_unique<SkThreadPool<WorkList>>(numWorkLists, |
| 209 |
threads > 0 ? threads : num_cores(), |
| 210 |
allowBorrowing); |
| 211 |
} |
| 212 |
std::unique_ptr<SkExecutor> SkExecutor::MakeMultiListLIFOThreadPool(int numWorkLists, |
| 213 |
int threads, |
| 214 |
bool allowBorrowing) { |
| 215 |
using WorkList = TArray<std::function<void(void)>>; |
| 216 |
return std::make_unique<SkThreadPool<WorkList>>(numWorkLists, |
| 217 |
threads > 0 ? threads : num_cores(), |
| 218 |
allowBorrowing); |
| 219 |
} |