1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2014 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SkTaskGroup_DEFINED
9cb93a386Sopenharmony_ci#define SkTaskGroup_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkExecutor.h"
12cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
13cb93a386Sopenharmony_ci#include "include/private/SkNoncopyable.h"
14cb93a386Sopenharmony_ci#include <atomic>
15cb93a386Sopenharmony_ci#include <functional>
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ciclass SkTaskGroup : SkNoncopyable {
18cb93a386Sopenharmony_cipublic:
19cb93a386Sopenharmony_ci    // Tasks added to this SkTaskGroup will run on its executor.
20cb93a386Sopenharmony_ci    explicit SkTaskGroup(SkExecutor& executor = SkExecutor::GetDefault());
21cb93a386Sopenharmony_ci    ~SkTaskGroup() { this->wait(); }
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci    // Add a task to this SkTaskGroup.
24cb93a386Sopenharmony_ci    void add(std::function<void(void)> fn);
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci    // Add a batch of N tasks, all calling fn with different arguments.
27cb93a386Sopenharmony_ci    void batch(int N, std::function<void(int)> fn);
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_ci    // Returns true if all Tasks previously add()ed to this SkTaskGroup have run.
30cb93a386Sopenharmony_ci    // It is safe to reuse this SkTaskGroup once done().
31cb93a386Sopenharmony_ci    bool done() const;
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    // Block until done().
34cb93a386Sopenharmony_ci    void wait();
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    // A convenience for testing tools.
37cb93a386Sopenharmony_ci    // Creates and owns a thread pool, and passes it to SkExecutor::SetDefault().
38cb93a386Sopenharmony_ci    struct Enabler {
39cb93a386Sopenharmony_ci        explicit Enabler(int threads = -1);  // -1 -> num_cores, 0 -> noop
40cb93a386Sopenharmony_ci        std::unique_ptr<SkExecutor> fThreadPool;
41cb93a386Sopenharmony_ci    };
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_ciprivate:
44cb93a386Sopenharmony_ci    std::atomic<int32_t> fPending;
45cb93a386Sopenharmony_ci    SkExecutor&          fExecutor;
46cb93a386Sopenharmony_ci};
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci#endif//SkTaskGroup_DEFINED
49