Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkTaskGroup.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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 */
8
10
11#include <type_traits>
12#include <utility>
13
14SkTaskGroup::SkTaskGroup(SkExecutor& executor) : fPending(0), fExecutor(executor) {}
15
16void SkTaskGroup::add(std::function<void(void)> fn) {
17 fPending.fetch_add(+1, std::memory_order_relaxed);
18 fExecutor.add([this, fn{std::move(fn)}] {
19 fn();
20 fPending.fetch_add(-1, std::memory_order_release);
21 });
22}
23
24void SkTaskGroup::batch(int N, std::function<void(int)> fn) {
25 // TODO: I really thought we had some sort of more clever chunking logic.
26 fPending.fetch_add(+N, std::memory_order_relaxed);
27 for (int i = 0; i < N; i++) {
28 fExecutor.add([fn, i, this] {
29 fn(i);
30 fPending.fetch_add(-1, std::memory_order_release);
31 });
32 }
33}
34
35bool SkTaskGroup::done() const {
36 return fPending.load(std::memory_order_acquire) == 0;
37}
38
40 // Actively help the executor do work until our task group is done.
41 // This lets SkTaskGroups nest arbitrarily deep on a single SkExecutor:
42 // no thread ever blocks waiting for others to do its work.
43 // (We may end up doing work that's not part of our task group. That's fine.)
44 while (!this->done()) {
45 fExecutor.borrow();
46 }
47}
48
50 if (threads) {
53 }
54}
#define N
Definition beziers.cpp:19
virtual void add(std::function< void(void)>)=0
virtual void borrow()
Definition SkExecutor.h:33
static void SetDefault(SkExecutor *)
static std::unique_ptr< SkExecutor > MakeLIFOThreadPool(int threads=0, bool allowBorrowing=true)
SkTaskGroup(SkExecutor &executor=SkExecutor::GetDefault())
void batch(int N, std::function< void(int)> fn)
void add(std::function< void(void)> fn)
bool done() const
Enabler(int threads=-1)
std::unique_ptr< SkExecutor > fThreadPool
Definition SkTaskGroup.h:43