OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 MOJO_EDK_SYSTEM_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_DISPATCHER_H_ |
6 #define MOJO_EDK_SYSTEM_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_DISPATCHER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <ostream> | 11 #include <ostream> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
| 14 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
17 #include "mojo/edk/embedder/platform_handle_vector.h" | 18 #include "mojo/edk/embedder/platform_handle.h" |
| 19 #include "mojo/edk/embedder/platform_shared_buffer.h" |
18 #include "mojo/edk/system/handle_signals_state.h" | 20 #include "mojo/edk/system/handle_signals_state.h" |
| 21 #include "mojo/edk/system/ports/name.h" |
19 #include "mojo/edk/system/system_impl_export.h" | 22 #include "mojo/edk/system/system_impl_export.h" |
20 #include "mojo/public/c/system/buffer.h" | 23 #include "mojo/public/c/system/buffer.h" |
21 #include "mojo/public/c/system/data_pipe.h" | 24 #include "mojo/public/c/system/data_pipe.h" |
22 #include "mojo/public/c/system/message_pipe.h" | 25 #include "mojo/public/c/system/message_pipe.h" |
23 #include "mojo/public/c/system/types.h" | 26 #include "mojo/public/c/system/types.h" |
24 #include "mojo/public/cpp/system/macros.h" | |
25 | 27 |
26 namespace mojo { | 28 namespace mojo { |
27 namespace edk { | 29 namespace edk { |
28 | 30 |
29 class Core; | 31 class Awakable; |
30 class Dispatcher; | 32 class Dispatcher; |
31 class DispatcherTransport; | |
32 class HandleTable; | |
33 class LocalMessagePipeEndpoint; | |
34 class PlatformSharedBufferMapping; | |
35 class ProxyMessagePipeEndpoint; | |
36 class TransportData; | |
37 class Awakable; | |
38 | 33 |
39 using DispatcherVector = std::vector<scoped_refptr<Dispatcher>>; | 34 using DispatcherVector = std::vector<scoped_refptr<Dispatcher>>; |
40 | 35 |
41 namespace test { | 36 // A |Dispatcher| implements Mojo EDK calls that are associated with a |
42 | 37 // particular MojoHandle, with the exception of MojoWait and MojoWaitMany ( |
43 // Test helper. We need to declare it here so we can friend it. | 38 // which are implemented directly in Core.). |
44 MOJO_SYSTEM_IMPL_EXPORT DispatcherTransport | |
45 DispatcherTryStartTransport(Dispatcher* dispatcher); | |
46 | |
47 } // namespace test | |
48 | |
49 // A |Dispatcher| implements Mojo primitives that are "attached" to a particular | |
50 // handle. This includes most (all?) primitives except for |MojoWait...()|. This | |
51 // object is thread-safe, with its state being protected by a single lock | |
52 // |lock_|, which is also made available to implementation subclasses (via the | |
53 // |lock()| method). | |
54 class MOJO_SYSTEM_IMPL_EXPORT Dispatcher | 39 class MOJO_SYSTEM_IMPL_EXPORT Dispatcher |
55 : public base::RefCountedThreadSafe<Dispatcher> { | 40 : public base::RefCountedThreadSafe<Dispatcher> { |
56 public: | 41 public: |
| 42 struct DispatcherInTransit { |
| 43 DispatcherInTransit(); |
| 44 ~DispatcherInTransit(); |
| 45 |
| 46 scoped_refptr<Dispatcher> dispatcher; |
| 47 MojoHandle local_handle; |
| 48 }; |
| 49 |
57 enum class Type { | 50 enum class Type { |
58 UNKNOWN = 0, | 51 UNKNOWN = 0, |
59 MESSAGE_PIPE, | 52 MESSAGE_PIPE, |
60 DATA_PIPE_PRODUCER, | 53 DATA_PIPE_PRODUCER, |
61 DATA_PIPE_CONSUMER, | 54 DATA_PIPE_CONSUMER, |
62 SHARED_BUFFER, | 55 SHARED_BUFFER, |
63 WAIT_SET, | 56 WAIT_SET, |
64 | 57 |
65 // "Private" types (not exposed via the public interface): | 58 // "Private" types (not exposed via the public interface): |
66 PLATFORM_HANDLE = -1 | 59 PLATFORM_HANDLE = -1, |
67 }; | 60 }; |
| 61 |
| 62 // All Dispatchers must minimally implement these methods. |
| 63 |
68 virtual Type GetType() const = 0; | 64 virtual Type GetType() const = 0; |
| 65 virtual MojoResult Close() = 0; |
69 | 66 |
70 // These methods implement the various primitives named |Mojo...()|. These | 67 ///////////// Message pipe API ///////////// |
71 // take |lock_| and handle races with |Close()|. Then they call out to | |
72 // subclasses' |...ImplNoLock()| methods (still under |lock_|), which actually | |
73 // implement the primitives. | |
74 // NOTE(vtl): This puts a big lock around each dispatcher (i.e., handle), and | |
75 // prevents the various |...ImplNoLock()|s from releasing the lock as soon as | |
76 // possible. If this becomes an issue, we can rethink this. | |
77 MojoResult Close(); | |
78 | 68 |
79 // |transports| may be non-null if and only if there are handles to be | 69 virtual MojoResult WriteMessage(const void* bytes, |
80 // written; not that |this| must not be in |transports|. On success, all the | 70 uint32_t num_bytes, |
81 // dispatchers in |transports| must have been moved to a closed state; on | 71 const DispatcherInTransit* dispatchers, |
82 // failure, they should remain in their original state. | 72 uint32_t num_dispatchers, |
83 MojoResult WriteMessage(const void* bytes, | 73 MojoWriteMessageFlags flags); |
84 uint32_t num_bytes, | 74 |
85 std::vector<DispatcherTransport>* transports, | 75 virtual MojoResult ReadMessage(void* bytes, |
86 MojoWriteMessageFlags flags); | 76 uint32_t* num_bytes, |
87 // |dispatchers| must be non-null but empty, if |num_dispatchers| is non-null | 77 MojoHandle* handles, |
88 // and nonzero. On success, it will be set to the dispatchers to be received | 78 uint32_t* num_handles, |
89 // (and assigned handles) as part of the message. | 79 MojoReadMessageFlags flags); |
90 MojoResult ReadMessage(void* bytes, | 80 |
91 uint32_t* num_bytes, | 81 ///////////// Shared buffer API ///////////// |
92 DispatcherVector* dispatchers, | 82 |
93 uint32_t* num_dispatchers, | |
94 MojoReadMessageFlags flags); | |
95 MojoResult WriteData(const void* elements, | |
96 uint32_t* elements_num_bytes, | |
97 MojoWriteDataFlags flags); | |
98 MojoResult BeginWriteData(void** buffer, | |
99 uint32_t* buffer_num_bytes, | |
100 MojoWriteDataFlags flags); | |
101 MojoResult EndWriteData(uint32_t num_bytes_written); | |
102 MojoResult ReadData(void* elements, | |
103 uint32_t* num_bytes, | |
104 MojoReadDataFlags flags); | |
105 MojoResult BeginReadData(const void** buffer, | |
106 uint32_t* buffer_num_bytes, | |
107 MojoReadDataFlags flags); | |
108 MojoResult EndReadData(uint32_t num_bytes_read); | |
109 // |options| may be null. |new_dispatcher| must not be null, but | 83 // |options| may be null. |new_dispatcher| must not be null, but |
110 // |*new_dispatcher| should be null (and will contain the dispatcher for the | 84 // |*new_dispatcher| should be null (and will contain the dispatcher for the |
111 // new handle on success). | 85 // new handle on success). |
112 MojoResult DuplicateBufferHandle( | 86 virtual MojoResult DuplicateBufferHandle( |
113 const MojoDuplicateBufferHandleOptions* options, | 87 const MojoDuplicateBufferHandleOptions* options, |
114 scoped_refptr<Dispatcher>* new_dispatcher); | 88 scoped_refptr<Dispatcher>* new_dispatcher); |
115 MojoResult MapBuffer( | 89 |
| 90 virtual MojoResult MapBuffer( |
116 uint64_t offset, | 91 uint64_t offset, |
117 uint64_t num_bytes, | 92 uint64_t num_bytes, |
118 MojoMapBufferFlags flags, | 93 MojoMapBufferFlags flags, |
119 scoped_ptr<PlatformSharedBufferMapping>* mapping); | 94 scoped_ptr<PlatformSharedBufferMapping>* mapping); |
120 | 95 |
| 96 ///////////// Data pipe consumer API ///////////// |
| 97 |
| 98 virtual MojoResult ReadData(void* elements, |
| 99 uint32_t* num_bytes, |
| 100 MojoReadDataFlags flags); |
| 101 |
| 102 virtual MojoResult BeginReadData(const void** buffer, |
| 103 uint32_t* buffer_num_bytes, |
| 104 MojoReadDataFlags flags); |
| 105 |
| 106 virtual MojoResult EndReadData(uint32_t num_bytes_read); |
| 107 |
| 108 ///////////// Data pipe producer API ///////////// |
| 109 |
| 110 virtual MojoResult WriteData(const void* elements, |
| 111 uint32_t* num_bytes, |
| 112 MojoWriteDataFlags flags); |
| 113 |
| 114 virtual MojoResult BeginWriteData(void** buffer, |
| 115 uint32_t* buffer_num_bytes, |
| 116 MojoWriteDataFlags flags); |
| 117 |
| 118 virtual MojoResult EndWriteData(uint32_t num_bytes_written); |
| 119 |
| 120 ///////////// Wait set API ///////////// |
| 121 |
| 122 // Adds a dispatcher to wait on. When the dispatcher satisfies |signals|, it |
| 123 // will be returned in the next call to |GetReadyDispatchers()|. If |
| 124 // |dispatcher| has been added, it must be removed before adding again, |
| 125 // otherwise |MOJO_RESULT_ALREADY_EXISTS| will be returned. |
| 126 virtual MojoResult AddWaitingDispatcher( |
| 127 const scoped_refptr<Dispatcher>& dispatcher, |
| 128 MojoHandleSignals signals, |
| 129 uintptr_t context); |
| 130 |
| 131 // Removes a dispatcher to wait on. If |dispatcher| has not been added, |
| 132 // |MOJO_RESULT_NOT_FOUND| will be returned. |
| 133 virtual MojoResult RemoveWaitingDispatcher( |
| 134 const scoped_refptr<Dispatcher>& dispatcher); |
| 135 |
| 136 // Returns a set of ready dispatchers. |*count| is the maximum number of |
| 137 // dispatchers to return, and will contain the number of dispatchers returned |
| 138 // in |dispatchers| on completion. |
| 139 virtual MojoResult GetReadyDispatchers(uint32_t* count, |
| 140 DispatcherVector* dispatchers, |
| 141 MojoResult* results, |
| 142 uintptr_t* contexts); |
| 143 |
| 144 ///////////// General-purpose API for all handle types ///////// |
| 145 |
121 // Gets the current handle signals state. (The default implementation simply | 146 // Gets the current handle signals state. (The default implementation simply |
122 // returns a default-constructed |HandleSignalsState|, i.e., no signals | 147 // returns a default-constructed |HandleSignalsState|, i.e., no signals |
123 // satisfied or satisfiable.) Note: The state is subject to change from other | 148 // satisfied or satisfiable.) Note: The state is subject to change from other |
124 // threads. | 149 // threads. |
125 HandleSignalsState GetHandleSignalsState() const; | 150 virtual HandleSignalsState GetHandleSignalsState() const; |
126 | 151 |
127 // Adds an awakable to this dispatcher, which will be woken up when this | 152 // Adds an awakable to this dispatcher, which will be woken up when this |
128 // object changes state to satisfy |signals| with context |context|. It will | 153 // object changes state to satisfy |signals| with context |context|. It will |
129 // also be woken up when it becomes impossible for the object to ever satisfy | 154 // also be woken up when it becomes impossible for the object to ever satisfy |
130 // |signals| with a suitable error status. | 155 // |signals| with a suitable error status. |
131 // | 156 // |
132 // If |signals_state| is non-null, on *failure* |*signals_state| will be set | 157 // If |signals_state| is non-null, on *failure* |*signals_state| will be set |
133 // to the current handle signals state (on success, it is left untouched). | 158 // to the current handle signals state (on success, it is left untouched). |
134 // | 159 // |
135 // Returns: | 160 // Returns: |
136 // - |MOJO_RESULT_OK| if the awakable was added; | 161 // - |MOJO_RESULT_OK| if the awakable was added; |
137 // - |MOJO_RESULT_ALREADY_EXISTS| if |signals| is already satisfied; | 162 // - |MOJO_RESULT_ALREADY_EXISTS| if |signals| is already satisfied; |
138 // - |MOJO_RESULT_INVALID_ARGUMENT| if the dispatcher has been closed; and | 163 // - |MOJO_RESULT_INVALID_ARGUMENT| if the dispatcher has been closed; and |
139 // - |MOJO_RESULT_FAILED_PRECONDITION| if it is not (or no longer) possible | 164 // - |MOJO_RESULT_FAILED_PRECONDITION| if it is not (or no longer) possible |
140 // that |signals| will ever be satisfied. | 165 // that |signals| will ever be satisfied. |
141 MojoResult AddAwakable(Awakable* awakable, | 166 virtual MojoResult AddAwakable(Awakable* awakable, |
142 MojoHandleSignals signals, | 167 MojoHandleSignals signals, |
143 uintptr_t context, | 168 uintptr_t context, |
144 HandleSignalsState* signals_state); | 169 HandleSignalsState* signals_state); |
| 170 |
145 // Removes an awakable from this dispatcher. (It is valid to call this | 171 // Removes an awakable from this dispatcher. (It is valid to call this |
146 // multiple times for the same |awakable| on the same object, so long as | 172 // multiple times for the same |awakable| on the same object, so long as |
147 // |AddAwakable()| was called at most once.) If |signals_state| is non-null, | 173 // |AddAwakable()| was called at most once.) If |signals_state| is non-null, |
148 // |*signals_state| will be set to the current handle signals state. | 174 // |*signals_state| will be set to the current handle signals state. |
149 void RemoveAwakable(Awakable* awakable, HandleSignalsState* signals_state); | 175 virtual void RemoveAwakable(Awakable* awakable, |
| 176 HandleSignalsState* signals_state); |
150 | 177 |
151 // Adds a dispatcher to wait on. When the dispatcher satisfies |signals|, it | 178 // Informs the caller of the total serialized size (in bytes) and the total |
152 // will be returned in the next call to |GetReadyDispatchers()|. If | 179 // number of platform handles and ports needed to transfer this dispatcher |
153 // |dispatcher| has been added, it must be removed before adding again, | 180 // across a message pipe. |
154 // otherwise |MOJO_RESULT_ALREADY_EXISTS| will be returned. | 181 // |
155 MojoResult AddWaitingDispatcher(const scoped_refptr<Dispatcher>& dispatcher, | 182 // Must eventually be followed by a call to EndSerializeAndClose(). Note that |
156 MojoHandleSignals signals, | 183 // StartSerialize() and EndSerialize() are always called in sequence, and |
157 uintptr_t context); | 184 // only between calls to BeginTransit() and either (but not both) |
158 // Removes a dispatcher to wait on. If |dispatcher| has not been added, | 185 // CompleteTransitAndClose() or CancelTransit(). |
159 // |MOJO_RESULT_NOT_FOUND| will be returned. | 186 // |
160 MojoResult RemoveWaitingDispatcher( | 187 // For this reason it is IMPERATIVE that the implementation ensure a |
161 const scoped_refptr<Dispatcher>& dispatcher); | 188 // consistent serializable state between BeginTransit() and |
162 // Returns a set of ready dispatchers. |*count| is the maximum number of | 189 // CompleteTransitAndClose()/CancelTransit(). |
163 // dispatchers to return, and will contain the number of dispatchers returned | 190 virtual void StartSerialize(uint32_t* num_bytes, |
164 // in |dispatchers| on completion. | 191 uint32_t* num_ports, |
165 MojoResult GetReadyDispatchers(uint32_t* count, | 192 uint32_t* num_platform_handles); |
166 DispatcherVector* dispatchers, | |
167 MojoResult* results, | |
168 uintptr_t* contexts); | |
169 | 193 |
170 // A dispatcher must be put into a special state in order to be sent across a | 194 // Serializes this dispatcher into |destination|, |ports|, and |handles|. |
171 // message pipe. Outside of tests, only |HandleTableAccess| is allowed to do | 195 // Returns true iff successful, false otherwise. In either case the dispatcher |
172 // this, since there are requirements on the handle table (see below). | 196 // will close. |
173 // | 197 // |
174 // In this special state, only a restricted set of operations is allowed. | 198 // NOTE: Transit MAY still fail after this call returns. Implementations |
175 // These are the ones available as |DispatcherTransport| methods. Other | 199 // should not assume PlatformHandle ownership has transferred until |
176 // |Dispatcher| methods must not be called until |DispatcherTransport::End()| | 200 // CompleteTransitAndClose() is called. In other words, if CancelTransit() is |
177 // has been called. | 201 // called, the implementation should retain its PlatformHandles in working |
178 class HandleTableAccess { | 202 // condition. |
179 private: | 203 virtual bool EndSerialize(void* destination, |
180 friend class Core; | 204 ports::PortName* ports, |
181 friend class HandleTable; | 205 PlatformHandle* handles); |
182 // Tests also need this, to avoid needing |Core|. | |
183 friend DispatcherTransport test::DispatcherTryStartTransport(Dispatcher*); | |
184 | 206 |
185 // This must be called under the handle table lock and only if the handle | 207 // Does whatever is necessary to begin transit of the dispatcher. This |
186 // table entry is not marked busy. The caller must maintain a reference to | 208 // should return |true| if transit is OK, or false if the underlying resource |
187 // |dispatcher| until |DispatcherTransport::End()| is called. | 209 // is deemed busy by the implementation. |
188 static DispatcherTransport TryStartTransport(Dispatcher* dispatcher); | 210 virtual bool BeginTransit(); |
189 }; | |
190 | 211 |
191 // A |TransportData| may serialize dispatchers that are given to it (and which | 212 // Does whatever is necessary to complete transit of the dispatcher, including |
192 // were previously attached to the |MessageInTransit| that is creating it) and | 213 // closure. This is only called upon successfully transmitting an outgoing |
193 // and then (probably in a different process) deserialize. | 214 // message containing this serialized dispatcher. |
194 // Note that the |MessageInTransit| "owns" (i.e., has the only ref to) these | 215 virtual void CompleteTransitAndClose(); |
195 // dispatchers, so there are no locking issues. (There's no lock ordering | |
196 // issue, and in fact no need to take dispatcher locks at all.) | |
197 // TODO(vtl): Consider making another wrapper similar to |DispatcherTransport| | |
198 // (but with an owning, unique reference), and having | |
199 // |CreateEquivalentDispatcherAndCloseImplNoLock()| return that wrapper (and | |
200 // |MessageInTransit|, etc. only holding on to such wrappers). | |
201 class TransportDataAccess { | |
202 private: | |
203 friend class TransportData; | |
204 | 216 |
205 // Serialization API. These functions may only be called on such | 217 // Does whatever is necessary to cancel transit of the dispatcher. The |
206 // dispatchers. See the |Dispatcher| methods of the same names for more | 218 // dispatcher should remain in a working state and resume normal operation. |
207 // details. | 219 virtual void CancelTransit(); |
208 static void StartSerialize(Dispatcher* dispatcher, | |
209 size_t* max_size, | |
210 size_t* max_platform_handles); | |
211 static bool EndSerializeAndClose( | |
212 Dispatcher* dispatcher, | |
213 void* destination, | |
214 size_t* actual_size, | |
215 PlatformHandleVector* platform_handles); | |
216 | 220 |
217 // Deserialization API. | 221 // Deserializes a specific dispatcher type from an incoming message. |
218 // Note: This "clears" (i.e., reset to the invalid handle) any platform | 222 static scoped_refptr<Dispatcher> Deserialize( |
219 // handles that it takes ownership of. | 223 Type type, |
220 static scoped_refptr<Dispatcher> Deserialize( | 224 const void* bytes, |
221 int32_t type, | 225 size_t num_bytes, |
222 const void* source, | 226 const ports::PortName* ports, |
223 size_t size, | 227 size_t num_ports, |
224 PlatformHandleVector* platform_handles); | 228 PlatformHandle* platform_handles, |
225 }; | 229 size_t num_platform_handles); |
226 | 230 |
227 protected: | 231 protected: |
228 friend class base::RefCountedThreadSafe<Dispatcher>; | 232 friend class base::RefCountedThreadSafe<Dispatcher>; |
229 friend class MessagePipeDispatcher; // For TransportStarted/TransportEnded. | |
230 | 233 |
231 Dispatcher(); | 234 Dispatcher(); |
232 virtual ~Dispatcher(); | 235 virtual ~Dispatcher(); |
233 | 236 |
234 // These are to be overridden by subclasses (if necessary). They are called | 237 DISALLOW_COPY_AND_ASSIGN(Dispatcher); |
235 // exactly once -- first |CancelAllAwakablesNoLock()|, then | |
236 // |CloseImplNoLock()|, | |
237 // when the dispatcher is being closed. They are called under |lock_|. | |
238 virtual void CancelAllAwakablesNoLock(); | |
239 virtual void CloseImplNoLock(); | |
240 virtual scoped_refptr<Dispatcher> | |
241 CreateEquivalentDispatcherAndCloseImplNoLock() = 0; | |
242 | |
243 // These are to be overridden by subclasses (if necessary). They are never | |
244 // called after the dispatcher has been closed. They are called under |lock_|. | |
245 // See the descriptions of the methods without the "ImplNoLock" for more | |
246 // information. | |
247 virtual MojoResult WriteMessageImplNoLock( | |
248 const void* bytes, | |
249 uint32_t num_bytes, | |
250 std::vector<DispatcherTransport>* transports, | |
251 MojoWriteMessageFlags flags); | |
252 virtual MojoResult ReadMessageImplNoLock(void* bytes, | |
253 uint32_t* num_bytes, | |
254 DispatcherVector* dispatchers, | |
255 uint32_t* num_dispatchers, | |
256 MojoReadMessageFlags flags); | |
257 virtual MojoResult WriteDataImplNoLock(const void* elements, | |
258 uint32_t* num_bytes, | |
259 MojoWriteDataFlags flags); | |
260 virtual MojoResult BeginWriteDataImplNoLock( | |
261 void** buffer, | |
262 uint32_t* buffer_num_bytes, | |
263 MojoWriteDataFlags flags); | |
264 virtual MojoResult EndWriteDataImplNoLock(uint32_t num_bytes_written); | |
265 virtual MojoResult ReadDataImplNoLock(void* elements, | |
266 uint32_t* num_bytes, | |
267 MojoReadDataFlags flags); | |
268 virtual MojoResult BeginReadDataImplNoLock( | |
269 const void** buffer, | |
270 uint32_t* buffer_num_bytes, | |
271 MojoReadDataFlags flags); | |
272 virtual MojoResult EndReadDataImplNoLock(uint32_t num_bytes_read); | |
273 virtual MojoResult DuplicateBufferHandleImplNoLock( | |
274 const MojoDuplicateBufferHandleOptions* options, | |
275 scoped_refptr<Dispatcher>* new_dispatcher); | |
276 virtual MojoResult MapBufferImplNoLock( | |
277 uint64_t offset, | |
278 uint64_t num_bytes, | |
279 MojoMapBufferFlags flags, | |
280 scoped_ptr<PlatformSharedBufferMapping>* mapping); | |
281 virtual HandleSignalsState GetHandleSignalsStateImplNoLock() const; | |
282 virtual MojoResult AddAwakableImplNoLock(Awakable* awakable, | |
283 MojoHandleSignals signals, | |
284 uintptr_t context, | |
285 HandleSignalsState* signals_state); | |
286 virtual void RemoveAwakableImplNoLock(Awakable* awakable, | |
287 HandleSignalsState* signals_state); | |
288 virtual MojoResult AddWaitingDispatcherImplNoLock( | |
289 const scoped_refptr<Dispatcher>& dispatcher, | |
290 MojoHandleSignals signals, | |
291 uintptr_t context); | |
292 virtual MojoResult RemoveWaitingDispatcherImplNoLock( | |
293 const scoped_refptr<Dispatcher>& dispatcher); | |
294 virtual MojoResult GetReadyDispatchersImplNoLock( | |
295 uint32_t* count, | |
296 DispatcherVector* dispatchers, | |
297 MojoResult* results, | |
298 uintptr_t* contexts); | |
299 | |
300 // These implement the API used to serialize dispatchers (described below). | |
301 // They will only be called on a dispatcher that's attached to and "owned" by | |
302 // a |MessageInTransit|. See the non-"impl" versions for more information. | |
303 // | |
304 // TODO(vtl): Consider making these pure virtual once most things support | |
305 // being passed over a message pipe. | |
306 virtual void StartSerializeImplNoLock(size_t* max_size, | |
307 size_t* max_platform_handles); | |
308 virtual bool EndSerializeAndCloseImplNoLock( | |
309 void* destination, | |
310 size_t* actual_size, | |
311 PlatformHandleVector* platform_handles); | |
312 | |
313 // These are called before and after a dispatcher is being transported. i.e. | |
314 // |TransportStarted| is called |StartSerializeImplNoLock| and | |
315 // |TransportEnded| is called after |EndSerializeAndCloseImplNoLock|. They are | |
316 // needed to avoid deadlocks when transporting a dispatcher. The reason is | |
317 // that |StartSerializeImplNoLock| is called on the main thread and will lead | |
318 // to calling RawChannel::ReleaseHandle. The dispatcher is locked and it will | |
319 // acquire RawChannel's locks as well. however the RawChannel could have its | |
320 // locks acquired on the IO thread and be waiting to acquire the dispatcher's | |
321 // lock. To solve this deadlock, |TransportStarted| is called before the | |
322 // dispatcher's lock is acquired. | |
323 virtual void TransportStarted() {} | |
324 virtual void TransportEnded() {} | |
325 | |
326 // This should be overridden to return true if/when there's an ongoing | |
327 // operation (e.g., two-phase read/writes on data pipes) that should prevent a | |
328 // handle from being sent over a message pipe (with status "busy"). | |
329 virtual bool IsBusyNoLock() const; | |
330 | |
331 // Available to subclasses. (Note: Returns a non-const reference, just like | |
332 // |base::AutoLock|'s constructor takes a non-const reference.) | |
333 base::Lock& lock() const { return lock_; } | |
334 bool is_closed() const { return is_closed_; } | |
335 | |
336 private: | |
337 friend class DispatcherTransport; | |
338 | |
339 // Closes the dispatcher. This must be done under lock, and unlike |Close()|, | |
340 // the dispatcher must not be closed already. (This is the "equivalent" of | |
341 // |CreateEquivalentDispatcherAndCloseNoLock()|, for situations where the | |
342 // dispatcher must be disposed of instead of "transferred".) | |
343 void CloseNoLock(); | |
344 | |
345 // Creates an equivalent dispatcher -- representing the same resource as this | |
346 // dispatcher -- and close (i.e., disable) this dispatcher. I.e., this | |
347 // dispatcher will look as though it was closed, but the resource it | |
348 // represents will be assigned to the new dispatcher. This must be called | |
349 // under the dispatcher's lock. | |
350 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseNoLock(); | |
351 | |
352 // API to serialize dispatchers, exposed to only |TransportData| (via | |
353 // |TransportData|). They may only be called on a dispatcher attached to a | |
354 // |MessageInTransit| (and in particular not in |CoreImpl|'s handle table). | |
355 // | |
356 // Starts the serialization. Returns (via the two "out" parameters) the | |
357 // maximum amount of space that may be needed to serialize this dispatcher (no | |
358 // more than |TransportData::kMaxSerializedDispatcherSize|) and the maximum | |
359 // number of |PlatformHandle|s that may need to be attached. If this | |
360 // dispatcher cannot be serialized, |*max_size| and |*max_platform_handles| | |
361 // should be set to zero. A call to this method will ALWAYS be followed by a | |
362 // call to |EndSerializeAndClose()| (even if this dispatcher cannot be | |
363 // serialized). | |
364 void StartSerialize(size_t* max_size, | |
365 size_t* max_platform_handles); | |
366 // Completes the serialization of this dispatcher and closes it. (This call | |
367 // will always follow an earlier call to |StartSerialize()|. This does so by | |
368 // writing to |destination| and appending any |PlatformHandle|s needed to | |
369 // |platform_handles| (which may be null if no platform handles were indicated | |
370 // to be required to |StartSerialize()|). This may write no more than the | |
371 // amount indicated by |StartSerialize()|. (WARNING: Beware of races, e.g., if | |
372 // something can be mutated between the two calls!) Returns true on success, | |
373 // in which case |*actual_size| is set to the amount it actually wrote to | |
374 // |destination|. On failure, |*actual_size| should not be modified; however, | |
375 // the dispatcher will still be closed. | |
376 bool EndSerializeAndClose(void* destination, | |
377 size_t* actual_size, | |
378 PlatformHandleVector* platform_handles); | |
379 | |
380 // This protects the following members as well as any state added by | |
381 // subclasses. | |
382 mutable base::Lock lock_; | |
383 bool is_closed_; | |
384 | |
385 MOJO_DISALLOW_COPY_AND_ASSIGN(Dispatcher); | |
386 }; | |
387 | |
388 // Wrapper around a |Dispatcher| pointer, while it's being processed to be | |
389 // passed in a message pipe. See the comment about | |
390 // |Dispatcher::HandleTableAccess| for more details. | |
391 // | |
392 // Note: This class is deliberately "thin" -- no more expensive than a | |
393 // |Dispatcher*|. | |
394 class MOJO_SYSTEM_IMPL_EXPORT DispatcherTransport { | |
395 public: | |
396 DispatcherTransport() : dispatcher_(nullptr) {} | |
397 | |
398 void End(); | |
399 | |
400 Dispatcher::Type GetType() const { return dispatcher_->GetType(); } | |
401 bool IsBusy() const { return dispatcher_->IsBusyNoLock(); } | |
402 void Close() { dispatcher_->CloseNoLock(); } | |
403 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndClose() { | |
404 return dispatcher_->CreateEquivalentDispatcherAndCloseNoLock(); | |
405 } | |
406 | |
407 bool is_valid() const { return !!dispatcher_; } | |
408 | |
409 protected: | |
410 Dispatcher* dispatcher() { return dispatcher_; } | |
411 | |
412 private: | |
413 friend class Dispatcher::HandleTableAccess; | |
414 friend class MessagePipeDispatcher; // For AttachTransportsNoLock. | |
415 | |
416 explicit DispatcherTransport(Dispatcher* dispatcher) | |
417 : dispatcher_(dispatcher) {} | |
418 | |
419 Dispatcher* dispatcher_; | |
420 | |
421 // Copy and assign allowed. | |
422 }; | 238 }; |
423 | 239 |
424 // So logging macros and |DCHECK_EQ()|, etc. work. | 240 // So logging macros and |DCHECK_EQ()|, etc. work. |
425 MOJO_SYSTEM_IMPL_EXPORT inline std::ostream& operator<<(std::ostream& out, | 241 MOJO_SYSTEM_IMPL_EXPORT inline std::ostream& operator<<(std::ostream& out, |
426 Dispatcher::Type type) { | 242 Dispatcher::Type type) { |
427 return out << static_cast<int>(type); | 243 return out << static_cast<int>(type); |
428 } | 244 } |
429 | 245 |
430 } // namespace edk | 246 } // namespace edk |
431 } // namespace mojo | 247 } // namespace mojo |
432 | 248 |
433 #endif // MOJO_EDK_SYSTEM_DISPATCHER_H_ | 249 #endif // MOJO_EDK_SYSTEM_DISPATCHER_H_ |
OLD | NEW |