Flutter Engine
The Flutter Engine
SkExecutor.cpp
Go to the documentation of this file.
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
13
14#include <deque>
15#include <thread>
16#include <utility>
17
18using namespace skia_private;
19
20#if defined(SK_BUILD_FOR_WIN)
22 static int num_cores() {
23 SYSTEM_INFO sysinfo;
24 GetNativeSystemInfo(&sysinfo);
25 return (int)sysinfo.dwNumberOfProcessors;
26 }
27#else
28 #include <unistd.h>
29 static int num_cores() {
30 return (int)sysconf(_SC_NPROCESSORS_ONLN);
31 }
32#endif
33
35
36// The default default SkExecutor is an SkTrivialExecutor, which just runs the work right away.
37class SkTrivialExecutor final : public SkExecutor {
38 void add(std::function<void(void)> work) override {
39 work();
40 }
41};
42
45 return *executor;
46}
47
48static SkExecutor* gDefaultExecutor = nullptr;
49
51 if (gDefaultExecutor) {
52 return *gDefaultExecutor;
53 }
54 return trivial_executor();
55}
56
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.
62static 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}
67static 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.
74template <typename WorkList>
75class SkThreadPool final : public SkExecutor {
76public:
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
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
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
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
111private:
112 // This method should be called only when fWorkAvailable indicates there's work to do.
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
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
146std::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}
151std::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}
AutoreleasePool pool
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkExecutor & trivial_executor()
Definition: SkExecutor.cpp:43
static std::function< void(void)> pop(std::deque< std::function< void(void)> > *list)
Definition: SkExecutor.cpp:62
static int num_cores()
Definition: SkExecutor.cpp:29
static SkExecutor * gDefaultExecutor
Definition: SkExecutor.cpp:48
virtual ~SkExecutor()
Definition: SkExecutor.cpp:34
static std::unique_ptr< SkExecutor > MakeFIFOThreadPool(int threads=0, bool allowBorrowing=true)
Definition: SkExecutor.cpp:146
static SkExecutor & GetDefault()
Definition: SkExecutor.cpp:50
static void SetDefault(SkExecutor *)
Definition: SkExecutor.cpp:57
static std::unique_ptr< SkExecutor > MakeLIFOThreadPool(int threads=0, bool allowBorrowing=true)
Definition: SkExecutor.cpp:151
void signal(int n=1)
Definition: SkSemaphore.h:56
SK_SPI bool try_wait()
Definition: SkSemaphore.cpp:77
void borrow() override
Definition: SkExecutor.cpp:104
SkThreadPool(int threads, bool allowBorrowing)
Definition: SkExecutor.cpp:77
~SkThreadPool() override
Definition: SkExecutor.cpp:83
void add(std::function< void(void)> work) override
Definition: SkExecutor.cpp:94
int size() const
Definition: SkTArray.h:421
T & emplace_back(Args &&... args)
Definition: SkTArray.h:248
Dart_NativeFunction function
Definition: fuchsia.cc:51