[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" | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | fb44196 | 2013-05-08 05:35:24 | [diff] [blame] | 15 | #include "base/sequenced_task_runner.h" |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 16 | #include "base/synchronization/lock.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 | #include "base/tracked_objects.h" |
19 | |||||
20 | namespace base { | ||||
21 | |||||
22 | // A DeferredSequencedTaskRunner is a subclass of SequencedTaskRunner that | ||||
23 | // queues up all requests until the first call to Start() is issued. | ||||
24 | class BASE_EXPORT DeferredSequencedTaskRunner : public SequencedTaskRunner { | ||||
25 | public: | ||||
26 | explicit DeferredSequencedTaskRunner( | ||||
vmpstr | 82b0c16d | 2016-03-18 19:17:28 | [diff] [blame] | 27 | scoped_refptr<SequencedTaskRunner> target_runner); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 28 | |
29 | // TaskRunner implementation | ||||
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 30 | bool PostDelayedTask(const tracked_objects::Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 31 | OnceClosure task, |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 32 | TimeDelta delay) override; |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame^] | 33 | bool RunsTasksInCurrentSequence() const override; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 34 | |
35 | // SequencedTaskRunner implementation | ||||
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 36 | bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 37 | OnceClosure task, |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 38 | TimeDelta delay) override; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 39 | |
40 | // Start the execution - posts all queued tasks to the target executor. The | ||||
41 | // deferred tasks are posted with their initial delay, meaning that the task | ||||
42 | // execution delay is actually measured from Start. | ||||
43 | // Fails when called a second time. | ||||
44 | void Start(); | ||||
45 | |||||
46 | private: | ||||
47 | struct DeferredTask { | ||||
48 | DeferredTask(); | ||||
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 49 | DeferredTask(DeferredTask&& other); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 50 | ~DeferredTask(); |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 51 | DeferredTask& operator=(DeferredTask&& other); |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 52 | |
53 | tracked_objects::Location posted_from; | ||||
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 54 | OnceClosure task; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 55 | // The delay this task was initially posted with. |
56 | TimeDelta delay; | ||||
57 | bool is_non_nestable; | ||||
58 | }; | ||||
59 | |||||
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 60 | ~DeferredSequencedTaskRunner() override; |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 61 | |
62 | // Creates a |Task| object and adds it to |deferred_tasks_queue_|. | ||||
63 | void QueueDeferredTask(const tracked_objects::Location& from_here, | ||||
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 64 | OnceClosure task, |
[email protected] | afecfb7 | 2013-04-18 17:17:33 | [diff] [blame] | 65 | TimeDelta delay, |
66 | bool is_non_nestable); | ||||
67 | |||||
68 | // // Protects |started_| and |deferred_tasks_queue_|. | ||||
69 | mutable Lock lock_; | ||||
70 | |||||
71 | bool started_; | ||||
72 | const scoped_refptr<SequencedTaskRunner> target_task_runner_; | ||||
73 | std::vector<DeferredTask> deferred_tasks_queue_; | ||||
74 | |||||
75 | DISALLOW_COPY_AND_ASSIGN(DeferredSequencedTaskRunner); | ||||
76 | }; | ||||
77 | |||||
78 | } // namespace base | ||||
79 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 80 | #endif // BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_ |