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 <functional> 12 13 #include "SkTypes.h" 14 #include "SkAtomics.h" 15 #include "SkTemplates.h" 16 17 class SkTaskGroup : SkNoncopyable { 18 public: 19 // Create one of these in main() to enable SkTaskGroups globally. 20 struct Enabler : SkNoncopyable { 21 explicit Enabler(int threads = -1); // Default is system-reported core count. 22 ~Enabler(); 23 }; 24 25 SkTaskGroup(); ~SkTaskGroup()26 ~SkTaskGroup() { this->wait(); } 27 28 // Add a task to this SkTaskGroup. It will likely run on another thread. 29 void add(std::function<void(void)> fn); 30 31 // Add a batch of N tasks, all calling fn with different arguments. 32 void batch(int N, std::function<void(int)> fn); 33 34 // Block until all Tasks previously add()ed to this SkTaskGroup have run. 35 // You may safely reuse this SkTaskGroup after wait() returns. 36 void wait(); 37 38 private: 39 SkAtomic<int32_t> fPending; 40 }; 41 42 // Returns best estimate of number of CPU cores available to use. 43 int sk_num_cores(); 44 45 #endif//SkTaskGroup_DEFINED 46