SkTaskGroup.h 1.4 KB

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