[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 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_ |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 10 | #include "base/base_export.h" |
tzik | 0352751 | 2017-02-08 12:29:47 | [diff] [blame] | 11 | #include "base/callback.h" |
fdoray | 1d08c58 | 2016-06-09 22:55:06 | [diff] [blame] | 12 | #include "base/location.h" |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 8f9a3a5 | 2013-06-28 15:14:18 | [diff] [blame] | 14 | #include "base/time/time.h" |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 15 | |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 16 | namespace base { |
| 17 | |
| 18 | struct 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. |
| 56 | class 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 Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 64 | bool PostTask(const Location& from_here, OnceClosure task); |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 65 | |
Wez | da38b06 | 2017-07-18 17:19:46 | [diff] [blame] | 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|. |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 69 | virtual bool PostDelayedTask(const Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 70 | OnceClosure task, |
[email protected] | 17dc674 | 2012-02-26 08:17:37 | [diff] [blame] | 71 | base::TimeDelta delay) = 0; |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 72 | |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 73 | // Returns true iff tasks posted to this TaskRunner are sequenced |
| 74 | // with this call. |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 75 | // |
peary2 | 3322df6 | 2017-05-09 03:55:48 | [diff] [blame] | 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; |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 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(); |
skyostil | b1f0299 | 2015-06-19 17:22:54 | [diff] [blame] | 112 | // target_thread_.task_runner()->PostTaskAndReply( |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 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<>. |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 132 | bool PostTaskAndReply(const Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 133 | OnceClosure task, |
| 134 | OnceClosure reply); |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 135 | |
| 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 | |
| 152 | struct BASE_EXPORT TaskRunnerTraits { |
| 153 | static void Destruct(const TaskRunner* task_runner); |
| 154 | }; |
| 155 | |
| 156 | } // namespace base |
| 157 | |
| 158 | #endif // BASE_TASK_RUNNER_H_ |