| 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 |
*/ |
| 7 |
|
| 8 |
#ifndef SkTaskGroup_DEFINED |
| 9 |
#define SkTaskGroup_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkExecutor.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "include/private/base/SkNoncopyable.h" |
| 14 |
|
| 15 |
#include <atomic> |
| 16 |
#include <cstdint> |
| 17 |
#include <functional> |
| 18 |
#include <memory> |
| 19 |
|
| 20 |
class SkTaskGroup : SkNoncopyable { |
| 21 |
public: |
| 22 |
// Tasks added to this SkTaskGroup will run on its executor. |
| 23 |
explicit SkTaskGroup(SkExecutor& executor = SkExecutor::GetDefault()); |
| 24 |
~SkTaskGroup() { this->wait(); } |
| 25 |
|
| 26 |
// Add a task to this SkTaskGroup. |
| 27 |
void add(std::function<void(void)> fn); |
| 28 |
void add(std::function<void(void)> fn, int workList); |
| 29 |
|
| 30 |
void discardAllPendingWork(); |
| 31 |
|
| 32 |
// Add a batch of N tasks, all calling fn with different arguments. |
| 33 |
void batch(int N, std::function<void(int)> fn); |
| 34 |
|
| 35 |
// Returns true if all Tasks previously add()ed to this SkTaskGroup have run. |
| 36 |
// It is safe to reuse this SkTaskGroup once done(). |
| 37 |
bool done() const; |
| 38 |
|
| 39 |
// Block until done(). |
| 40 |
void wait(); |
| 41 |
|
| 42 |
// A convenience for testing tools. |
| 43 |
// Creates and owns a thread pool, and passes it to SkExecutor::SetDefault(). |
| 44 |
struct Enabler { |
| 45 |
explicit Enabler(int threads = -1); // -1 -> num_cores, 0 -> noop |
| 46 |
std::unique_ptr<SkExecutor> fThreadPool; |
| 47 |
}; |
| 48 |
|
| 49 |
private: |
| 50 |
std::atomic<int32_t> fPending; |
| 51 |
SkExecutor& fExecutor; |
| 52 |
}; |
| 53 |
|
| 54 |
#endif//SkTaskGroup_DEFINED |