OLD | NEW |
---|---|
(Empty) | |
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 #pragma once | |
8 | |
9 #include "base/base_export.h" | |
10 #include "base/basictypes.h" | |
11 #include "base/callback_forward.h" | |
12 #include "base/memory/ref_counted.h" | |
13 | |
14 namespace tracked_objects { | |
15 class Location; | |
16 } // namespace tracked_objects | |
17 | |
18 namespace base { | |
19 | |
20 struct TaskRunnerTraits; | |
21 | |
22 // An TaskRunner is an object that runs submitted tasks (in the form | |
23 // of Closure objects). The TaskRunner interface provides a way of | |
24 // decoupling task submission from the mechanics of how each task will | |
25 // be run. TaskRunner provides very weak guarantees as to how | |
26 // submitted tasks are run (or if they're run at all). In particular, | |
27 // it only guarantees: | |
28 // | |
29 // - Submitting a task will not run it synchronously. That is, no | |
30 // Post*Task method will call task.Run() directly. | |
31 // | |
32 // - Increasing the delay can only delay when the task gets run. | |
33 // That is, increasing the delay may not affect when the task gets | |
34 // run, or it could make it run later than it normally would, but | |
35 // it won't make it run earlier than it normally would. | |
36 // | |
37 // TaskRunner does not guarantee the order in which submitted tasks | |
38 // are run, whether tasks overlap, or whether they're run on a | |
39 // particular thread. Also it does not guarantee a memory model for | |
40 // shared data between tasks. (In other words, you should use your | |
41 // own synchronization/locking primitives if you need to share data | |
42 // between tasks.) | |
43 // | |
44 // Implementations of TaskRunner should be thread-safe in that all | |
45 // methods must be safe to call on any thread. Ownership semantics | |
46 // for TaskRunners are in general not clear, which is why the | |
47 // interface itself is RefCountedThreadSafe. | |
48 // | |
49 // Some theoretical implementations of TaskRunner: | |
50 // | |
51 // - An TaskRunner that uses a worker pool to run submitted tasks. | |
brettw
2012/02/09 00:26:04
Grammar: An -> A (same in the next paras).
akalin
2012/02/09 00:36:05
Done.
| |
52 // | |
53 // - An TaskRunner that, for each task, spawns a non-joinable thread | |
54 // to run that task and immediately quit. | |
55 // | |
56 // - An TaskRunner that stores the list of submitted tasks and has a | |
57 // method Run() that runs each runnable task in random order. | |
58 class BASE_EXPORT TaskRunner | |
59 : public RefCountedThreadSafe<TaskRunner, TaskRunnerTraits> { | |
60 public: | |
61 // TODO(akalin): Get rid of the boolean return value for the | |
62 // Post*Task methods. | |
63 | |
64 // Submits the given task for execution. Returns true if the task | |
65 // may be run at some point in the future, and false if the task | |
66 // definitely will not be run. | |
67 // | |
68 // TODO(akalin): Consider forwarding this to PostDelayedTask(_, _, | |
69 // 0). | |
70 virtual bool PostTask(const tracked_objects::Location& from_here, | |
71 const Closure& task) = 0; | |
72 | |
73 // Like PostTask, but tries to run the submitted task only after | |
74 // |delay_ms| has passed. PostDelayedTask with zero delay should be | |
75 // equivalent to PostTask. | |
76 // | |
77 // It is valid for an implementation to ignore |delay_ms|; that is, | |
78 // to have PostDelayedTask behave the same as PostTask. | |
79 // | |
80 // TODO(akalin): Make PostDelayedTask use TimeDelta instead. | |
81 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
82 const Closure& task, | |
83 int64 delay_ms) = 0; | |
84 | |
85 // Returns true if the current thread is a thread on which a task | |
86 // may be run, and false if no task will be run on the current | |
87 // thread. | |
88 // | |
89 // It is valid for an implementation to always return true, or in | |
90 // general to use 'true' as a default value. | |
91 virtual bool RunsTasksOnCurrentThread() const = 0; | |
92 | |
93 // Posts |task| on the current TaskRunner. On completion, |reply| | |
94 // is posted to the thread that called PostTaskAndReply(). Both | |
95 // |task| and |reply| are guaranteed to be deleted on the thread | |
96 // from which PostTaskAndReply() is invoked. This allows objects | |
97 // that must be deleted on the originating thread to be bound into | |
98 // the |task| and |reply| Closures. In particular, it can be useful | |
99 // to use WeakPtr<> in the |reply| Closure so that the reply | |
100 // operation can be canceled. See the following pseudo-code: | |
101 // | |
102 // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { | |
103 // public: | |
104 // // Called to add data into a buffer. | |
105 // void AddData(void* buf, size_t length); | |
106 // ... | |
107 // }; | |
108 // | |
109 // | |
110 // class DataLoader : public SupportsWeakPtr<DataLoader> { | |
111 // public: | |
112 // void GetData() { | |
113 // scoped_refptr<DataBuffer> buffer = new DataBuffer(); | |
114 // target_thread_.message_loop_proxy()->PostTaskAndReply( | |
115 // FROM_HERE, | |
116 // base::Bind(&DataBuffer::AddData, buffer), | |
117 // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer)); | |
118 // } | |
119 // | |
120 // private: | |
121 // void OnDataReceived(scoped_refptr<DataBuffer> buffer) { | |
122 // // Do something with buffer. | |
123 // } | |
124 // }; | |
125 // | |
126 // | |
127 // Things to notice: | |
128 // * Results of |task| are shared with |reply| by binding a shared argument | |
129 // (a DataBuffer instance). | |
130 // * The DataLoader object has no special thread safety. | |
131 // * The DataLoader object can be deleted while |task| is still running, | |
132 // and the reply will cancel itself safely because it is bound to a | |
133 // WeakPtr<>. | |
134 bool PostTaskAndReply(const tracked_objects::Location& from_here, | |
135 const Closure& task, | |
136 const Closure& reply); | |
137 | |
138 protected: | |
139 friend struct TaskRunnerTraits; | |
140 | |
141 // Only the Windows debug build seems to need this: see | |
142 // http://crbug.com/112250. | |
143 friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>; | |
144 | |
145 TaskRunner(); | |
146 virtual ~TaskRunner(); | |
147 | |
148 // Called when this object should be destroyed. By default simply | |
149 // deletes |this|, but can be overridden to do something else, like | |
150 // delete on a certain thread. | |
151 virtual void OnDestruct() const; | |
152 }; | |
153 | |
154 struct BASE_EXPORT TaskRunnerTraits { | |
155 static void Destruct(const TaskRunner* task_runner); | |
156 }; | |
157 | |
158 } // namespace base | |
159 | |
160 #endif // BASE_TASK_RUNNER_H_ | |
OLD | NEW |