simple_task_executor.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TASK_SIMPLE_TASK_EXECUTOR_H_
  5. #define BASE_TASK_SIMPLE_TASK_EXECUTOR_H_
  6. #include "base/task/task_executor.h"
  7. #include "build/build_config.h"
  8. namespace base {
  9. // A simple TaskExecutor with exactly one SingleThreadTaskRunner.
  10. // Must be instantiated and destroyed on the thread that runs tasks for the
  11. // SingleThreadTaskRunner.
  12. class BASE_EXPORT SimpleTaskExecutor : public TaskExecutor {
  13. public:
  14. explicit SimpleTaskExecutor(scoped_refptr<SingleThreadTaskRunner> task_queue);
  15. ~SimpleTaskExecutor() override;
  16. bool PostDelayedTask(const Location& from_here,
  17. const TaskTraits& traits,
  18. OnceClosure task,
  19. TimeDelta delay) override;
  20. scoped_refptr<TaskRunner> CreateTaskRunner(const TaskTraits& traits) override;
  21. scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunner(
  22. const TaskTraits& traits) override;
  23. scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunner(
  24. const TaskTraits& traits,
  25. SingleThreadTaskRunnerThreadMode thread_mode) override;
  26. #if defined(OS_WIN)
  27. scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunner(
  28. const TaskTraits& traits,
  29. SingleThreadTaskRunnerThreadMode thread_mode) override;
  30. #endif // defined(OS_WIN)
  31. private:
  32. const scoped_refptr<SingleThreadTaskRunner> task_queue_;
  33. // In tests there may already be a TaskExecutor registered for the thread, we
  34. // keep tack of the previous TaskExecutor and restored it upon destruction.
  35. TaskExecutor* const previous_task_executor_;
  36. };
  37. } // namespace base
  38. #endif // BASE_TASK_SIMPLE_TASK_EXECUTOR_H_