Home | History | Annotate | Download | only in core
      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());
     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     // Returns true if all Tasks previously add()ed to this SkTaskGroup have run.
     29     // It is safe to reuse this SkTaskGroup once done().
     30     bool done() const;
     31 
     32     // Block until done().
     33     void wait();
     34 
     35     // A convenience for testing tools.
     36     // Creates and owns a thread pool, and passes it to SkExecutor::SetDefault().
     37     struct Enabler {
     38         explicit Enabler(int threads = -1);  // -1 -> num_cores, 0 -> noop
     39         std::unique_ptr<SkExecutor> fThreadPool;
     40     };
     41 
     42 private:
     43     std::atomic<int32_t> fPending;
     44     SkExecutor&          fExecutor;
     45 };
     46 
     47 #endif//SkTaskGroup_DEFINED
     48