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()); ~SkTaskGroup()24 ~SkTaskGroup() { this->wait(); } 25 26 // Add a task to this SkTaskGroup. 27 void add(std::function<void(void)> fn); 28 29 // Add a batch of N tasks, all calling fn with different arguments. 30 void batch(int N, std::function<void(int)> fn); 31 32 // Returns true if all Tasks previously add()ed to this SkTaskGroup have run. 33 // It is safe to reuse this SkTaskGroup once done(). 34 bool done() const; 35 36 // Block until done(). 37 void wait(); 38 39 // A convenience for testing tools. 40 // Creates and owns a thread pool, and passes it to SkExecutor::SetDefault(). 41 struct Enabler { 42 explicit Enabler(int threads = -1); // -1 -> num_cores, 0 -> noop 43 std::unique_ptr<SkExecutor> fThreadPool; 44 }; 45 46 private: 47 std::atomic<int32_t> fPending; 48 SkExecutor& fExecutor; 49 }; 50 51 #endif//SkTaskGroup_DEFINED 52