[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 5 | #ifndef BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_ |
6 | #define BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_ | ||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 7 | |
8 | #include <vector> | ||||
9 | |||||
10 | #include "base/base_export.h" | ||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 11 | #include "base/callback.h" |
12 | #include "base/compiler_specific.h" | ||||
13 | #include "base/memory/ref_counted.h" | ||||
[email protected] | fb44196 | 2013-05-08 05:35:24 | [diff] [blame] | 14 | #include "base/sequenced_task_runner.h" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 16 | #include "base/threading/platform_thread.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 17 | #include "base/time/time.h" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 18 | |
19 | namespace base { | ||||
20 | |||||
21 | // A DeferredSequencedTaskRunner is a subclass of SequencedTaskRunner that | ||||
22 | // queues up all requests until the first call to Start() is issued. | ||||
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 23 | // DeferredSequencedTaskRunner may be created in two ways: |
24 | // . with an explicit SequencedTaskRunner that the events are flushed to | ||||
25 | // . without a SequencedTaskRunner. In this configuration the | ||||
26 | // SequencedTaskRunner is supplied in StartWithTaskRunner(). | ||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 27 | class BASE_EXPORT DeferredSequencedTaskRunner : public SequencedTaskRunner { |
28 | public: | ||||
29 | explicit DeferredSequencedTaskRunner( | ||||
vmpstr | 82b0c16d | 2016-03-18 19:17:28 | [diff] [blame] | 30 | scoped_refptr<SequencedTaskRunner> target_runner); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 31 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 32 | // Use this constructor when you don't have the target SequencedTaskRunner. |
33 | // When using this call StartWithTaskRunner(). | ||||
34 | DeferredSequencedTaskRunner(); | ||||
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 35 | DeferredSequencedTaskRunner(const DeferredSequencedTaskRunner&) = delete; |
36 | DeferredSequencedTaskRunner& operator=(const DeferredSequencedTaskRunner&) = | ||||
37 | delete; | ||||
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 38 | |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 39 | // TaskRunner implementation |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 40 | bool PostDelayedTask(const Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 41 | OnceClosure task, |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 42 | TimeDelta delay) override; |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 43 | bool RunsTasksInCurrentSequence() const override; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 44 | |
45 | // SequencedTaskRunner implementation | ||||
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 46 | bool PostNonNestableDelayedTask(const Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 47 | OnceClosure task, |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 48 | TimeDelta delay) override; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 49 | |
50 | // Start the execution - posts all queued tasks to the target executor. The | ||||
51 | // deferred tasks are posted with their initial delay, meaning that the task | ||||
52 | // execution delay is actually measured from Start. | ||||
Xiyuan Xia | c04da47d | 2018-01-31 23:41:31 | [diff] [blame] | 53 | // Fails when called a second time. |
54 | void Start(); | ||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 55 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 56 | // Same as Start(), but must be used with the no-arg constructor. |
57 | void StartWithTaskRunner( | ||||
58 | scoped_refptr<SequencedTaskRunner> target_task_runner); | ||||
59 | |||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 60 | private: |
61 | struct DeferredTask { | ||||
62 | DeferredTask(); | ||||
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 63 | DeferredTask(DeferredTask&& other); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 64 | ~DeferredTask(); |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 65 | DeferredTask& operator=(DeferredTask&& other); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 66 | |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 67 | Location posted_from; |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 68 | OnceClosure task; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 69 | // The delay this task was initially posted with. |
70 | TimeDelta delay; | ||||
71 | bool is_non_nestable; | ||||
72 | }; | ||||
73 | |||||
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 74 | ~DeferredSequencedTaskRunner() override; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 75 | |
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 76 | // Both variants of Start() call into this. |
77 | void StartImpl(); | ||||
78 | |||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 79 | // Creates a |Task| object and adds it to |deferred_tasks_queue_|. |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 80 | void QueueDeferredTask(const Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 81 | OnceClosure task, |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 82 | TimeDelta delay, |
83 | bool is_non_nestable); | ||||
84 | |||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 85 | mutable Lock lock_; |
86 | |||||
Scott Violet | 875789e | 2018-02-02 07:46:48 | [diff] [blame] | 87 | const PlatformThreadId created_thread_id_; |
88 | |||||
Benoit Lize | 2585915 | 2020-07-09 11:52:09 | [diff] [blame] | 89 | bool started_ GUARDED_BY(lock_) = false; |
90 | scoped_refptr<SequencedTaskRunner> target_task_runner_ GUARDED_BY(lock_); | ||||
91 | std::vector<DeferredTask> deferred_tasks_queue_ GUARDED_BY(lock_); | ||||
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 92 | }; |
93 | |||||
94 | } // namespace base | ||||
95 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 96 | #endif // BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_ |