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
3 // found in the LICENSE file.
5 #include "mojo/system/core.h"
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "mojo/embedder/platform_shared_buffer.h"
12 #include "mojo/embedder/simple_platform_support.h" // TODO(vtl): Remove this.
13 #include "mojo/public/c/system/macros.h"
14 #include "mojo/system/constants.h"
15 #include "mojo/system/data_pipe.h"
16 #include "mojo/system/data_pipe_consumer_dispatcher.h"
17 #include "mojo/system/data_pipe_producer_dispatcher.h"
18 #include "mojo/system/dispatcher.h"
19 #include "mojo/system/handle_signals_state.h"
20 #include "mojo/system/local_data_pipe.h"
21 #include "mojo/system/memory.h"
22 #include "mojo/system/message_pipe.h"
23 #include "mojo/system/message_pipe_dispatcher.h"
24 #include "mojo/system/shared_buffer_dispatcher.h"
25 #include "mojo/system/waiter.h"
30 // Implementation notes
32 // Mojo primitives are implemented by the singleton |Core| object. Most calls
33 // are for a "primary" handle (the first argument). |Core::GetDispatcher()| is
34 // used to look up a |Dispatcher| object for a given handle. That object
35 // implements most primitives for that object. The wait primitives are not
36 // attached to objects and are implemented by |Core| itself.
38 // Some objects have multiple handles associated to them, e.g., message pipes
39 // (which have two). In such a case, there is still a |Dispatcher| (e.g.,
40 // |MessagePipeDispatcher|) for each handle, with each handle having a strong
41 // reference to the common "secondary" object (e.g., |MessagePipe|). This
42 // secondary object does NOT have any references to the |Dispatcher|s (even if
43 // it did, it wouldn't be able to do anything with them due to lock order
44 // requirements -- see below).
46 // Waiting is implemented by having the thread that wants to wait call the
47 // |Dispatcher|s for the handles that it wants to wait on with a |Waiter|
48 // object; this |Waiter| object may be created on the stack of that thread or be
49 // kept in thread local storage for that thread (TODO(vtl): future improvement).
50 // The |Dispatcher| then adds the |Waiter| to a |WaiterList| that's either owned
51 // by that |Dispatcher| (see |SimpleDispatcher|) or by a secondary object (e.g.,
52 // |MessagePipe|). To signal/wake a |Waiter|, the object in question -- either a
53 // |SimpleDispatcher| or a secondary object -- talks to its |WaiterList|.
55 // Thread-safety notes
57 // Mojo primitives calls are thread-safe. We achieve this with relatively
58 // fine-grained locking. There is a global handle table lock. This lock should
59 // be held as briefly as possible (TODO(vtl): a future improvement would be to
60 // switch it to a reader-writer lock). Each |Dispatcher| object then has a lock
61 // (which subclasses can use to protect their data).
63 // The lock ordering is as follows:
64 // 1. global handle table lock, global mapping table lock
65 // 2. |Dispatcher| locks
66 // 3. secondary object locks
68 // INF. |Waiter| locks
71 // - While holding a |Dispatcher| lock, you may not unconditionally attempt
72 // to take another |Dispatcher| lock. (This has consequences on the
73 // concurrency semantics of |MojoWriteMessage()| when passing handles.)
74 // Doing so would lead to deadlock.
75 // - Locks at the "INF" level may not have any locks taken while they are
84 MojoHandle Core::AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher) {
85 base::AutoLock locker(handle_table_lock_);
86 return handle_table_.AddDispatcher(dispatcher);
89 scoped_refptr<Dispatcher> Core::GetDispatcher(MojoHandle handle) {
90 if (handle == MOJO_HANDLE_INVALID)
93 base::AutoLock locker(handle_table_lock_);
94 return handle_table_.GetDispatcher(handle);
97 MojoTimeTicks Core::GetTimeTicksNow() {
98 return base::TimeTicks::Now().ToInternalValue();
101 MojoResult Core::Close(MojoHandle handle) {
102 if (handle == MOJO_HANDLE_INVALID)
103 return MOJO_RESULT_INVALID_ARGUMENT;
105 scoped_refptr<Dispatcher> dispatcher;
107 base::AutoLock locker(handle_table_lock_);
109 handle_table_.GetAndRemoveDispatcher(handle, &dispatcher);
110 if (result != MOJO_RESULT_OK)
114 // The dispatcher doesn't have a say in being closed, but gets notified of it.
115 // Note: This is done outside of |handle_table_lock_|. As a result, there's a
116 // race condition that the dispatcher must handle; see the comment in
117 // |Dispatcher| in dispatcher.h.
118 return dispatcher->Close();
121 MojoResult Core::Wait(MojoHandle handle,
122 MojoHandleSignals signals,
123 MojoDeadline deadline,
124 UserPointer<MojoHandleSignalsState> signals_state) {
125 uint32_t unused = static_cast<uint32_t>(-1);
126 HandleSignalsState hss;
127 MojoResult rv = WaitManyInternal(&handle,
132 signals_state.IsNull() ? NULL : &hss);
133 if (rv != MOJO_RESULT_INVALID_ARGUMENT && !signals_state.IsNull())
134 signals_state.Put(hss);
138 MojoResult Core::WaitMany(UserPointer<const MojoHandle> handles,
139 UserPointer<const MojoHandleSignals> signals,
140 uint32_t num_handles,
141 MojoDeadline deadline,
142 UserPointer<uint32_t> result_index,
143 UserPointer<MojoHandleSignalsState> signals_states) {
145 return MOJO_RESULT_INVALID_ARGUMENT;
146 if (num_handles > kMaxWaitManyNumHandles)
147 return MOJO_RESULT_RESOURCE_EXHAUSTED;
149 UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles);
150 UserPointer<const MojoHandleSignals>::Reader signals_reader(signals,
152 uint32_t index = static_cast<uint32_t>(-1);
154 if (signals_states.IsNull()) {
155 rv = WaitManyInternal(handles_reader.GetPointer(),
156 signals_reader.GetPointer(),
162 UserPointer<MojoHandleSignalsState>::Writer signals_states_writer(
163 signals_states, num_handles);
164 // Note: The |reinterpret_cast| is safe, since |HandleSignalsState| is a
165 // subclass of |MojoHandleSignalsState| that doesn't add any data members.
166 rv = WaitManyInternal(handles_reader.GetPointer(),
167 signals_reader.GetPointer(),
171 reinterpret_cast<HandleSignalsState*>(
172 signals_states_writer.GetPointer()));
173 if (rv != MOJO_RESULT_INVALID_ARGUMENT)
174 signals_states_writer.Commit();
176 if (index != static_cast<uint32_t>(-1) && !result_index.IsNull())
177 result_index.Put(index);
181 MojoResult Core::CreateMessagePipe(
182 UserPointer<const MojoCreateMessagePipeOptions> options,
183 UserPointer<MojoHandle> message_pipe_handle0,
184 UserPointer<MojoHandle> message_pipe_handle1) {
185 MojoCreateMessagePipeOptions validated_options = {};
187 MessagePipeDispatcher::ValidateCreateOptions(options, &validated_options);
188 if (result != MOJO_RESULT_OK)
191 scoped_refptr<MessagePipeDispatcher> dispatcher0(
192 new MessagePipeDispatcher(validated_options));
193 scoped_refptr<MessagePipeDispatcher> dispatcher1(
194 new MessagePipeDispatcher(validated_options));
196 std::pair<MojoHandle, MojoHandle> handle_pair;
198 base::AutoLock locker(handle_table_lock_);
199 handle_pair = handle_table_.AddDispatcherPair(dispatcher0, dispatcher1);
201 if (handle_pair.first == MOJO_HANDLE_INVALID) {
202 DCHECK_EQ(handle_pair.second, MOJO_HANDLE_INVALID);
203 LOG(ERROR) << "Handle table full";
204 dispatcher0->Close();
205 dispatcher1->Close();
206 return MOJO_RESULT_RESOURCE_EXHAUSTED;
209 scoped_refptr<MessagePipe> message_pipe(new MessagePipe());
210 dispatcher0->Init(message_pipe, 0);
211 dispatcher1->Init(message_pipe, 1);
213 message_pipe_handle0.Put(handle_pair.first);
214 message_pipe_handle1.Put(handle_pair.second);
215 return MOJO_RESULT_OK;
218 // Implementation note: To properly cancel waiters and avoid other races, this
219 // does not transfer dispatchers from one handle to another, even when sending a
220 // message in-process. Instead, it must transfer the "contents" of the
221 // dispatcher to a new dispatcher, and then close the old dispatcher. If this
222 // isn't done, in the in-process case, calls on the old handle may complete
223 // after the the message has been received and a new handle created (and
224 // possibly even after calls have been made on the new handle).
225 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle,
226 UserPointer<const void> bytes,
228 UserPointer<const MojoHandle> handles,
229 uint32_t num_handles,
230 MojoWriteMessageFlags flags) {
231 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle));
233 return MOJO_RESULT_INVALID_ARGUMENT;
235 // Easy case: not sending any handles.
236 if (num_handles == 0)
237 return dispatcher->WriteMessage(bytes, num_bytes, NULL, flags);
239 // We have to handle |handles| here, since we have to mark them busy in the
240 // global handle table. We can't delegate this to the dispatcher, since the
241 // handle table lock must be acquired before the dispatcher lock.
243 // (This leads to an oddity: |handles|/|num_handles| are always verified for
244 // validity, even for dispatchers that don't support |WriteMessage()| and will
245 // simply return failure unconditionally. It also breaks the usual
246 // left-to-right verification order of arguments.)
247 if (num_handles > kMaxMessageNumHandles)
248 return MOJO_RESULT_RESOURCE_EXHAUSTED;
250 UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles);
252 // We'll need to hold on to the dispatchers so that we can pass them on to
253 // |WriteMessage()| and also so that we can unlock their locks afterwards
254 // without accessing the handle table. These can be dumb pointers, since their
255 // entries in the handle table won't get removed (since they'll be marked as
257 std::vector<DispatcherTransport> transports(num_handles);
259 // When we pass handles, we have to try to take all their dispatchers' locks
260 // and mark the handles as busy. If the call succeeds, we then remove the
261 // handles from the handle table.
263 base::AutoLock locker(handle_table_lock_);
265 handle_table_.MarkBusyAndStartTransport(message_pipe_handle,
266 handles_reader.GetPointer(),
269 if (result != MOJO_RESULT_OK)
274 dispatcher->WriteMessage(bytes, num_bytes, &transports, flags);
276 // We need to release the dispatcher locks before we take the handle table
278 for (uint32_t i = 0; i < num_handles; i++)
282 base::AutoLock locker(handle_table_lock_);
283 if (rv == MOJO_RESULT_OK) {
284 handle_table_.RemoveBusyHandles(handles_reader.GetPointer(), num_handles);
286 handle_table_.RestoreBusyHandles(handles_reader.GetPointer(),
294 MojoResult Core::ReadMessage(MojoHandle message_pipe_handle,
295 UserPointer<void> bytes,
296 UserPointer<uint32_t> num_bytes,
297 UserPointer<MojoHandle> handles,
298 UserPointer<uint32_t> num_handles,
299 MojoReadMessageFlags flags) {
300 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle));
302 return MOJO_RESULT_INVALID_ARGUMENT;
304 uint32_t num_handles_value = num_handles.IsNull() ? 0 : num_handles.Get();
307 if (num_handles_value == 0) {
308 // Easy case: won't receive any handles.
309 rv = dispatcher->ReadMessage(
310 bytes, num_bytes, NULL, &num_handles_value, flags);
312 DispatcherVector dispatchers;
313 rv = dispatcher->ReadMessage(
314 bytes, num_bytes, &dispatchers, &num_handles_value, flags);
315 if (!dispatchers.empty()) {
316 DCHECK_EQ(rv, MOJO_RESULT_OK);
317 DCHECK(!num_handles.IsNull());
318 DCHECK_LE(dispatchers.size(), static_cast<size_t>(num_handles_value));
321 UserPointer<MojoHandle>::Writer handles_writer(handles,
324 base::AutoLock locker(handle_table_lock_);
325 success = handle_table_.AddDispatcherVector(
326 dispatchers, handles_writer.GetPointer());
329 handles_writer.Commit();
331 LOG(ERROR) << "Received message with " << dispatchers.size()
332 << " handles, but handle table full";
333 // Close dispatchers (outside the lock).
334 for (size_t i = 0; i < dispatchers.size(); i++) {
336 dispatchers[i]->Close();
338 if (rv == MOJO_RESULT_OK)
339 rv = MOJO_RESULT_RESOURCE_EXHAUSTED;
344 if (!num_handles.IsNull())
345 num_handles.Put(num_handles_value);
349 MojoResult Core::CreateDataPipe(
350 UserPointer<const MojoCreateDataPipeOptions> options,
351 UserPointer<MojoHandle> data_pipe_producer_handle,
352 UserPointer<MojoHandle> data_pipe_consumer_handle) {
353 MojoCreateDataPipeOptions validated_options = {};
355 DataPipe::ValidateCreateOptions(options, &validated_options);
356 if (result != MOJO_RESULT_OK)
359 scoped_refptr<DataPipeProducerDispatcher> producer_dispatcher(
360 new DataPipeProducerDispatcher());
361 scoped_refptr<DataPipeConsumerDispatcher> consumer_dispatcher(
362 new DataPipeConsumerDispatcher());
364 std::pair<MojoHandle, MojoHandle> handle_pair;
366 base::AutoLock locker(handle_table_lock_);
367 handle_pair = handle_table_.AddDispatcherPair(producer_dispatcher,
368 consumer_dispatcher);
370 if (handle_pair.first == MOJO_HANDLE_INVALID) {
371 DCHECK_EQ(handle_pair.second, MOJO_HANDLE_INVALID);
372 LOG(ERROR) << "Handle table full";
373 producer_dispatcher->Close();
374 consumer_dispatcher->Close();
375 return MOJO_RESULT_RESOURCE_EXHAUSTED;
377 DCHECK_NE(handle_pair.second, MOJO_HANDLE_INVALID);
379 scoped_refptr<DataPipe> data_pipe(new LocalDataPipe(validated_options));
380 producer_dispatcher->Init(data_pipe);
381 consumer_dispatcher->Init(data_pipe);
383 data_pipe_producer_handle.Put(handle_pair.first);
384 data_pipe_consumer_handle.Put(handle_pair.second);
385 return MOJO_RESULT_OK;
388 MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle,
389 UserPointer<const void> elements,
390 UserPointer<uint32_t> num_bytes,
391 MojoWriteDataFlags flags) {
392 scoped_refptr<Dispatcher> dispatcher(
393 GetDispatcher(data_pipe_producer_handle));
395 return MOJO_RESULT_INVALID_ARGUMENT;
397 return dispatcher->WriteData(elements, num_bytes, flags);
400 MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle,
401 UserPointer<void*> buffer,
402 UserPointer<uint32_t> buffer_num_bytes,
403 MojoWriteDataFlags flags) {
404 scoped_refptr<Dispatcher> dispatcher(
405 GetDispatcher(data_pipe_producer_handle));
407 return MOJO_RESULT_INVALID_ARGUMENT;
409 return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags);
412 MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle,
413 uint32_t num_bytes_written) {
414 scoped_refptr<Dispatcher> dispatcher(
415 GetDispatcher(data_pipe_producer_handle));
417 return MOJO_RESULT_INVALID_ARGUMENT;
419 return dispatcher->EndWriteData(num_bytes_written);
422 MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle,
423 UserPointer<void> elements,
424 UserPointer<uint32_t> num_bytes,
425 MojoReadDataFlags flags) {
426 scoped_refptr<Dispatcher> dispatcher(
427 GetDispatcher(data_pipe_consumer_handle));
429 return MOJO_RESULT_INVALID_ARGUMENT;
431 return dispatcher->ReadData(elements, num_bytes, flags);
434 MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle,
435 UserPointer<const void*> buffer,
436 UserPointer<uint32_t> buffer_num_bytes,
437 MojoReadDataFlags flags) {
438 scoped_refptr<Dispatcher> dispatcher(
439 GetDispatcher(data_pipe_consumer_handle));
441 return MOJO_RESULT_INVALID_ARGUMENT;
443 return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags);
446 MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle,
447 uint32_t num_bytes_read) {
448 scoped_refptr<Dispatcher> dispatcher(
449 GetDispatcher(data_pipe_consumer_handle));
451 return MOJO_RESULT_INVALID_ARGUMENT;
453 return dispatcher->EndReadData(num_bytes_read);
456 MojoResult Core::CreateSharedBuffer(
457 UserPointer<const MojoCreateSharedBufferOptions> options,
459 UserPointer<MojoHandle> shared_buffer_handle) {
460 MojoCreateSharedBufferOptions validated_options = {};
461 MojoResult result = SharedBufferDispatcher::ValidateCreateOptions(
462 options, &validated_options);
463 if (result != MOJO_RESULT_OK)
466 // TODO(vtl): |Core| should have a |PlatformSupport| passed in at creation
467 // time, and we should use that instead.
468 embedder::SimplePlatformSupport platform_support;
469 scoped_refptr<SharedBufferDispatcher> dispatcher;
470 result = SharedBufferDispatcher::Create(
471 &platform_support, validated_options, num_bytes, &dispatcher);
472 if (result != MOJO_RESULT_OK) {
477 MojoHandle h = AddDispatcher(dispatcher);
478 if (h == MOJO_HANDLE_INVALID) {
479 LOG(ERROR) << "Handle table full";
481 return MOJO_RESULT_RESOURCE_EXHAUSTED;
484 shared_buffer_handle.Put(h);
485 return MOJO_RESULT_OK;
488 MojoResult Core::DuplicateBufferHandle(
489 MojoHandle buffer_handle,
490 UserPointer<const MojoDuplicateBufferHandleOptions> options,
491 UserPointer<MojoHandle> new_buffer_handle) {
492 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle));
494 return MOJO_RESULT_INVALID_ARGUMENT;
496 // Don't verify |options| here; that's the dispatcher's job.
497 scoped_refptr<Dispatcher> new_dispatcher;
499 dispatcher->DuplicateBufferHandle(options, &new_dispatcher);
500 if (result != MOJO_RESULT_OK)
503 MojoHandle new_handle = AddDispatcher(new_dispatcher);
504 if (new_handle == MOJO_HANDLE_INVALID) {
505 LOG(ERROR) << "Handle table full";
507 return MOJO_RESULT_RESOURCE_EXHAUSTED;
510 new_buffer_handle.Put(new_handle);
511 return MOJO_RESULT_OK;
514 MojoResult Core::MapBuffer(MojoHandle buffer_handle,
517 UserPointer<void*> buffer,
518 MojoMapBufferFlags flags) {
519 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle));
521 return MOJO_RESULT_INVALID_ARGUMENT;
523 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping;
524 MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping);
525 if (result != MOJO_RESULT_OK)
529 void* address = mapping->GetBase();
531 base::AutoLock locker(mapping_table_lock_);
532 result = mapping_table_.AddMapping(mapping.Pass());
534 if (result != MOJO_RESULT_OK)
538 return MOJO_RESULT_OK;
541 MojoResult Core::UnmapBuffer(UserPointer<void> buffer) {
542 base::AutoLock locker(mapping_table_lock_);
543 return mapping_table_.RemoveMapping(buffer.GetPointerValue());
546 // Note: We allow |handles| to repeat the same handle multiple times, since
547 // different flags may be specified.
548 // TODO(vtl): This incurs a performance cost in |RemoveWaiter()|. Analyze this
549 // more carefully and address it if necessary.
550 MojoResult Core::WaitManyInternal(const MojoHandle* handles,
551 const MojoHandleSignals* signals,
552 uint32_t num_handles,
553 MojoDeadline deadline,
554 uint32_t* result_index,
555 HandleSignalsState* signals_states) {
556 DCHECK_GT(num_handles, 0u);
557 DCHECK_EQ(*result_index, static_cast<uint32_t>(-1));
559 DispatcherVector dispatchers;
560 dispatchers.reserve(num_handles);
561 for (uint32_t i = 0; i < num_handles; i++) {
562 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]);
565 return MOJO_RESULT_INVALID_ARGUMENT;
567 dispatchers.push_back(dispatcher);
570 // TODO(vtl): Should make the waiter live (permanently) in TLS.
575 MojoResult rv = MOJO_RESULT_OK;
576 for (i = 0; i < num_handles; i++) {
577 rv = dispatchers[i]->AddWaiter(
578 &waiter, signals[i], i, signals_states ? &signals_states[i] : NULL);
579 if (rv != MOJO_RESULT_OK) {
584 uint32_t num_added = i;
586 if (rv == MOJO_RESULT_ALREADY_EXISTS)
587 rv = MOJO_RESULT_OK; // The i-th one is already "triggered".
588 else if (rv == MOJO_RESULT_OK)
589 rv = waiter.Wait(deadline, result_index);
591 // Make sure no other dispatchers try to wake |waiter| for the current
592 // |Wait()|/|WaitMany()| call. (Only after doing this can |waiter| be
593 // destroyed, but this would still be required if the waiter were in TLS.)
594 for (i = 0; i < num_added; i++) {
595 dispatchers[i]->RemoveWaiter(&waiter,
596 signals_states ? &signals_states[i] : NULL);
598 if (signals_states) {
599 for (; i < num_handles; i++)
600 signals_states[i] = dispatchers[i]->GetHandleSignalsState();
606 } // namespace system