OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_H__ | 5 #ifndef BASE_TASK_H__ |
6 #define BASE_TASK_H__ | 6 #define BASE_TASK_H__ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/non_thread_safe.h" | 12 #include "base/non_thread_safe.h" |
13 #include "base/revocable_store.h" | 13 #include "base/revocable_store.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "base/tracked.h" | 15 #include "base/tracked.h" |
16 #include "base/tuple.h" | 16 #include "base/tuple.h" |
17 | 17 |
18 namespace base { | |
19 class TimerManager; | |
20 } | |
21 | |
22 //------------------------------------------------------------------------------ | |
23 // Base class of Task, where we store info to help MessageLoop handle PostTask() | |
24 // elements of Task processing. | |
25 | |
26 class Task; | |
27 | |
28 // TODO(darin): Eliminate TaskBase. This data is ML specific and is not | |
29 // applicable to other uses of Task. | |
30 class TaskBase : public tracked_objects::Tracked { | |
31 public: | |
32 TaskBase() { Reset(); } | |
33 virtual ~TaskBase() {} | |
34 | |
35 // Use this method to adjust the priority given to a task by MessageLoop. | |
36 void set_priority(int priority) { priority_ = priority; } | |
37 int priority() const { return priority_; } | |
38 | |
39 // Change whether this task will run in nested message loops. | |
40 void set_nestable(bool nestable) { nestable_ = nestable; } | |
41 bool nestable() { return nestable_; } | |
42 | |
43 // Used to manage a linked-list of tasks. | |
44 Task* next_task() const { return next_task_; } | |
45 void set_next_task(Task* next) { next_task_ = next; } | |
46 | |
47 protected: | |
48 // If a derived class wishes to re-use this instance, then it should override | |
49 // this method. This method is called by MessageLoop after processing a task | |
50 // that was submitted to PostTask() or PostDelayedTask(). As seen, by default | |
51 // it deletes the task, but the derived class can change this behaviour and | |
52 // recycle (re-use) it. Be sure to call Reset() if you recycle it! | |
53 virtual void RecycleOrDelete() { delete this; } | |
54 | |
55 // Call this method if you are trying to recycle a Task. Note that only | |
56 // derived classes should attempt this feat, as a replacement for creating a | |
57 // new instance. | |
58 void Reset() { | |
59 priority_ = 0; | |
60 delayed_run_time_ = Time(); | |
61 next_task_ = NULL; | |
62 nestable_ = true; | |
63 owned_by_message_loop_ = false; | |
64 } | |
65 | |
66 private: | |
67 friend class base::TimerManager; // To check is_owned_by_message_loop(). | |
68 friend class MessageLoop; // To maintain posted_task_delay(). | |
69 | |
70 // Priority for execution by MessageLoop. 0 is default. Higher means run | |
71 // sooner, and lower (including negative) means run less soon. | |
72 int priority_; | |
73 | |
74 // The time when a delayed task should be run. | |
75 Time delayed_run_time_; | |
76 | |
77 // When tasks are collected into a queue by MessageLoop, this member is used | |
78 // to form a null terminated list. | |
79 Task* next_task_; | |
80 | |
81 // A nestable task will run in nested message loops, otherwise it will run | |
82 // only in the top level message loop. | |
83 bool nestable_; | |
84 | |
85 // True if this Task is owned by the message loop. | |
86 bool owned_by_message_loop_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(TaskBase); | |
89 }; | |
90 | |
91 | |
92 // Task ------------------------------------------------------------------------ | 18 // Task ------------------------------------------------------------------------ |
93 // | 19 // |
94 // A task is a generic runnable thingy, usually used for running code on a | 20 // A task is a generic runnable thingy, usually used for running code on a |
95 // different thread or for scheduling future tasks off of the message loop. | 21 // different thread or for scheduling future tasks off of the message loop. |
96 | 22 |
97 class Task : public TaskBase { | 23 class Task : public tracked_objects::Tracked { |
98 public: | 24 public: |
99 Task() {} | 25 Task() {} |
100 virtual ~Task() {} | 26 virtual ~Task() {} |
101 | 27 |
102 // Tasks are automatically deleted after Run is called. | 28 // Tasks are automatically deleted after Run is called. |
103 virtual void Run() = 0; | 29 virtual void Run() = 0; |
104 }; | 30 }; |
105 | 31 |
106 class CancelableTask : public Task { | 32 class CancelableTask : public Task { |
107 public: | 33 public: |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 DISALLOW_EVIL_CONSTRUCTORS(ScopedRunnableMethodFactory); | 215 DISALLOW_EVIL_CONSTRUCTORS(ScopedRunnableMethodFactory); |
290 }; | 216 }; |
291 | 217 |
292 // General task implementations ------------------------------------------------ | 218 // General task implementations ------------------------------------------------ |
293 | 219 |
294 // Task to delete an object | 220 // Task to delete an object |
295 template<class T> | 221 template<class T> |
296 class DeleteTask : public CancelableTask { | 222 class DeleteTask : public CancelableTask { |
297 public: | 223 public: |
298 explicit DeleteTask(T* obj) : obj_(obj) { | 224 explicit DeleteTask(T* obj) : obj_(obj) { |
299 set_nestable(false); | |
300 } | 225 } |
301 virtual void Run() { | 226 virtual void Run() { |
302 delete obj_; | 227 delete obj_; |
303 } | 228 } |
304 virtual void Cancel() { | 229 virtual void Cancel() { |
305 obj_ = NULL; | 230 obj_ = NULL; |
306 } | 231 } |
307 private: | 232 private: |
308 T* obj_; | 233 T* obj_; |
309 }; | 234 }; |
310 | 235 |
311 // Task to Release() an object | 236 // Task to Release() an object |
312 template<class T> | 237 template<class T> |
313 class ReleaseTask : public CancelableTask { | 238 class ReleaseTask : public CancelableTask { |
314 public: | 239 public: |
315 explicit ReleaseTask(T* obj) : obj_(obj) { | 240 explicit ReleaseTask(T* obj) : obj_(obj) { |
316 set_nestable(false); | |
317 } | 241 } |
318 virtual void Run() { | 242 virtual void Run() { |
319 if (obj_) | 243 if (obj_) |
320 obj_->Release(); | 244 obj_->Release(); |
321 } | 245 } |
322 virtual void Cancel() { | 246 virtual void Cancel() { |
323 obj_ = NULL; | 247 obj_ = NULL; |
324 } | 248 } |
325 private: | 249 private: |
326 T* obj_; | 250 T* obj_; |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 typename Arg3, typename Arg4, typename Arg5> | 620 typename Arg3, typename Arg4, typename Arg5> |
697 typename Callback5<Arg1, Arg2, Arg3, Arg4, Arg5>::Type* NewCallback( | 621 typename Callback5<Arg1, Arg2, Arg3, Arg4, Arg5>::Type* NewCallback( |
698 T* object, | 622 T* object, |
699 void (T::*method)(Arg1, Arg2, Arg3, Arg4, Arg5)) { | 623 void (T::*method)(Arg1, Arg2, Arg3, Arg4, Arg5)) { |
700 return new CallbackImpl<T, void (T::*)(Arg1, Arg2, Arg3, Arg4, Arg5), | 624 return new CallbackImpl<T, void (T::*)(Arg1, Arg2, Arg3, Arg4, Arg5), |
701 Tuple5<Arg1, Arg2, Arg3, Arg4, Arg5> >(object, method); | 625 Tuple5<Arg1, Arg2, Arg3, Arg4, Arg5> >(object, method); |
702 } | 626 } |
703 | 627 |
704 #endif // BASE_TASK_H__ | 628 #endif // BASE_TASK_H__ |
705 | 629 |
OLD | NEW |