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_MESSAGE_LOOP_PROXY_H_ | 5 #ifndef BASE_MESSAGE_LOOP_PROXY_H_ |
6 #define BASE_MESSAGE_LOOP_PROXY_H_ | 6 #define BASE_MESSAGE_LOOP_PROXY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/base_export.h" | 9 #include "base/base_export.h" |
10 #include "base/basictypes.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/callback_forward.h" | |
12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
13 #include "base/message_loop_helpers.h" | 12 #include "base/single_thread_task_runner.h" |
14 | |
15 namespace tracked_objects { | |
16 class Location; | |
17 } // namespace tracked_objects | |
18 | 13 |
19 namespace base { | 14 namespace base { |
20 | 15 |
21 struct MessageLoopProxyTraits; | |
22 | |
23 // This class provides a thread-safe refcounted interface to the Post* methods | 16 // This class provides a thread-safe refcounted interface to the Post* methods |
24 // of a message loop. This class can outlive the target message loop. | 17 // of a message loop. This class can outlive the target message loop. |
25 // MessageLoopProxy objects are constructed automatically for all MessageLoops. | 18 // MessageLoopProxy objects are constructed automatically for all MessageLoops. |
26 // So, to access them, you can use any of the following: | 19 // So, to access them, you can use any of the following: |
27 // Thread::message_loop_proxy() | 20 // Thread::message_loop_proxy() |
28 // MessageLoop::current()->message_loop_proxy() | 21 // MessageLoop::current()->message_loop_proxy() |
29 // MessageLoopProxy::current() | 22 // MessageLoopProxy::current() |
30 class BASE_EXPORT MessageLoopProxy | 23 // |
31 : public base::RefCountedThreadSafe<MessageLoopProxy, | 24 // TODO(akalin): Now that we have the *TaskRunner interfaces, we can |
32 MessageLoopProxyTraits> { | 25 // merge this with MessageLoopProxyImpl. |
| 26 class BASE_EXPORT MessageLoopProxy : public SingleThreadTaskRunner { |
33 public: | 27 public: |
34 // These methods are the same as in message_loop.h, but are guaranteed to | |
35 // either post the Task to the MessageLoop (if it's still alive), or the task | |
36 // is discarded. | |
37 // They return true iff the thread existed and the task was posted. Note that | |
38 // even if the task is posted, there's no guarantee that it will run; for | |
39 // example the target loop may already be quitting, or in the case of a | |
40 // delayed task a Quit message may preempt it in the message loop queue. | |
41 // Conversely, a return value of false is a guarantee the task will not run. | |
42 virtual bool PostTask(const tracked_objects::Location& from_here, | |
43 const base::Closure& task) = 0; | |
44 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
45 const base::Closure& task, | |
46 int64 delay_ms) = 0; | |
47 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, | |
48 const base::Closure& task) = 0; | |
49 virtual bool PostNonNestableDelayedTask( | |
50 const tracked_objects::Location& from_here, | |
51 const base::Closure& task, | |
52 int64 delay_ms) = 0; | |
53 | |
54 // A method which checks if the caller is currently running in the thread that | |
55 // this proxy represents. | |
56 virtual bool BelongsToCurrentThread() = 0; | |
57 | |
58 // Executes |task| on the given MessageLoopProxy. On completion, |reply| | |
59 // is passed back to the MessageLoopProxy for the thread that called | |
60 // PostTaskAndReply(). Both |task| and |reply| are guaranteed to be deleted | |
61 // on the thread from which PostTaskAndReply() is invoked. This allows | |
62 // objects that must be deleted on the originating thread to be bound into the | |
63 // |task| and |reply| Closures. In particular, it can be useful to use | |
64 // WeakPtr<> in the |reply| Closure so that the reply operation can be | |
65 // canceled. See the following pseudo-code: | |
66 // | |
67 // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { | |
68 // public: | |
69 // // Called to add data into a buffer. | |
70 // void AddData(void* buf, size_t length); | |
71 // ... | |
72 // }; | |
73 // | |
74 // | |
75 // class DataLoader : public SupportsWeakPtr<DataLoader> { | |
76 // public: | |
77 // void GetData() { | |
78 // scoped_refptr<DataBuffer> buffer = new DataBuffer(); | |
79 // target_thread_.message_loop_proxy()->PostTaskAndReply( | |
80 // FROM_HERE, | |
81 // base::Bind(&DataBuffer::AddData, buffer), | |
82 // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer)); | |
83 // } | |
84 // | |
85 // private: | |
86 // void OnDataReceived(scoped_refptr<DataBuffer> buffer) { | |
87 // // Do something with buffer. | |
88 // } | |
89 // }; | |
90 // | |
91 // | |
92 // Things to notice: | |
93 // * Results of |task| are shared with |reply| by binding a shared argument | |
94 // (a DataBuffer instance). | |
95 // * The DataLoader object has no special thread safety. | |
96 // * The DataLoader object can be deleted while |task| is still running, | |
97 // and the reply will cancel itself safely because it is bound to a | |
98 // WeakPtr<>. | |
99 bool PostTaskAndReply(const tracked_objects::Location& from_here, | |
100 const Closure& task, | |
101 const Closure& reply); | |
102 | |
103 template <class T> | |
104 bool DeleteSoon(const tracked_objects::Location& from_here, | |
105 const T* object) { | |
106 return subtle::DeleteHelperInternal<T, bool>::DeleteOnMessageLoop( | |
107 this, from_here, object); | |
108 } | |
109 template <class T> | |
110 bool ReleaseSoon(const tracked_objects::Location& from_here, | |
111 T* object) { | |
112 return subtle::ReleaseHelperInternal<T, bool>::ReleaseOnMessageLoop( | |
113 this, from_here, object); | |
114 } | |
115 | |
116 // Gets the MessageLoopProxy for the current message loop, creating one if | 28 // Gets the MessageLoopProxy for the current message loop, creating one if |
117 // needed. | 29 // needed. |
118 static scoped_refptr<MessageLoopProxy> current(); | 30 static scoped_refptr<MessageLoopProxy> current(); |
119 | 31 |
120 protected: | 32 protected: |
121 friend class RefCountedThreadSafe<MessageLoopProxy, MessageLoopProxyTraits>; | |
122 friend struct MessageLoopProxyTraits; | |
123 | |
124 MessageLoopProxy(); | 33 MessageLoopProxy(); |
125 virtual ~MessageLoopProxy(); | 34 virtual ~MessageLoopProxy(); |
126 | |
127 // Called when the proxy is about to be deleted. Subclasses can override this | |
128 // to provide deletion on specific threads. | |
129 virtual void OnDestruct() const; | |
130 | |
131 private: | |
132 template <class T, class R> friend class subtle::DeleteHelperInternal; | |
133 template <class T, class R> friend class subtle::ReleaseHelperInternal; | |
134 bool DeleteSoonInternal(const tracked_objects::Location& from_here, | |
135 void(*deleter)(const void*), | |
136 const void* object); | |
137 bool ReleaseSoonInternal(const tracked_objects::Location& from_here, | |
138 void(*releaser)(const void*), | |
139 const void* object); | |
140 }; | |
141 | |
142 struct MessageLoopProxyTraits { | |
143 static void Destruct(const MessageLoopProxy* proxy) { | |
144 proxy->OnDestruct(); | |
145 } | |
146 }; | 35 }; |
147 | 36 |
148 } // namespace base | 37 } // namespace base |
149 | 38 |
150 #endif // BASE_MESSAGE_LOOP_PROXY_H_ | 39 #endif // BASE_MESSAGE_LOOP_PROXY_H_ |
OLD | NEW |