Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2015 The Chromium Authors |
bauerb | 8a40872 | 2015-10-30 09:05:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_THREADING_SEQUENCED_TASK_RUNNER_HANDLE_H_ |
| 6 | #define BASE_THREADING_SEQUENCED_TASK_RUNNER_HANDLE_H_ |
| 7 | |
Gabriel Charette | b62806f4a | 2019-01-29 21:08:41 | [diff] [blame] | 8 | #include "base/base_export.h" |
Gabriel Charette | b62806f4a | 2019-01-29 21:08:41 | [diff] [blame] | 9 | #include "base/memory/scoped_refptr.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 10 | #include "base/task/sequenced_task_runner.h" |
bauerb | 8a40872 | 2015-10-30 09:05:31 | [diff] [blame] | 11 | |
| 12 | namespace base { |
| 13 | |
Gabriel Charette | b62806f4a | 2019-01-29 21:08:41 | [diff] [blame] | 14 | class ThreadTaskRunnerHandle; |
| 15 | |
bauerb | 8a40872 | 2015-10-30 09:05:31 | [diff] [blame] | 16 | class BASE_EXPORT SequencedTaskRunnerHandle { |
| 17 | public: |
| 18 | // Returns a SequencedTaskRunner which guarantees that posted tasks will only |
| 19 | // run after the current task is finished and will satisfy a SequenceChecker. |
| 20 | // It should only be called if IsSet() returns true (see the comment there for |
| 21 | // the requirements). |
Daniel Cheng | 4455c984 | 2022-01-13 23:26:37 | [diff] [blame] | 22 | [[nodiscard]] static const scoped_refptr<SequencedTaskRunner>& Get(); |
bauerb | 8a40872 | 2015-10-30 09:05:31 | [diff] [blame] | 23 | |
| 24 | // Returns true if one of the following conditions is fulfilled: |
gab | a8e9e2c | 2016-05-02 21:17:47 | [diff] [blame] | 25 | // a) A SequencedTaskRunner has been assigned to the current thread by |
| 26 | // instantiating a SequencedTaskRunnerHandle. |
| 27 | // b) The current thread has a ThreadTaskRunnerHandle (which includes any |
Francois Doray | 4914ec14 | 2018-02-22 12:48:38 | [diff] [blame] | 28 | // thread that has a MessageLoop associated with it). |
Daniel Cheng | 4455c984 | 2022-01-13 23:26:37 | [diff] [blame] | 29 | [[nodiscard]] static bool IsSet(); |
bauerb | 8a40872 | 2015-10-30 09:05:31 | [diff] [blame] | 30 | |
gab | a8e9e2c | 2016-05-02 21:17:47 | [diff] [blame] | 31 | // Binds |task_runner| to the current thread. |
| 32 | explicit SequencedTaskRunnerHandle( |
| 33 | scoped_refptr<SequencedTaskRunner> task_runner); |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 34 | |
| 35 | SequencedTaskRunnerHandle(const SequencedTaskRunnerHandle&) = delete; |
| 36 | SequencedTaskRunnerHandle& operator=(const SequencedTaskRunnerHandle&) = |
| 37 | delete; |
| 38 | |
gab | a8e9e2c | 2016-05-02 21:17:47 | [diff] [blame] | 39 | ~SequencedTaskRunnerHandle(); |
| 40 | |
bauerb | 8a40872 | 2015-10-30 09:05:31 | [diff] [blame] | 41 | private: |
Minoru Chikamune | 76ba682 | 2021-01-12 10:39:51 | [diff] [blame] | 42 | friend class ThreadTaskRunnerHandleOverride; |
Gabriel Charette | b62806f4a | 2019-01-29 21:08:41 | [diff] [blame] | 43 | |
gab | a8e9e2c | 2016-05-02 21:17:47 | [diff] [blame] | 44 | scoped_refptr<SequencedTaskRunner> task_runner_; |
bauerb | 8a40872 | 2015-10-30 09:05:31 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | } // namespace base |
| 48 | |
| 49 | #endif // BASE_THREADING_SEQUENCED_TASK_RUNNER_HANDLE_H_ |