OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TASK_RUNNER_H_ | 5 #ifndef BASE_TASK_RUNNER_H_ |
6 #define BASE_TASK_RUNNER_H_ | 6 #define BASE_TASK_RUNNER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 // - A TaskRunner that stores the list of posted tasks and has a | 54 // - A TaskRunner that stores the list of posted tasks and has a |
55 // method Run() that runs each runnable task in random order. | 55 // method Run() that runs each runnable task in random order. |
56 class BASE_EXPORT TaskRunner | 56 class BASE_EXPORT TaskRunner |
57 : public RefCountedThreadSafe<TaskRunner, TaskRunnerTraits> { | 57 : public RefCountedThreadSafe<TaskRunner, TaskRunnerTraits> { |
58 public: | 58 public: |
59 // Posts the given task to be run. Returns true if the task may be | 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 | 60 // run at some point in the future, and false if the task definitely |
61 // will not be run. | 61 // will not be run. |
62 // | 62 // |
63 // Equivalent to PostDelayedTask(from_here, task, 0). | 63 // Equivalent to PostDelayedTask(from_here, task, 0). |
64 bool PostTask(const tracked_objects::Location& from_here, Closure task); | 64 bool PostTask(const tracked_objects::Location& from_here, OnceClosure task); |
65 | 65 |
66 // Like PostTask, but tries to run the posted task only after | 66 // Like PostTask, but tries to run the posted task only after |
67 // |delay_ms| has passed. | 67 // |delay_ms| has passed. |
68 // | 68 // |
69 // It is valid for an implementation to ignore |delay_ms|; that is, | 69 // It is valid for an implementation to ignore |delay_ms|; that is, |
70 // to have PostDelayedTask behave the same as PostTask. | 70 // to have PostDelayedTask behave the same as PostTask. |
71 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 71 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
72 Closure task, | 72 OnceClosure task, |
73 base::TimeDelta delay) = 0; | 73 base::TimeDelta delay) = 0; |
74 | 74 |
75 // Returns true if the current thread is a thread on which a task | 75 // Returns true if the current thread is a thread on which a task |
76 // may be run, and false if no task will be run on the current | 76 // may be run, and false if no task will be run on the current |
77 // thread. | 77 // thread. |
78 // | 78 // |
79 // It is valid for an implementation to always return true, or in | 79 // It is valid for an implementation to always return true, or in |
80 // general to use 'true' as a default value. | 80 // general to use 'true' as a default value. |
81 virtual bool RunsTasksOnCurrentThread() const = 0; | 81 virtual bool RunsTasksOnCurrentThread() const = 0; |
82 | 82 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // | 115 // |
116 // | 116 // |
117 // Things to notice: | 117 // Things to notice: |
118 // * Results of |task| are shared with |reply| by binding a shared argument | 118 // * Results of |task| are shared with |reply| by binding a shared argument |
119 // (a DataBuffer instance). | 119 // (a DataBuffer instance). |
120 // * The DataLoader object has no special thread safety. | 120 // * The DataLoader object has no special thread safety. |
121 // * The DataLoader object can be deleted while |task| is still running, | 121 // * The DataLoader object can be deleted while |task| is still running, |
122 // and the reply will cancel itself safely because it is bound to a | 122 // and the reply will cancel itself safely because it is bound to a |
123 // WeakPtr<>. | 123 // WeakPtr<>. |
124 bool PostTaskAndReply(const tracked_objects::Location& from_here, | 124 bool PostTaskAndReply(const tracked_objects::Location& from_here, |
125 Closure task, | 125 OnceClosure task, |
126 Closure reply); | 126 OnceClosure reply); |
127 | 127 |
128 protected: | 128 protected: |
129 friend struct TaskRunnerTraits; | 129 friend struct TaskRunnerTraits; |
130 | 130 |
131 // Only the Windows debug build seems to need this: see | 131 // Only the Windows debug build seems to need this: see |
132 // http://crbug.com/112250. | 132 // http://crbug.com/112250. |
133 friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>; | 133 friend class RefCountedThreadSafe<TaskRunner, TaskRunnerTraits>; |
134 | 134 |
135 TaskRunner(); | 135 TaskRunner(); |
136 virtual ~TaskRunner(); | 136 virtual ~TaskRunner(); |
137 | 137 |
138 // Called when this object should be destroyed. By default simply | 138 // Called when this object should be destroyed. By default simply |
139 // deletes |this|, but can be overridden to do something else, like | 139 // deletes |this|, but can be overridden to do something else, like |
140 // delete on a certain thread. | 140 // delete on a certain thread. |
141 virtual void OnDestruct() const; | 141 virtual void OnDestruct() const; |
142 }; | 142 }; |
143 | 143 |
144 struct BASE_EXPORT TaskRunnerTraits { | 144 struct BASE_EXPORT TaskRunnerTraits { |
145 static void Destruct(const TaskRunner* task_runner); | 145 static void Destruct(const TaskRunner* task_runner); |
146 }; | 146 }; |
147 | 147 |
148 } // namespace base | 148 } // namespace base |
149 | 149 |
150 #endif // BASE_TASK_RUNNER_H_ | 150 #endif // BASE_TASK_RUNNER_H_ |
OLD | NEW |