1// 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_
7
8#include <stddef.h>
9
10#include "base/base_export.h"
11#include "base/callback.h"
12#include "base/location.h"
13#include "base/memory/ref_counted.h"
14#include "base/time/time.h"
15
16namespace 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).
64 bool PostTask(const Location& from_here, OnceClosure task);
65
66 // 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|.
69 virtual bool PostDelayedTask(const Location& from_here,
70 OnceClosure task,
71 base::TimeDelta delay) = 0;
72
73 // Returns true iff tasks posted to this TaskRunner are sequenced
74 // with this call.
75 //
76 // 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;
90
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();
112 // target_thread_.task_runner()->PostTaskAndReply(
113 // 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<>.
132 bool PostTaskAndReply(const Location& from_here,
133 OnceClosure task,
134 OnceClosure reply);
135
136 protected:
137 friend struct TaskRunnerTraits;
138
139 TaskRunner();
140 virtual ~TaskRunner();
141
142 // Called when this object should be destroyed. By default simply
143 // deletes |this|, but can be overridden to do something else, like
144 // delete on a certain thread.
145 virtual void OnDestruct() const;
146};
147
148struct BASE_EXPORT TaskRunnerTraits {
149 static void Destruct(const TaskRunner* task_runner);
150};
151
152} // namespace base
153
154#endif // BASE_TASK_RUNNER_H_
155