// The default default SkExecutor is an SkTrivialExecutor, which just runs the work right away. class SkTrivialExecutor final : public SkExecutor { void add(std::function<void(void)> work) override {
work();
}
};
// We'll always push_back() new work, but pop from the front of deques or the back of SkTArray. staticinline std::function<void(void)> pop(std::deque<std::function<void(void)>>* list) {
std::function<void(void)> fn = std::move(list->front());
list->pop_front(); return fn;
} staticinline std::function<void(void)> pop(TArray<std::function<void(void)>>* list) {
std::function<void(void)> fn = std::move(list->back());
list->pop_back(); return fn;
}
// An SkThreadPool is an executor that runs work on a fixed pool of OS threads. template <typename WorkList> class SkThreadPool final : public SkExecutor { public: explicit SkThreadPool(int threads, bool allowBorrowing) : fAllowBorrowing(allowBorrowing) { for (int i = 0; i < threads; i++) {
fThreads.emplace_back(&Loop, this);
}
}
~SkThreadPool() override { // Signal each thread that it's time to shut down. for (int i = 0; i < fThreads.size(); i++) {
this->add(nullptr);
} // Wait for each thread to shut down. for (int i = 0; i < fThreads.size(); i++) {
fThreads[i].join();
}
}
void add(std::function<void(void)> work) override { // Add some work to our pile of work to do.
{
SkAutoMutexExclusive lock(fWorkLock);
fWork.emplace_back(std::move(work));
} // Tell the Loop() threads to pick it up.
fWorkAvailable.signal(1);
}
void borrow() override { // If there is work waiting and we're allowed to borrow work, do it. if (fAllowBorrowing && fWorkAvailable.try_wait()) {
SkAssertResult(this->do_work());
}
}
private: // This method should be called only when fWorkAvailable indicates there's work to do. bool do_work() {
std::function<void(void)> work;
{
SkAutoMutexExclusive lock(fWorkLock);
SkASSERT(!fWork.empty()); // TODO: if (fWork.empty()) { return true; } ?
work = pop(&fWork);
}
if (!work) { returnfalse; // This is Loop()'s signal to shut down.
}
work(); returntrue;
}
staticvoid Loop(void* ctx) { auto pool = (SkThreadPool*)ctx; do {
pool->fWorkAvailable.wait();
} while (pool->do_work());
}
// Both SkMutex and SkSpinlock can work here. using Lock = SkMutex;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.