blob: e4c6b41db6178d89eceb135793ffc38e52b4f487 [file] [log] [blame]
[email protected]6b28d942012-02-15 01:43:191// Copyright (c) 2012 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
5#ifndef BASE_TASK_RUNNER_H_
6#define BASE_TASK_RUNNER_H_
[email protected]6b28d942012-02-15 01:43:197
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9
[email protected]6b28d942012-02-15 01:43:1910#include "base/base_export.h"
tzik03527512017-02-08 12:29:4711#include "base/callback.h"
fdoray1d08c582016-06-09 22:55:0612#include "base/location.h"
[email protected]6b28d942012-02-15 01:43:1913#include "base/memory/ref_counted.h"
[email protected]8f9a3a52013-06-28 15:14:1814#include "base/time/time.h"
[email protected]6b28d942012-02-15 01:43:1915
[email protected]6b28d942012-02-15 01:43:1916namespace base {
17
18struct TaskRunnerTraits;
19
20// A TaskRunner is an object that runs posted tasks (in the form of
21// Closure objects). The TaskRunner interface provides a way of
22// decoupling task posting from the mechanics of how each task will be
23// run. TaskRunner provides very weak guarantees as to how posted
24// tasks are run (or if they're run at all). In particular, it only
25// guarantees:
26//
27// - Posting a task will not run it synchronously. That is, no
28// Post*Task method will call task.Run() directly.
29//
30// - Increasing the delay can only delay when the task gets run.
31// That is, increasing the delay may not affect when the task gets
32// run, or it could make it run later than it normally would, but
33// it won't make it run earlier than it normally would.
34//
35// TaskRunner does not guarantee the order in which posted tasks are
36// run, whether tasks overlap, or whether they're run on a particular
37// thread. Also it does not guarantee a memory model for shared data
38// between tasks. (In other words, you should use your own
39// synchronization/locking primitives if you need to share data
40// between tasks.)
41//
42// Implementations of TaskRunner should be thread-safe in that all
43// methods must be safe to call on any thread. Ownership semantics
44// for TaskRunners are in general not clear, which is why the
45// interface itself is RefCountedThreadSafe.
46//
47// Some theoretical implementations of TaskRunner:
48//
49// - A TaskRunner that uses a thread pool to run posted tasks.
50//
51// - A TaskRunner that, for each task, spawns a non-joinable thread
52// to run that task and immediately quit.
53//
54// - A TaskRunner that stores the list of posted tasks and has a
55// method Run() that runs each runnable task in random order.
56class BASE_EXPORT TaskRunner
57 : public RefCountedThreadSafe<TaskRunner, TaskRunnerTraits> {
58 public:
59 // Posts the given task to be run. Returns true if the task may be
60 // run at some point in the future, and false if the task definitely
61 // will not be run.
62 //
63 // Equivalent to PostDelayedTask(from_here, task, 0).
Brett Wilson8e88b312017-09-12 05:22:1664 bool PostTask(const Location& from_here, OnceClosure task);
[email protected]6b28d942012-02-15 01:43:1965
Wezda38b062017-07-18 17:19:4666 // Like PostTask, but tries to run the posted task only after |delay_ms|
67 // has passed. Implementations should use a tick clock, rather than wall-
68 // clock time, to implement |delay|.
Brett Wilson8e88b312017-09-12 05:22:1669 virtual bool PostDelayedTask(const Location& from_here,
tzik6e427842017-04-05 10:13:2170 OnceClosure task,
[email protected]17dc6742012-02-26 08:17:3771 base::TimeDelta delay) = 0;
[email protected]6b28d942012-02-15 01:43:1972
peary23322df62017-05-09 03:55:4873 // Returns true iff tasks posted to this TaskRunner are sequenced
74 // with this call.
[email protected]6b28d942012-02-15 01:43:1975 //
peary23322df62017-05-09 03:55:4876 // In particular:
77 // - Returns true if this is a SequencedTaskRunner to which the
78 // current task was posted.
79 // - Returns true if this is a SequencedTaskRunner bound to the
80 // same sequence as the SequencedTaskRunner to which the current
81 // task was posted.
82 // - Returns true if this is a SingleThreadTaskRunner bound to
83 // the current thread.
84 // TODO(http://crbug.com/665062):
85 // This API doesn't make sense for parallel TaskRunners.
86 // Introduce alternate static APIs for documentation purposes of "this runs
87 // in pool X", have RunsTasksInCurrentSequence() return false for parallel
88 // TaskRunners, and ultimately move this method down to SequencedTaskRunner.
89 virtual bool RunsTasksInCurrentSequence() const = 0;
[email protected]6b28d942012-02-15 01:43:1990
91 // Posts |task| on the current TaskRunner. On completion, |reply|
92 // is posted to the thread that called PostTaskAndReply(). Both
93 // |task| and |reply| are guaranteed to be deleted on the thread
94 // from which PostTaskAndReply() is invoked. This allows objects
95 // that must be deleted on the originating thread to be bound into
96 // the |task| and |reply| Closures. In particular, it can be useful
97 // to use WeakPtr<> in the |reply| Closure so that the reply
98 // operation can be canceled. See the following pseudo-code:
99 //
100 // class DataBuffer : public RefCountedThreadSafe<DataBuffer> {
101 // public:
102 // // Called to add data into a buffer.
103 // void AddData(void* buf, size_t length);
104 // ...
105 // };
106 //
107 //
108 // class DataLoader : public SupportsWeakPtr<DataLoader> {
109 // public:
110 // void GetData() {
111 // scoped_refptr<DataBuffer> buffer = new DataBuffer();
skyostilb1f02992015-06-19 17:22:54112 // target_thread_.task_runner()->PostTaskAndReply(
[email protected]6b28d942012-02-15 01:43:19113 // FROM_HERE,
114 // base::Bind(&DataBuffer::AddData, buffer),
115 // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer));
116 // }
117 //
118 // private:
119 // void OnDataReceived(scoped_refptr<DataBuffer> buffer) {
120 // // Do something with buffer.
121 // }
122 // };
123 //
124 //
125 // Things to notice:
126 // * Results of |task| are shared with |reply| by binding a shared argument
127 // (a DataBuffer instance).
128 // * The DataLoader object has no special thread safety.
129 // * The DataLoader object can be deleted while |task| is still running,
130 // and the reply will cancel itself safely because it is bound to a
131 // WeakPtr<>.
Brett Wilson8e88b312017-09-12 05:22:16132 bool PostTaskAndReply(const Location& from_here,
tzik6e427842017-04-05 10:13:21133 OnceClosure task,
134 OnceClosure reply);
[email protected]6b28d942012-02-15 01:43:19135
136 protected:
137 friend struct TaskRunnerTraits;
138
139 // Only the Windows debug build seems to need this: see
140 // http://crbug.com/112250.
141 friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>;
142
143 TaskRunner();
144 virtual ~TaskRunner();
145
146 // Called when this object should be destroyed. By default simply
147 // deletes |this|, but can be overridden to do something else, like
148 // delete on a certain thread.
149 virtual void OnDestruct() const;
150};
151
152struct BASE_EXPORT TaskRunnerTraits {
153 static void Destruct(const TaskRunner* task_runner);
154};
155
156} // namespace base
157
158#endif // BASE_TASK_RUNNER_H_