| 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 #include "mojo/edk/system/core.h" | 5 #include "mojo/edk/system/core.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 // |MessagePipeDispatcher|) for each handle, with each handle having a strong | 40 // |MessagePipeDispatcher|) for each handle, with each handle having a strong |
| 41 // reference to the common "secondary" object (e.g., |MessagePipe|). This | 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 | 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 | 43 // it did, it wouldn't be able to do anything with them due to lock order |
| 44 // requirements -- see below). | 44 // requirements -- see below). |
| 45 // | 45 // |
| 46 // Waiting is implemented by having the thread that wants to wait call the | 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| | 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 | 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). | 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 | 50 // The |Dispatcher| then adds the |Waiter| to an |AwakableList| that's either |
| 51 // by that |Dispatcher| (see |SimpleDispatcher|) or by a secondary object (e.g., | 51 // owned by that |Dispatcher| (see |SimpleDispatcher|) or by a secondary object |
| 52 // |MessagePipe|). To signal/wake a |Waiter|, the object in question -- either a | 52 // (e.g., |MessagePipe|). To signal/wake a |Waiter|, the object in question -- |
| 53 // |SimpleDispatcher| or a secondary object -- talks to its |WaiterList|. | 53 // either a |SimpleDispatcher| or a secondary object -- talks to its |
| 54 // |AwakableList|. |
| 54 | 55 |
| 55 // Thread-safety notes | 56 // Thread-safety notes |
| 56 // | 57 // |
| 57 // Mojo primitives calls are thread-safe. We achieve this with relatively | 58 // 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 // 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 // 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 // switch it to a reader-writer lock). Each |Dispatcher| object then has a lock |
| 61 // (which subclasses can use to protect their data). | 62 // (which subclasses can use to protect their data). |
| 62 // | 63 // |
| 63 // The lock ordering is as follows: | 64 // The lock ordering is as follows: |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 return MOJO_RESULT_OK; | 526 return MOJO_RESULT_OK; |
| 526 } | 527 } |
| 527 | 528 |
| 528 MojoResult Core::UnmapBuffer(UserPointer<void> buffer) { | 529 MojoResult Core::UnmapBuffer(UserPointer<void> buffer) { |
| 529 base::AutoLock locker(mapping_table_lock_); | 530 base::AutoLock locker(mapping_table_lock_); |
| 530 return mapping_table_.RemoveMapping(buffer.GetPointerValue()); | 531 return mapping_table_.RemoveMapping(buffer.GetPointerValue()); |
| 531 } | 532 } |
| 532 | 533 |
| 533 // Note: We allow |handles| to repeat the same handle multiple times, since | 534 // Note: We allow |handles| to repeat the same handle multiple times, since |
| 534 // different flags may be specified. | 535 // different flags may be specified. |
| 535 // TODO(vtl): This incurs a performance cost in |RemoveWaiter()|. Analyze this | 536 // TODO(vtl): This incurs a performance cost in |Remove()|. Analyze this |
| 536 // more carefully and address it if necessary. | 537 // more carefully and address it if necessary. |
| 537 MojoResult Core::WaitManyInternal(const MojoHandle* handles, | 538 MojoResult Core::WaitManyInternal(const MojoHandle* handles, |
| 538 const MojoHandleSignals* signals, | 539 const MojoHandleSignals* signals, |
| 539 uint32_t num_handles, | 540 uint32_t num_handles, |
| 540 MojoDeadline deadline, | 541 MojoDeadline deadline, |
| 541 uint32_t* result_index, | 542 uint32_t* result_index, |
| 542 HandleSignalsState* signals_states) { | 543 HandleSignalsState* signals_states) { |
| 543 DCHECK_GT(num_handles, 0u); | 544 DCHECK_GT(num_handles, 0u); |
| 544 DCHECK_EQ(*result_index, static_cast<uint32_t>(-1)); | 545 DCHECK_EQ(*result_index, static_cast<uint32_t>(-1)); |
| 545 | 546 |
| 546 DispatcherVector dispatchers; | 547 DispatcherVector dispatchers; |
| 547 dispatchers.reserve(num_handles); | 548 dispatchers.reserve(num_handles); |
| 548 for (uint32_t i = 0; i < num_handles; i++) { | 549 for (uint32_t i = 0; i < num_handles; i++) { |
| 549 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]); | 550 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]); |
| 550 if (!dispatcher.get()) { | 551 if (!dispatcher.get()) { |
| 551 *result_index = i; | 552 *result_index = i; |
| 552 return MOJO_RESULT_INVALID_ARGUMENT; | 553 return MOJO_RESULT_INVALID_ARGUMENT; |
| 553 } | 554 } |
| 554 dispatchers.push_back(dispatcher); | 555 dispatchers.push_back(dispatcher); |
| 555 } | 556 } |
| 556 | 557 |
| 557 // TODO(vtl): Should make the waiter live (permanently) in TLS. | 558 // TODO(vtl): Should make the waiter live (permanently) in TLS. |
| 558 Waiter waiter; | 559 Waiter waiter; |
| 559 waiter.Init(); | 560 waiter.Init(); |
| 560 | 561 |
| 561 uint32_t i; | 562 uint32_t i; |
| 562 MojoResult rv = MOJO_RESULT_OK; | 563 MojoResult rv = MOJO_RESULT_OK; |
| 563 for (i = 0; i < num_handles; i++) { | 564 for (i = 0; i < num_handles; i++) { |
| 564 rv = dispatchers[i]->AddWaiter( | 565 rv = dispatchers[i]->AddAwakable( |
| 565 &waiter, signals[i], i, signals_states ? &signals_states[i] : nullptr); | 566 &waiter, signals[i], i, signals_states ? &signals_states[i] : nullptr); |
| 566 if (rv != MOJO_RESULT_OK) { | 567 if (rv != MOJO_RESULT_OK) { |
| 567 *result_index = i; | 568 *result_index = i; |
| 568 break; | 569 break; |
| 569 } | 570 } |
| 570 } | 571 } |
| 571 uint32_t num_added = i; | 572 uint32_t num_added = i; |
| 572 | 573 |
| 573 if (rv == MOJO_RESULT_ALREADY_EXISTS) | 574 if (rv == MOJO_RESULT_ALREADY_EXISTS) |
| 574 rv = MOJO_RESULT_OK; // The i-th one is already "triggered". | 575 rv = MOJO_RESULT_OK; // The i-th one is already "triggered". |
| 575 else if (rv == MOJO_RESULT_OK) | 576 else if (rv == MOJO_RESULT_OK) |
| 576 rv = waiter.Wait(deadline, result_index); | 577 rv = waiter.Wait(deadline, result_index); |
| 577 | 578 |
| 578 // Make sure no other dispatchers try to wake |waiter| for the current | 579 // Make sure no other dispatchers try to wake |waiter| for the current |
| 579 // |Wait()|/|WaitMany()| call. (Only after doing this can |waiter| be | 580 // |Wait()|/|WaitMany()| call. (Only after doing this can |waiter| be |
| 580 // destroyed, but this would still be required if the waiter were in TLS.) | 581 // destroyed, but this would still be required if the waiter were in TLS.) |
| 581 for (i = 0; i < num_added; i++) { | 582 for (i = 0; i < num_added; i++) { |
| 582 dispatchers[i]->RemoveWaiter(&waiter, | 583 dispatchers[i]->RemoveAwakable( |
| 583 signals_states ? &signals_states[i] : nullptr); | 584 &waiter, signals_states ? &signals_states[i] : nullptr); |
| 584 } | 585 } |
| 585 if (signals_states) { | 586 if (signals_states) { |
| 586 for (; i < num_handles; i++) | 587 for (; i < num_handles; i++) |
| 587 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); | 588 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); |
| 588 } | 589 } |
| 589 | 590 |
| 590 return rv; | 591 return rv; |
| 591 } | 592 } |
| 592 | 593 |
| 593 } // namespace system | 594 } // namespace system |
| 594 } // namespace mojo | 595 } // namespace mojo |
| OLD | NEW |