| 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/dispatcher.h" | 5 #include "mojo/edk/system/dispatcher.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "mojo/edk/system/configuration.h" | 8 #include "mojo/edk/system/configuration.h" |
| 9 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" | 9 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" |
| 10 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" | 10 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" |
| 11 #include "mojo/edk/system/handle.h" | 11 #include "mojo/edk/system/handle.h" |
| 12 #include "mojo/edk/system/handle_transport.h" | 12 #include "mojo/edk/system/handle_transport.h" |
| 13 #include "mojo/edk/system/message_pipe_dispatcher.h" | 13 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 14 #include "mojo/edk/system/platform_handle_dispatcher.h" | 14 #include "mojo/edk/system/platform_handle_dispatcher.h" |
| 15 #include "mojo/edk/system/shared_buffer_dispatcher.h" | 15 #include "mojo/edk/system/shared_buffer_dispatcher.h" |
| 16 | 16 |
| 17 using mojo::platform::PlatformSharedBufferMapping; | 17 using mojo::platform::PlatformSharedBufferMapping; |
| 18 using mojo::platform::ScopedPlatformHandle; | 18 using mojo::platform::ScopedPlatformHandle; |
| 19 using mojo::util::MutexLocker; | 19 using mojo::util::MutexLocker; |
| 20 using mojo::util::RefPtr; | 20 using mojo::util::RefPtr; |
| 21 | 21 |
| 22 namespace mojo { | 22 namespace mojo { |
| 23 namespace system { | 23 namespace system { |
| 24 | 24 |
| 25 namespace test { | 25 namespace test { |
| 26 | 26 |
| 27 // TODO(vtl): Maybe this should be defined in a test-only file instead. | 27 // TODO(vtl): Maybe this should be defined in a test-only file instead. |
| 28 DispatcherTransport HandleTryStartTransport(const Handle& handle) { | 28 HandleTransport HandleTryStartTransport(const Handle& handle) { |
| 29 return Dispatcher::HandleTableAccess::TryStartTransport(handle); | 29 return Dispatcher::HandleTableAccess::TryStartTransport(handle); |
| 30 } | 30 } |
| 31 | 31 |
| 32 } // namespace test | 32 } // namespace test |
| 33 | 33 |
| 34 // TODO(vtl): The thread-safety analyzer isn't smart enough to deal with the | 34 // TODO(vtl): The thread-safety analyzer isn't smart enough to deal with the |
| 35 // fact that we give up if |TryLock()| fails. | 35 // fact that we give up if |TryLock()| fails. |
| 36 // static | 36 // static |
| 37 DispatcherTransport Dispatcher::HandleTableAccess::TryStartTransport( | 37 HandleTransport Dispatcher::HandleTableAccess::TryStartTransport( |
| 38 const Handle& handle) MOJO_NO_THREAD_SAFETY_ANALYSIS { | 38 const Handle& handle) MOJO_NO_THREAD_SAFETY_ANALYSIS { |
| 39 DCHECK(handle.dispatcher); | 39 DCHECK(handle.dispatcher); |
| 40 | 40 |
| 41 if (!handle.dispatcher->mutex_.TryLock()) | 41 if (!handle.dispatcher->mutex_.TryLock()) |
| 42 return DispatcherTransport(); | 42 return HandleTransport(); |
| 43 | 43 |
| 44 // We shouldn't race with things that close dispatchers, since closing can | 44 // We shouldn't race with things that close dispatchers, since closing can |
| 45 // only take place either under |handle_table_mutex_| or when the handle is | 45 // only take place either under |handle_table_mutex_| or when the handle is |
| 46 // marked as busy. | 46 // marked as busy. |
| 47 DCHECK(!handle.dispatcher->is_closed_); | 47 DCHECK(!handle.dispatcher->is_closed_); |
| 48 | 48 |
| 49 return DispatcherTransport(handle); | 49 return HandleTransport(handle); |
| 50 } | 50 } |
| 51 | 51 |
| 52 // static | 52 // static |
| 53 void Dispatcher::TransportDataAccess::StartSerialize( | 53 void Dispatcher::TransportDataAccess::StartSerialize( |
| 54 Dispatcher* dispatcher, | 54 Dispatcher* dispatcher, |
| 55 Channel* channel, | 55 Channel* channel, |
| 56 size_t* max_size, | 56 size_t* max_size, |
| 57 size_t* max_platform_handles) { | 57 size_t* max_platform_handles) { |
| 58 DCHECK(dispatcher); | 58 DCHECK(dispatcher); |
| 59 dispatcher->StartSerialize(channel, max_size, max_platform_handles); | 59 dispatcher->StartSerialize(channel, max_size, max_platform_handles); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 101 |
| 102 MojoResult Dispatcher::Close() { | 102 MojoResult Dispatcher::Close() { |
| 103 MutexLocker locker(&mutex_); | 103 MutexLocker locker(&mutex_); |
| 104 if (is_closed_) | 104 if (is_closed_) |
| 105 return MOJO_RESULT_INVALID_ARGUMENT; | 105 return MOJO_RESULT_INVALID_ARGUMENT; |
| 106 | 106 |
| 107 CloseNoLock(); | 107 CloseNoLock(); |
| 108 return MOJO_RESULT_OK; | 108 return MOJO_RESULT_OK; |
| 109 } | 109 } |
| 110 | 110 |
| 111 MojoResult Dispatcher::WriteMessage( | 111 MojoResult Dispatcher::WriteMessage(UserPointer<const void> bytes, |
| 112 UserPointer<const void> bytes, | 112 uint32_t num_bytes, |
| 113 uint32_t num_bytes, | 113 std::vector<HandleTransport>* transports, |
| 114 std::vector<DispatcherTransport>* transports, | 114 MojoWriteMessageFlags flags) { |
| 115 MojoWriteMessageFlags flags) { | |
| 116 DCHECK(!transports || | 115 DCHECK(!transports || |
| 117 (transports->size() > 0 && | 116 (transports->size() > 0 && |
| 118 transports->size() < GetConfiguration().max_message_num_handles)); | 117 transports->size() < GetConfiguration().max_message_num_handles)); |
| 119 | 118 |
| 120 MutexLocker locker(&mutex_); | 119 MutexLocker locker(&mutex_); |
| 121 if (is_closed_) | 120 if (is_closed_) |
| 122 return MOJO_RESULT_INVALID_ARGUMENT; | 121 return MOJO_RESULT_INVALID_ARGUMENT; |
| 123 | 122 |
| 124 return WriteMessageImplNoLock(bytes, num_bytes, transports, flags); | 123 return WriteMessageImplNoLock(bytes, num_bytes, transports, flags); |
| 125 } | 124 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 void Dispatcher::CloseImplNoLock() { | 316 void Dispatcher::CloseImplNoLock() { |
| 318 mutex_.AssertHeld(); | 317 mutex_.AssertHeld(); |
| 319 DCHECK(is_closed_); | 318 DCHECK(is_closed_); |
| 320 // This may not need to do anything. Dispatchers should override this to do | 319 // This may not need to do anything. Dispatchers should override this to do |
| 321 // any actual close-time cleanup necessary. | 320 // any actual close-time cleanup necessary. |
| 322 } | 321 } |
| 323 | 322 |
| 324 MojoResult Dispatcher::WriteMessageImplNoLock( | 323 MojoResult Dispatcher::WriteMessageImplNoLock( |
| 325 UserPointer<const void> /*bytes*/, | 324 UserPointer<const void> /*bytes*/, |
| 326 uint32_t /*num_bytes*/, | 325 uint32_t /*num_bytes*/, |
| 327 std::vector<DispatcherTransport>* /*transports*/, | 326 std::vector<HandleTransport>* /*transports*/, |
| 328 MojoWriteMessageFlags /*flags*/) { | 327 MojoWriteMessageFlags /*flags*/) { |
| 329 mutex_.AssertHeld(); | 328 mutex_.AssertHeld(); |
| 330 DCHECK(!is_closed_); | 329 DCHECK(!is_closed_); |
| 331 // By default, not supported. Only needed for message pipe dispatchers. | 330 // By default, not supported. Only needed for message pipe dispatchers. |
| 332 return MOJO_RESULT_INVALID_ARGUMENT; | 331 return MOJO_RESULT_INVALID_ARGUMENT; |
| 333 } | 332 } |
| 334 | 333 |
| 335 MojoResult Dispatcher::ReadMessageImplNoLock( | 334 MojoResult Dispatcher::ReadMessageImplNoLock( |
| 336 UserPointer<void> /*bytes*/, | 335 UserPointer<void> /*bytes*/, |
| 337 UserPointer<uint32_t> /*num_bytes*/, | 336 UserPointer<uint32_t> /*num_bytes*/, |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 // want to remove or weaken). | 566 // want to remove or weaken). |
| 568 MutexLocker locker(&mutex_); | 567 MutexLocker locker(&mutex_); |
| 569 #endif | 568 #endif |
| 570 | 569 |
| 571 return EndSerializeAndCloseImplNoLock(channel, destination, actual_size, | 570 return EndSerializeAndCloseImplNoLock(channel, destination, actual_size, |
| 572 platform_handles); | 571 platform_handles); |
| 573 } | 572 } |
| 574 | 573 |
| 575 } // namespace system | 574 } // namespace system |
| 576 } // namespace mojo | 575 } // namespace mojo |
| OLD | NEW |