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