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 "src/base/SkNoDestructor.h"
13
14 #include <deque>
15 #include <thread>
16 #include <utility>
17
18 using namespace skia_private;
19
20 #if defined(SK_BUILD_FOR_WIN)
21 #include "src/base/SkLeanWindows.h"
num_cores()22 static int num_cores() {
23 SYSTEM_INFO sysinfo;
24 GetNativeSystemInfo(&sysinfo);
25 return (int)sysinfo.dwNumberOfProcessors;
26 }
27 #else
28 #include <unistd.h>
num_cores()29 static int num_cores() {
30 return (int)sysconf(_SC_NPROCESSORS_ONLN);
31 }
32 #endif
33
~SkExecutor()34 SkExecutor::~SkExecutor() {}
35
36 // The default default SkExecutor is an SkTrivialExecutor, which just runs the work right away.
37 class SkTrivialExecutor final : public SkExecutor {
add(std::function<void (void)> work)38 void add(std::function<void(void)> work) override {
39 work();
40 }
41 };
42
trivial_executor()43 static SkExecutor& trivial_executor() {
44 static SkNoDestructor<SkTrivialExecutor> executor;
45 return *executor;
46 }
47
48 static SkExecutor* gDefaultExecutor = nullptr;
49
GetDefault()50 SkExecutor& SkExecutor::GetDefault() {
51 if (gDefaultExecutor) {
52 return *gDefaultExecutor;
53 }
54 return trivial_executor();
55 }
56
SetDefault(SkExecutor * executor)57 void SkExecutor::SetDefault(SkExecutor* executor) {
58 gDefaultExecutor = executor;
59 }
60
61 // We'll always push_back() new work, but pop from the front of deques or the back of SkTArray.
pop(std::deque<std::function<void (void)>> * list)62 static inline std::function<void(void)> pop(std::deque<std::function<void(void)>>* list) {
63 std::function<void(void)> fn = std::move(list->front());
64 list->pop_front();
65 return fn;
66 }
pop(TArray<std::function<void (void)>> * list)67 static inline std::function<void(void)> pop(TArray<std::function<void(void)>>* list) {
68 std::function<void(void)> fn = std::move(list->back());
69 list->pop_back();
70 return fn;
71 }
72
73 // An SkThreadPool is an executor that runs work on a fixed pool of OS threads.
74 template <typename WorkList>
75 class SkThreadPool final : public SkExecutor {
76 public:
SkThreadPool(int threads,bool allowBorrowing)77 explicit SkThreadPool(int threads, bool allowBorrowing) : fAllowBorrowing(allowBorrowing) {
78 for (int i = 0; i < threads; i++) {
79 fThreads.emplace_back(&Loop, this);
80 }
81 }
82
~SkThreadPool()83 ~SkThreadPool() override {
84 // Signal each thread that it's time to shut down.
85 for (int i = 0; i < fThreads.size(); i++) {
86 this->add(nullptr);
87 }
88 // Wait for each thread to shut down.
89 for (int i = 0; i < fThreads.size(); i++) {
90 fThreads[i].join();
91 }
92 }
93
add(std::function<void (void)> work)94 void add(std::function<void(void)> work) override {
95 // Add some work to our pile of work to do.
96 {
97 SkAutoMutexExclusive lock(fWorkLock);
98 fWork.emplace_back(std::move(work));
99 }
100 // Tell the Loop() threads to pick it up.
101 fWorkAvailable.signal(1);
102 }
103
borrow()104 void borrow() override {
105 // If there is work waiting and we're allowed to borrow work, do it.
106 if (fAllowBorrowing && fWorkAvailable.try_wait()) {
107 SkAssertResult(this->do_work());
108 }
109 }
110
111 private:
112 // This method should be called only when fWorkAvailable indicates there's work to do.
do_work()113 bool do_work() {
114 std::function<void(void)> work;
115 {
116 SkAutoMutexExclusive lock(fWorkLock);
117 SkASSERT(!fWork.empty()); // TODO: if (fWork.empty()) { return true; } ?
118 work = pop(&fWork);
119 }
120
121 if (!work) {
122 return false; // This is Loop()'s signal to shut down.
123 }
124
125 work();
126 return true;
127 }
128
Loop(void * ctx)129 static void Loop(void* ctx) {
130 auto pool = (SkThreadPool*)ctx;
131 do {
132 pool->fWorkAvailable.wait();
133 } while (pool->do_work());
134 }
135
136 // Both SkMutex and SkSpinlock can work here.
137 using Lock = SkMutex;
138
139 TArray<std::thread> fThreads;
140 WorkList fWork;
141 Lock fWorkLock;
142 SkSemaphore fWorkAvailable;
143 bool fAllowBorrowing;
144 };
145
MakeFIFOThreadPool(int threads,bool allowBorrowing)146 std::unique_ptr<SkExecutor> SkExecutor::MakeFIFOThreadPool(int threads, bool allowBorrowing) {
147 using WorkList = std::deque<std::function<void(void)>>;
148 return std::make_unique<SkThreadPool<WorkList>>(threads > 0 ? threads : num_cores(),
149 allowBorrowing);
150 }
MakeLIFOThreadPool(int threads,bool allowBorrowing)151 std::unique_ptr<SkExecutor> SkExecutor::MakeLIFOThreadPool(int threads, bool allowBorrowing) {
152 using WorkList = TArray<std::function<void(void)>>;
153 return std::make_unique<SkThreadPool<WorkList>>(threads > 0 ? threads : num_cores(),
154 allowBorrowing);
155 }
156