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