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 // ============================================================================ | 5 // ============================================================================ |
6 // **************************************************************************** | 6 // **************************************************************************** |
7 // * THIS HEADER IS DEPRECATED, SEE base/callback.h FOR NEW IMPLEMENTATION * | 7 // * THIS HEADER IS DEPRECATED, SEE base/callback.h FOR NEW IMPLEMENTATION * |
8 // **************************************************************************** | 8 // **************************************************************************** |
9 // ============================================================================ | 9 // ============================================================================ |
10 // ============================================================================ | 10 // ============================================================================ |
(...skipping 24 matching lines...) Expand all Loading... |
35 #include "base/callback.h" | 35 #include "base/callback.h" |
36 #include "base/debug/alias.h" | 36 #include "base/debug/alias.h" |
37 #include "base/memory/raw_scoped_refptr_mismatch_checker.h" | 37 #include "base/memory/raw_scoped_refptr_mismatch_checker.h" |
38 #include "base/memory/weak_ptr.h" | 38 #include "base/memory/weak_ptr.h" |
39 #include "base/tuple.h" | 39 #include "base/tuple.h" |
40 | 40 |
41 namespace base { | 41 namespace base { |
42 const size_t kDeadTask = 0xDEAD7A53; | 42 const size_t kDeadTask = 0xDEAD7A53; |
43 } | 43 } |
44 | 44 |
45 // Task ------------------------------------------------------------------------ | |
46 // | |
47 // A task is a generic runnable thingy, usually used for running code on a | |
48 // different thread or for scheduling future tasks off of the message loop. | |
49 | |
50 class BASE_EXPORT Task { | |
51 public: | |
52 Task(); | |
53 virtual ~Task(); | |
54 | |
55 // Tasks are automatically deleted after Run is called. | |
56 virtual void Run() = 0; | |
57 }; | |
58 | |
59 template<typename T> | 45 template<typename T> |
60 void DeletePointer(T* obj) { | 46 void DeletePointer(T* obj) { |
61 delete obj; | 47 delete obj; |
62 } | 48 } |
63 | 49 |
64 namespace base { | 50 namespace base { |
65 | 51 |
66 // ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the | 52 // ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the |
67 // Closure is executed and deleted no matter how the current scope exits. | 53 // Closure is executed and deleted no matter how the current scope exits. |
68 class BASE_EXPORT ScopedClosureRunner { | 54 class BASE_EXPORT ScopedClosureRunner { |
69 public: | 55 public: |
70 explicit ScopedClosureRunner(const Closure& closure); | 56 explicit ScopedClosureRunner(const Closure& closure); |
71 ~ScopedClosureRunner(); | 57 ~ScopedClosureRunner(); |
72 | 58 |
73 Closure Release(); | 59 Closure Release(); |
74 | 60 |
75 private: | 61 private: |
76 Closure closure_; | 62 Closure closure_; |
77 | 63 |
78 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedClosureRunner); | 64 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedClosureRunner); |
79 }; | 65 }; |
80 | 66 |
81 namespace subtle { | |
82 | |
83 // This class is meant for use in the implementation of MessageLoop classes | |
84 // such as MessageLoop, MessageLoopProxy, BrowserThread, and WorkerPool to | |
85 // implement the compatibility APIs while we are transitioning from Task to | |
86 // Callback. | |
87 // | |
88 // It should NOT be used anywhere else! | |
89 // | |
90 // In particular, notice that this is RefCounted instead of | |
91 // RefCountedThreadSafe. We rely on the fact that users of this class are | |
92 // careful to ensure that a lock is taken during transfer of ownership for | |
93 // objects from this class to ensure the refcount is not corrupted. | |
94 class TaskClosureAdapter : public RefCounted<TaskClosureAdapter> { | |
95 public: | |
96 explicit TaskClosureAdapter(Task* task); | |
97 | |
98 // |should_leak_task| points to a flag variable that can be used to determine | |
99 // if this class should leak the Task on destruction. This is important | |
100 // at MessageLoop shutdown since not all tasks can be safely deleted without | |
101 // running. See MessageLoop::DeletePendingTasks() for the exact behavior | |
102 // of when a Task should be deleted. It is subtle. | |
103 TaskClosureAdapter(Task* task, bool* should_leak_task); | |
104 | |
105 void Run(); | |
106 | |
107 private: | |
108 friend class base::RefCounted<TaskClosureAdapter>; | |
109 | |
110 ~TaskClosureAdapter(); | |
111 | |
112 Task* task_; | |
113 bool* should_leak_task_; | |
114 static bool kTaskLeakingDefault; | |
115 }; | |
116 | |
117 } // namespace subtle | |
118 | |
119 } // namespace base | 67 } // namespace base |
120 | 68 |
121 #endif // BASE_TASK_H_ | 69 #endif // BASE_TASK_H_ |
OLD | NEW |