blob: 864b8d5355a37fc9fc8cefcce4030ba9535572ce [file] [log] [blame]
[email protected]afecfb72013-04-18 17:17:331// 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
danakj0a448602015-03-10 00:31:165#ifndef BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_
6#define BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_
[email protected]afecfb72013-04-18 17:17:337
8#include <vector>
9
10#include "base/base_export.h"
[email protected]afecfb72013-04-18 17:17:3311#include "base/callback.h"
12#include "base/compiler_specific.h"
13#include "base/memory/ref_counted.h"
[email protected]fb441962013-05-08 05:35:2414#include "base/sequenced_task_runner.h"
[email protected]afecfb72013-04-18 17:17:3315#include "base/synchronization/lock.h"
Scott Violet875789e2018-02-02 07:46:4816#include "base/threading/platform_thread.h"
[email protected]99084f62013-06-28 00:49:0717#include "base/time/time.h"
[email protected]afecfb72013-04-18 17:17:3318
19namespace 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 Violet875789e2018-02-02 07:46:4823// 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]afecfb72013-04-18 17:17:3327class BASE_EXPORT DeferredSequencedTaskRunner : public SequencedTaskRunner {
28 public:
29 explicit DeferredSequencedTaskRunner(
vmpstr82b0c16d2016-03-18 19:17:2830 scoped_refptr<SequencedTaskRunner> target_runner);
[email protected]afecfb72013-04-18 17:17:3331
Scott Violet875789e2018-02-02 07:46:4832 // Use this constructor when you don't have the target SequencedTaskRunner.
33 // When using this call StartWithTaskRunner().
34 DeferredSequencedTaskRunner();
David Bienvenub4b441e2020-09-23 05:49:5735 DeferredSequencedTaskRunner(const DeferredSequencedTaskRunner&) = delete;
36 DeferredSequencedTaskRunner& operator=(const DeferredSequencedTaskRunner&) =
37 delete;
Scott Violet875789e2018-02-02 07:46:4838
[email protected]afecfb72013-04-18 17:17:3339 // TaskRunner implementation
Brett Wilson8e88b312017-09-12 05:22:1640 bool PostDelayedTask(const Location& from_here,
tzik6e427842017-04-05 10:13:2141 OnceClosure task,
dcheng56488182014-10-21 10:54:5142 TimeDelta delay) override;
peary23322df62017-05-09 03:55:4843 bool RunsTasksInCurrentSequence() const override;
[email protected]afecfb72013-04-18 17:17:3344
45 // SequencedTaskRunner implementation
Brett Wilson8e88b312017-09-12 05:22:1646 bool PostNonNestableDelayedTask(const Location& from_here,
tzik6e427842017-04-05 10:13:2147 OnceClosure task,
dcheng56488182014-10-21 10:54:5148 TimeDelta delay) override;
[email protected]afecfb72013-04-18 17:17:3349
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 Xiac04da47d2018-01-31 23:41:3153 // Fails when called a second time.
54 void Start();
[email protected]afecfb72013-04-18 17:17:3355
Scott Violet875789e2018-02-02 07:46:4856 // 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]afecfb72013-04-18 17:17:3360 private:
61 struct DeferredTask {
62 DeferredTask();
tzik070c8ffb2017-03-29 05:28:1263 DeferredTask(DeferredTask&& other);
[email protected]afecfb72013-04-18 17:17:3364 ~DeferredTask();
tzik070c8ffb2017-03-29 05:28:1265 DeferredTask& operator=(DeferredTask&& other);
[email protected]afecfb72013-04-18 17:17:3366
Brett Wilson8e88b312017-09-12 05:22:1667 Location posted_from;
tzik6e427842017-04-05 10:13:2168 OnceClosure task;
[email protected]afecfb72013-04-18 17:17:3369 // The delay this task was initially posted with.
70 TimeDelta delay;
71 bool is_non_nestable;
72 };
73
dcheng56488182014-10-21 10:54:5174 ~DeferredSequencedTaskRunner() override;
[email protected]afecfb72013-04-18 17:17:3375
Scott Violet875789e2018-02-02 07:46:4876 // Both variants of Start() call into this.
77 void StartImpl();
78
[email protected]afecfb72013-04-18 17:17:3379 // Creates a |Task| object and adds it to |deferred_tasks_queue_|.
Brett Wilson8e88b312017-09-12 05:22:1680 void QueueDeferredTask(const Location& from_here,
tzik6e427842017-04-05 10:13:2181 OnceClosure task,
[email protected]afecfb72013-04-18 17:17:3382 TimeDelta delay,
83 bool is_non_nestable);
84
[email protected]afecfb72013-04-18 17:17:3385 mutable Lock lock_;
86
Scott Violet875789e2018-02-02 07:46:4887 const PlatformThreadId created_thread_id_;
88
Benoit Lize25859152020-07-09 11:52:0989 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]afecfb72013-04-18 17:17:3392};
93
94} // namespace base
95
danakj0a448602015-03-10 00:31:1696#endif // BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_