task_executor.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 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_TASK_EXECUTOR_H_
  5. #define BASE_TASK_TASK_EXECUTOR_H_
  6. #include <stdint.h>
  7. #include "base/base_export.h"
  8. #include "base/task/sequenced_task_runner.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "base/task/single_thread_task_runner_thread_mode.h"
  11. #include "base/task/task_runner.h"
  12. #include "build/build_config.h"
  13. namespace base {
  14. class Location;
  15. class TaskTraits;
  16. // A TaskExecutor can execute Tasks with a specific TaskTraits extension id.
  17. // TODO(1026641): Still used by task_runner_android.cc to reach
  18. // BrowserTaskExecutor but direct APIs should now be preferred where possible
  19. // (such as content::GetUIThreadTaskRunner). Follow-up cleanup expected.
  20. class BASE_EXPORT TaskExecutor {
  21. public:
  22. virtual ~TaskExecutor() = default;
  23. // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. For
  24. // one off tasks that don't require a TaskRunner. Returns false if the task
  25. // definitely won't run because of current shutdown state.
  26. virtual bool PostDelayedTask(const Location& from_here,
  27. const TaskTraits& traits,
  28. OnceClosure task,
  29. TimeDelta delay) = 0;
  30. // Returns a TaskRunner whose PostTask invocations result in scheduling tasks
  31. // using |traits|. Tasks may run in any order and in parallel.
  32. virtual scoped_refptr<TaskRunner> CreateTaskRunner(
  33. const TaskTraits& traits) = 0;
  34. // Returns a SequencedTaskRunner whose PostTask invocations result in
  35. // scheduling tasks using |traits|. Tasks run one at a time in posting order.
  36. virtual scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunner(
  37. const TaskTraits& traits) = 0;
  38. // Returns a SingleThreadTaskRunner whose PostTask invocations result in
  39. // scheduling tasks using |traits|. Tasks run on a single thread in posting
  40. // order. If |traits| identifies an existing thread,
  41. // SingleThreadTaskRunnerThreadMode::SHARED must be used.
  42. virtual scoped_refptr<SingleThreadTaskRunner> CreateSingleThreadTaskRunner(
  43. const TaskTraits& traits,
  44. SingleThreadTaskRunnerThreadMode thread_mode) = 0;
  45. #if BUILDFLAG(IS_WIN)
  46. // Returns a SingleThreadTaskRunner whose PostTask invocations result in
  47. // scheduling tasks using |traits| in a COM Single-Threaded Apartment. Tasks
  48. // run in the same Single-Threaded Apartment in posting order for the returned
  49. // SingleThreadTaskRunner. If |traits| identifies an existing thread,
  50. // SingleThreadTaskRunnerThreadMode::SHARED must be used.
  51. virtual scoped_refptr<SingleThreadTaskRunner> CreateCOMSTATaskRunner(
  52. const TaskTraits& traits,
  53. SingleThreadTaskRunnerThreadMode thread_mode) = 0;
  54. #endif // BUILDFLAG(IS_WIN)
  55. };
  56. // Register a TaskExecutor with the //base/task/post_task.h API in the current
  57. // process for tasks subsequently posted with a TaskTraits extension with the
  58. // given |extension_id|. All executors need to be registered before any tasks
  59. // are posted with |extension_id|. Only one executor per |extension_id| is
  60. // supported.
  61. void BASE_EXPORT RegisterTaskExecutor(uint8_t extension_id,
  62. TaskExecutor* task_executor);
  63. void BASE_EXPORT UnregisterTaskExecutorForTesting(uint8_t extension_id);
  64. // Stores the provided TaskExecutor in TLS for the current thread, to be used by
  65. // tasks with the CurrentThread() trait.
  66. void BASE_EXPORT SetTaskExecutorForCurrentThread(TaskExecutor* task_executor);
  67. // Returns the task executor registered for the current thread.
  68. BASE_EXPORT TaskExecutor* GetTaskExecutorForCurrentThread();
  69. // Determines whether a registered TaskExecutor will handle tasks with the given
  70. // |traits| and, if so, returns a pointer to it. Otherwise, returns |nullptr|.
  71. TaskExecutor* GetRegisteredTaskExecutorForTraits(const TaskTraits& traits);
  72. } // namespace base
  73. #endif // BASE_TASK_TASK_EXECUTOR_H_