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