| 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/system/core_impl.h" | 5 #include "mojo/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" |
| 11 #include "mojo/public/c/system/core.h" |
| 11 #include "mojo/system/constants.h" | 12 #include "mojo/system/constants.h" |
| 12 #include "mojo/system/data_pipe.h" | 13 #include "mojo/system/data_pipe.h" |
| 13 #include "mojo/system/data_pipe_consumer_dispatcher.h" | 14 #include "mojo/system/data_pipe_consumer_dispatcher.h" |
| 14 #include "mojo/system/data_pipe_producer_dispatcher.h" | 15 #include "mojo/system/data_pipe_producer_dispatcher.h" |
| 15 #include "mojo/system/dispatcher.h" | 16 #include "mojo/system/dispatcher.h" |
| 16 #include "mojo/system/local_data_pipe.h" | 17 #include "mojo/system/local_data_pipe.h" |
| 17 #include "mojo/system/memory.h" | 18 #include "mojo/system/memory.h" |
| 18 #include "mojo/system/message_pipe.h" | 19 #include "mojo/system/message_pipe.h" |
| 19 #include "mojo/system/message_pipe_dispatcher.h" | 20 #include "mojo/system/message_pipe_dispatcher.h" |
| 20 #include "mojo/system/raw_shared_buffer.h" | 21 #include "mojo/system/raw_shared_buffer.h" |
| 21 #include "mojo/system/shared_buffer_dispatcher.h" | 22 #include "mojo/system/shared_buffer_dispatcher.h" |
| 22 #include "mojo/system/waiter.h" | 23 #include "mojo/system/waiter.h" |
| 23 | 24 |
| 24 namespace mojo { | 25 namespace mojo { |
| 25 namespace system { | 26 namespace system { |
| 26 | 27 |
| 27 // Implementation notes | 28 // Implementation notes |
| 28 // | 29 // |
| 29 // Mojo primitives are implemented by the singleton |CoreImpl| object. Most | 30 // Mojo primitives are implemented by the singleton |Core| object. Most |
| 30 // calls are for a "primary" handle (the first argument). | 31 // calls are for a "primary" handle (the first argument). |
| 31 // |CoreImpl::GetDispatcher()| is used to look up a |Dispatcher| object for a | 32 // |Core::GetDispatcher()| is used to look up a |Dispatcher| object for a |
| 32 // given handle. That object implements most primitives for that object. The | 33 // given handle. That object implements most primitives for that object. The |
| 33 // wait primitives are not attached to objects and are implemented by |CoreImpl| | 34 // wait primitives are not attached to objects and are implemented by |Core| |
| 34 // itself. | 35 // itself. |
| 35 // | 36 // |
| 36 // Some objects have multiple handles associated to them, e.g., message pipes | 37 // Some objects have multiple handles associated to them, e.g., message pipes |
| 37 // (which have two). In such a case, there is still a |Dispatcher| (e.g., | 38 // (which have two). In such a case, there is still a |Dispatcher| (e.g., |
| 38 // |MessagePipeDispatcher|) for each handle, with each handle having a strong | 39 // |MessagePipeDispatcher|) for each handle, with each handle having a strong |
| 39 // reference to the common "secondary" object (e.g., |MessagePipe|). This | 40 // reference to the common "secondary" object (e.g., |MessagePipe|). This |
| 40 // secondary object does NOT have any references to the |Dispatcher|s (even if | 41 // secondary object does NOT have any references to the |Dispatcher|s (even if |
| 41 // it did, it wouldn't be able to do anything with them due to lock order | 42 // it did, it wouldn't be able to do anything with them due to lock order |
| 42 // requirements -- see below). | 43 // requirements -- see below). |
| 43 // | 44 // |
| (...skipping 22 matching lines...) Expand all Loading... |
| 66 // INF. |Waiter| locks | 67 // INF. |Waiter| locks |
| 67 // | 68 // |
| 68 // Notes: | 69 // Notes: |
| 69 // - While holding a |Dispatcher| lock, you may not unconditionally attempt | 70 // - While holding a |Dispatcher| lock, you may not unconditionally attempt |
| 70 // to take another |Dispatcher| lock. (This has consequences on the | 71 // to take another |Dispatcher| lock. (This has consequences on the |
| 71 // concurrency semantics of |MojoWriteMessage()| when passing handles.) | 72 // concurrency semantics of |MojoWriteMessage()| when passing handles.) |
| 72 // Doing so would lead to deadlock. | 73 // Doing so would lead to deadlock. |
| 73 // - Locks at the "INF" level may not have any locks taken while they are | 74 // - Locks at the "INF" level may not have any locks taken while they are |
| 74 // held. | 75 // held. |
| 75 | 76 |
| 76 CoreImpl::HandleTableEntry::HandleTableEntry() | 77 Core::HandleTableEntry::HandleTableEntry() |
| 77 : busy(false) { | 78 : busy(false) { |
| 78 } | 79 } |
| 79 | 80 |
| 80 CoreImpl::HandleTableEntry::HandleTableEntry( | 81 Core::HandleTableEntry::HandleTableEntry( |
| 81 const scoped_refptr<Dispatcher>& dispatcher) | 82 const scoped_refptr<Dispatcher>& dispatcher) |
| 82 : dispatcher(dispatcher), | 83 : dispatcher(dispatcher), |
| 83 busy(false) { | 84 busy(false) { |
| 84 } | 85 } |
| 85 | 86 |
| 86 CoreImpl::HandleTableEntry::~HandleTableEntry() { | 87 Core::HandleTableEntry::~HandleTableEntry() { |
| 87 DCHECK(!busy); | 88 DCHECK(!busy); |
| 88 } | 89 } |
| 89 | 90 |
| 90 CoreImpl::CoreImpl() { | 91 Core::Core() { |
| 91 } | 92 } |
| 92 | 93 |
| 93 CoreImpl::~CoreImpl() { | 94 Core::~Core() { |
| 94 // This should usually not be reached (the singleton lives forever), except in | |
| 95 // tests. | |
| 96 } | 95 } |
| 97 | 96 |
| 98 MojoHandle CoreImpl::AddDispatcher( | 97 MojoHandle Core::AddDispatcher( |
| 99 const scoped_refptr<Dispatcher>& dispatcher) { | 98 const scoped_refptr<Dispatcher>& dispatcher) { |
| 100 base::AutoLock locker(handle_table_lock_); | 99 base::AutoLock locker(handle_table_lock_); |
| 101 return handle_table_.AddDispatcher(dispatcher); | 100 return handle_table_.AddDispatcher(dispatcher); |
| 102 } | 101 } |
| 103 | 102 |
| 104 MojoTimeTicks CoreImpl::GetTimeTicksNow() { | 103 MojoTimeTicks Core::GetTimeTicksNow() { |
| 105 return base::TimeTicks::Now().ToInternalValue(); | 104 return base::TimeTicks::Now().ToInternalValue(); |
| 106 } | 105 } |
| 107 | 106 |
| 108 MojoResult CoreImpl::Close(MojoHandle handle) { | 107 MojoResult Core::Close(MojoHandle handle) { |
| 109 if (handle == MOJO_HANDLE_INVALID) | 108 if (handle == MOJO_HANDLE_INVALID) |
| 110 return MOJO_RESULT_INVALID_ARGUMENT; | 109 return MOJO_RESULT_INVALID_ARGUMENT; |
| 111 | 110 |
| 112 scoped_refptr<Dispatcher> dispatcher; | 111 scoped_refptr<Dispatcher> dispatcher; |
| 113 { | 112 { |
| 114 base::AutoLock locker(handle_table_lock_); | 113 base::AutoLock locker(handle_table_lock_); |
| 115 MojoResult result = handle_table_.GetAndRemoveDispatcher(handle, | 114 MojoResult result = handle_table_.GetAndRemoveDispatcher(handle, |
| 116 &dispatcher); | 115 &dispatcher); |
| 117 if (result != MOJO_RESULT_OK) | 116 if (result != MOJO_RESULT_OK) |
| 118 return result; | 117 return result; |
| 119 } | 118 } |
| 120 | 119 |
| 121 // The dispatcher doesn't have a say in being closed, but gets notified of it. | 120 // The dispatcher doesn't have a say in being closed, but gets notified of it. |
| 122 // Note: This is done outside of |handle_table_lock_|. As a result, there's a | 121 // Note: This is done outside of |handle_table_lock_|. As a result, there's a |
| 123 // race condition that the dispatcher must handle; see the comment in | 122 // race condition that the dispatcher must handle; see the comment in |
| 124 // |Dispatcher| in dispatcher.h. | 123 // |Dispatcher| in dispatcher.h. |
| 125 return dispatcher->Close(); | 124 return dispatcher->Close(); |
| 126 } | 125 } |
| 127 | 126 |
| 128 MojoResult CoreImpl::Wait(MojoHandle handle, | 127 MojoResult Core::Wait(MojoHandle handle, |
| 129 MojoWaitFlags flags, | 128 MojoWaitFlags flags, |
| 130 MojoDeadline deadline) { | 129 MojoDeadline deadline) { |
| 131 return WaitManyInternal(&handle, &flags, 1, deadline); | 130 return WaitManyInternal(&handle, &flags, 1, deadline); |
| 132 } | 131 } |
| 133 | 132 |
| 134 MojoResult CoreImpl::WaitMany(const MojoHandle* handles, | 133 MojoResult Core::WaitMany(const MojoHandle* handles, |
| 135 const MojoWaitFlags* flags, | 134 const MojoWaitFlags* flags, |
| 136 uint32_t num_handles, | 135 uint32_t num_handles, |
| 137 MojoDeadline deadline) { | 136 MojoDeadline deadline) { |
| 138 if (!VerifyUserPointer<MojoHandle>(handles, num_handles)) | 137 if (!VerifyUserPointer<MojoHandle>(handles, num_handles)) |
| 139 return MOJO_RESULT_INVALID_ARGUMENT; | 138 return MOJO_RESULT_INVALID_ARGUMENT; |
| 140 if (!VerifyUserPointer<MojoWaitFlags>(flags, num_handles)) | 139 if (!VerifyUserPointer<MojoWaitFlags>(flags, num_handles)) |
| 141 return MOJO_RESULT_INVALID_ARGUMENT; | 140 return MOJO_RESULT_INVALID_ARGUMENT; |
| 142 if (num_handles < 1) | 141 if (num_handles < 1) |
| 143 return MOJO_RESULT_INVALID_ARGUMENT; | 142 return MOJO_RESULT_INVALID_ARGUMENT; |
| 144 if (num_handles > kMaxWaitManyNumHandles) | 143 if (num_handles > kMaxWaitManyNumHandles) |
| 145 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 144 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 146 return WaitManyInternal(handles, flags, num_handles, deadline); | 145 return WaitManyInternal(handles, flags, num_handles, deadline); |
| 147 } | 146 } |
| 148 | 147 |
| 149 MojoResult CoreImpl::CreateMessagePipe(MojoHandle* message_pipe_handle0, | 148 MojoResult Core::CreateMessagePipe(MojoHandle* message_pipe_handle0, |
| 150 MojoHandle* message_pipe_handle1) { | 149 MojoHandle* message_pipe_handle1) { |
| 151 if (!VerifyUserPointer<MojoHandle>(message_pipe_handle0, 1)) | 150 if (!VerifyUserPointer<MojoHandle>(message_pipe_handle0, 1)) |
| 152 return MOJO_RESULT_INVALID_ARGUMENT; | 151 return MOJO_RESULT_INVALID_ARGUMENT; |
| 153 if (!VerifyUserPointer<MojoHandle>(message_pipe_handle1, 1)) | 152 if (!VerifyUserPointer<MojoHandle>(message_pipe_handle1, 1)) |
| 154 return MOJO_RESULT_INVALID_ARGUMENT; | 153 return MOJO_RESULT_INVALID_ARGUMENT; |
| 155 | 154 |
| 156 scoped_refptr<MessagePipeDispatcher> dispatcher0(new MessagePipeDispatcher()); | 155 scoped_refptr<MessagePipeDispatcher> dispatcher0(new MessagePipeDispatcher()); |
| 157 scoped_refptr<MessagePipeDispatcher> dispatcher1(new MessagePipeDispatcher()); | 156 scoped_refptr<MessagePipeDispatcher> dispatcher1(new MessagePipeDispatcher()); |
| 158 | 157 |
| 159 std::pair<MojoHandle, MojoHandle> handle_pair; | 158 std::pair<MojoHandle, MojoHandle> handle_pair; |
| 160 { | 159 { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 178 return MOJO_RESULT_OK; | 177 return MOJO_RESULT_OK; |
| 179 } | 178 } |
| 180 | 179 |
| 181 // Implementation note: To properly cancel waiters and avoid other races, this | 180 // Implementation note: To properly cancel waiters and avoid other races, this |
| 182 // does not transfer dispatchers from one handle to another, even when sending a | 181 // does not transfer dispatchers from one handle to another, even when sending a |
| 183 // message in-process. Instead, it must transfer the "contents" of the | 182 // message in-process. Instead, it must transfer the "contents" of the |
| 184 // dispatcher to a new dispatcher, and then close the old dispatcher. If this | 183 // dispatcher to a new dispatcher, and then close the old dispatcher. If this |
| 185 // isn't done, in the in-process case, calls on the old handle may complete | 184 // isn't done, in the in-process case, calls on the old handle may complete |
| 186 // after the the message has been received and a new handle created (and | 185 // after the the message has been received and a new handle created (and |
| 187 // possibly even after calls have been made on the new handle). | 186 // possibly even after calls have been made on the new handle). |
| 188 MojoResult CoreImpl::WriteMessage(MojoHandle message_pipe_handle, | 187 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle, |
| 189 const void* bytes, | 188 const void* bytes, |
| 190 uint32_t num_bytes, | 189 uint32_t num_bytes, |
| 191 const MojoHandle* handles, | 190 const MojoHandle* handles, |
| 192 uint32_t num_handles, | 191 uint32_t num_handles, |
| 193 MojoWriteMessageFlags flags) { | 192 MojoWriteMessageFlags flags) { |
| 194 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); | 193 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); |
| 195 if (!dispatcher.get()) | 194 if (!dispatcher.get()) |
| 196 return MOJO_RESULT_INVALID_ARGUMENT; | 195 return MOJO_RESULT_INVALID_ARGUMENT; |
| 197 | 196 |
| 198 // Easy case: not sending any handles. | 197 // Easy case: not sending any handles. |
| 199 if (num_handles == 0) | 198 if (num_handles == 0) |
| 200 return dispatcher->WriteMessage(bytes, num_bytes, NULL, flags); | 199 return dispatcher->WriteMessage(bytes, num_bytes, NULL, flags); |
| 201 | 200 |
| 202 // We have to handle |handles| here, since we have to mark them busy in the | 201 // We have to handle |handles| here, since we have to mark them busy in the |
| 203 // global handle table. We can't delegate this to the dispatcher, since the | 202 // global handle table. We can't delegate this to the dispatcher, since the |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 base::AutoLock locker(handle_table_lock_); | 241 base::AutoLock locker(handle_table_lock_); |
| 243 if (rv == MOJO_RESULT_OK) | 242 if (rv == MOJO_RESULT_OK) |
| 244 handle_table_.RemoveBusyHandles(handles, num_handles); | 243 handle_table_.RemoveBusyHandles(handles, num_handles); |
| 245 else | 244 else |
| 246 handle_table_.RestoreBusyHandles(handles, num_handles); | 245 handle_table_.RestoreBusyHandles(handles, num_handles); |
| 247 } | 246 } |
| 248 | 247 |
| 249 return rv; | 248 return rv; |
| 250 } | 249 } |
| 251 | 250 |
| 252 MojoResult CoreImpl::ReadMessage(MojoHandle message_pipe_handle, | 251 MojoResult Core::ReadMessage(MojoHandle message_pipe_handle, |
| 253 void* bytes, | 252 void* bytes, |
| 254 uint32_t* num_bytes, | 253 uint32_t* num_bytes, |
| 255 MojoHandle* handles, | 254 MojoHandle* handles, |
| 256 uint32_t* num_handles, | 255 uint32_t* num_handles, |
| 257 MojoReadMessageFlags flags) { | 256 MojoReadMessageFlags flags) { |
| 258 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); | 257 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); |
| 259 if (!dispatcher.get()) | 258 if (!dispatcher.get()) |
| 260 return MOJO_RESULT_INVALID_ARGUMENT; | 259 return MOJO_RESULT_INVALID_ARGUMENT; |
| 261 | 260 |
| 262 if (num_handles) { | 261 if (num_handles) { |
| 263 if (!VerifyUserPointer<uint32_t>(num_handles, 1)) | 262 if (!VerifyUserPointer<uint32_t>(num_handles, 1)) |
| 264 return MOJO_RESULT_INVALID_ARGUMENT; | 263 return MOJO_RESULT_INVALID_ARGUMENT; |
| 265 if (!VerifyUserPointer<MojoHandle>(handles, *num_handles)) | 264 if (!VerifyUserPointer<MojoHandle>(handles, *num_handles)) |
| 266 return MOJO_RESULT_INVALID_ARGUMENT; | 265 return MOJO_RESULT_INVALID_ARGUMENT; |
| 267 } | 266 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 291 for (size_t i = 0; i < dispatchers.size(); i++) { | 290 for (size_t i = 0; i < dispatchers.size(); i++) { |
| 292 if (dispatchers[i]) | 291 if (dispatchers[i]) |
| 293 dispatchers[i]->Close(); | 292 dispatchers[i]->Close(); |
| 294 } | 293 } |
| 295 } | 294 } |
| 296 } | 295 } |
| 297 | 296 |
| 298 return rv; | 297 return rv; |
| 299 } | 298 } |
| 300 | 299 |
| 301 MojoResult CoreImpl::CreateDataPipe(const MojoCreateDataPipeOptions* options, | 300 MojoResult Core::CreateDataPipe(const MojoCreateDataPipeOptions* options, |
| 302 MojoHandle* data_pipe_producer_handle, | 301 MojoHandle* data_pipe_producer_handle, |
| 303 MojoHandle* data_pipe_consumer_handle) { | 302 MojoHandle* data_pipe_consumer_handle) { |
| 304 if (options) { | 303 if (options) { |
| 305 // The |struct_size| field must be valid to read. | 304 // The |struct_size| field must be valid to read. |
| 306 if (!VerifyUserPointer<uint32_t>(&options->struct_size, 1)) | 305 if (!VerifyUserPointer<uint32_t>(&options->struct_size, 1)) |
| 307 return MOJO_RESULT_INVALID_ARGUMENT; | 306 return MOJO_RESULT_INVALID_ARGUMENT; |
| 308 // And then |options| must point to at least |options->struct_size| bytes. | 307 // And then |options| must point to at least |options->struct_size| bytes. |
| 309 if (!VerifyUserPointer<void>(options, options->struct_size)) | 308 if (!VerifyUserPointer<void>(options, options->struct_size)) |
| 310 return MOJO_RESULT_INVALID_ARGUMENT; | 309 return MOJO_RESULT_INVALID_ARGUMENT; |
| 311 } | 310 } |
| 312 if (!VerifyUserPointer<MojoHandle>(data_pipe_producer_handle, 1)) | 311 if (!VerifyUserPointer<MojoHandle>(data_pipe_producer_handle, 1)) |
| 313 return MOJO_RESULT_INVALID_ARGUMENT; | 312 return MOJO_RESULT_INVALID_ARGUMENT; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 341 | 340 |
| 342 scoped_refptr<DataPipe> data_pipe(new LocalDataPipe(validated_options)); | 341 scoped_refptr<DataPipe> data_pipe(new LocalDataPipe(validated_options)); |
| 343 producer_dispatcher->Init(data_pipe); | 342 producer_dispatcher->Init(data_pipe); |
| 344 consumer_dispatcher->Init(data_pipe); | 343 consumer_dispatcher->Init(data_pipe); |
| 345 | 344 |
| 346 *data_pipe_producer_handle = handle_pair.first; | 345 *data_pipe_producer_handle = handle_pair.first; |
| 347 *data_pipe_consumer_handle = handle_pair.second; | 346 *data_pipe_consumer_handle = handle_pair.second; |
| 348 return MOJO_RESULT_OK; | 347 return MOJO_RESULT_OK; |
| 349 } | 348 } |
| 350 | 349 |
| 351 MojoResult CoreImpl::WriteData(MojoHandle data_pipe_producer_handle, | 350 MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle, |
| 352 const void* elements, | 351 const void* elements, |
| 353 uint32_t* num_bytes, | 352 uint32_t* num_bytes, |
| 354 MojoWriteDataFlags flags) { | 353 MojoWriteDataFlags flags) { |
| 355 scoped_refptr<Dispatcher> dispatcher( | 354 scoped_refptr<Dispatcher> dispatcher( |
| 356 GetDispatcher(data_pipe_producer_handle)); | 355 GetDispatcher(data_pipe_producer_handle)); |
| 357 if (!dispatcher.get()) | 356 if (!dispatcher.get()) |
| 358 return MOJO_RESULT_INVALID_ARGUMENT; | 357 return MOJO_RESULT_INVALID_ARGUMENT; |
| 359 | 358 |
| 360 return dispatcher->WriteData(elements, num_bytes, flags); | 359 return dispatcher->WriteData(elements, num_bytes, flags); |
| 361 } | 360 } |
| 362 | 361 |
| 363 MojoResult CoreImpl::BeginWriteData(MojoHandle data_pipe_producer_handle, | 362 MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle, |
| 364 void** buffer, | 363 void** buffer, |
| 365 uint32_t* buffer_num_bytes, | 364 uint32_t* buffer_num_bytes, |
| 366 MojoWriteDataFlags flags) { | 365 MojoWriteDataFlags flags) { |
| 367 scoped_refptr<Dispatcher> dispatcher( | 366 scoped_refptr<Dispatcher> dispatcher( |
| 368 GetDispatcher(data_pipe_producer_handle)); | 367 GetDispatcher(data_pipe_producer_handle)); |
| 369 if (!dispatcher.get()) | 368 if (!dispatcher.get()) |
| 370 return MOJO_RESULT_INVALID_ARGUMENT; | 369 return MOJO_RESULT_INVALID_ARGUMENT; |
| 371 | 370 |
| 372 return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags); | 371 return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags); |
| 373 } | 372 } |
| 374 | 373 |
| 375 MojoResult CoreImpl::EndWriteData(MojoHandle data_pipe_producer_handle, | 374 MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle, |
| 376 uint32_t num_bytes_written) { | 375 uint32_t num_bytes_written) { |
| 377 scoped_refptr<Dispatcher> dispatcher( | 376 scoped_refptr<Dispatcher> dispatcher( |
| 378 GetDispatcher(data_pipe_producer_handle)); | 377 GetDispatcher(data_pipe_producer_handle)); |
| 379 if (!dispatcher.get()) | 378 if (!dispatcher.get()) |
| 380 return MOJO_RESULT_INVALID_ARGUMENT; | 379 return MOJO_RESULT_INVALID_ARGUMENT; |
| 381 | 380 |
| 382 return dispatcher->EndWriteData(num_bytes_written); | 381 return dispatcher->EndWriteData(num_bytes_written); |
| 383 } | 382 } |
| 384 | 383 |
| 385 MojoResult CoreImpl::ReadData(MojoHandle data_pipe_consumer_handle, | 384 MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle, |
| 386 void* elements, | 385 void* elements, |
| 387 uint32_t* num_bytes, | 386 uint32_t* num_bytes, |
| 388 MojoReadDataFlags flags) { | 387 MojoReadDataFlags flags) { |
| 389 scoped_refptr<Dispatcher> dispatcher( | 388 scoped_refptr<Dispatcher> dispatcher( |
| 390 GetDispatcher(data_pipe_consumer_handle)); | 389 GetDispatcher(data_pipe_consumer_handle)); |
| 391 if (!dispatcher.get()) | 390 if (!dispatcher.get()) |
| 392 return MOJO_RESULT_INVALID_ARGUMENT; | 391 return MOJO_RESULT_INVALID_ARGUMENT; |
| 393 | 392 |
| 394 return dispatcher->ReadData(elements, num_bytes, flags); | 393 return dispatcher->ReadData(elements, num_bytes, flags); |
| 395 } | 394 } |
| 396 | 395 |
| 397 MojoResult CoreImpl::BeginReadData(MojoHandle data_pipe_consumer_handle, | 396 MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle, |
| 398 const void** buffer, | 397 const void** buffer, |
| 399 uint32_t* buffer_num_bytes, | 398 uint32_t* buffer_num_bytes, |
| 400 MojoReadDataFlags flags) { | 399 MojoReadDataFlags flags) { |
| 401 scoped_refptr<Dispatcher> dispatcher( | 400 scoped_refptr<Dispatcher> dispatcher( |
| 402 GetDispatcher(data_pipe_consumer_handle)); | 401 GetDispatcher(data_pipe_consumer_handle)); |
| 403 if (!dispatcher.get()) | 402 if (!dispatcher.get()) |
| 404 return MOJO_RESULT_INVALID_ARGUMENT; | 403 return MOJO_RESULT_INVALID_ARGUMENT; |
| 405 | 404 |
| 406 return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags); | 405 return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags); |
| 407 } | 406 } |
| 408 | 407 |
| 409 MojoResult CoreImpl::EndReadData(MojoHandle data_pipe_consumer_handle, | 408 MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle, |
| 410 uint32_t num_bytes_read) { | 409 uint32_t num_bytes_read) { |
| 411 scoped_refptr<Dispatcher> dispatcher( | 410 scoped_refptr<Dispatcher> dispatcher( |
| 412 GetDispatcher(data_pipe_consumer_handle)); | 411 GetDispatcher(data_pipe_consumer_handle)); |
| 413 if (!dispatcher.get()) | 412 if (!dispatcher.get()) |
| 414 return MOJO_RESULT_INVALID_ARGUMENT; | 413 return MOJO_RESULT_INVALID_ARGUMENT; |
| 415 | 414 |
| 416 return dispatcher->EndReadData(num_bytes_read); | 415 return dispatcher->EndReadData(num_bytes_read); |
| 417 } | 416 } |
| 418 | 417 |
| 419 MojoResult CoreImpl::CreateSharedBuffer( | 418 MojoResult Core::CreateSharedBuffer( |
| 420 const MojoCreateSharedBufferOptions* options, | 419 const MojoCreateSharedBufferOptions* options, |
| 421 uint64_t num_bytes, | 420 uint64_t num_bytes, |
| 422 MojoHandle* shared_buffer_handle) { | 421 MojoHandle* shared_buffer_handle) { |
| 423 if (options) { | 422 if (options) { |
| 424 // The |struct_size| field must be valid to read. | 423 // The |struct_size| field must be valid to read. |
| 425 if (!VerifyUserPointer<uint32_t>(&options->struct_size, 1)) | 424 if (!VerifyUserPointer<uint32_t>(&options->struct_size, 1)) |
| 426 return MOJO_RESULT_INVALID_ARGUMENT; | 425 return MOJO_RESULT_INVALID_ARGUMENT; |
| 427 // And then |options| must point to at least |options->struct_size| bytes. | 426 // And then |options| must point to at least |options->struct_size| bytes. |
| 428 if (!VerifyUserPointer<void>(options, options->struct_size)) | 427 if (!VerifyUserPointer<void>(options, options->struct_size)) |
| 429 return MOJO_RESULT_INVALID_ARGUMENT; | 428 return MOJO_RESULT_INVALID_ARGUMENT; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 449 if (h == MOJO_HANDLE_INVALID) { | 448 if (h == MOJO_HANDLE_INVALID) { |
| 450 LOG(ERROR) << "Handle table full"; | 449 LOG(ERROR) << "Handle table full"; |
| 451 dispatcher->Close(); | 450 dispatcher->Close(); |
| 452 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 451 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 453 } | 452 } |
| 454 | 453 |
| 455 *shared_buffer_handle = h; | 454 *shared_buffer_handle = h; |
| 456 return MOJO_RESULT_OK; | 455 return MOJO_RESULT_OK; |
| 457 } | 456 } |
| 458 | 457 |
| 459 MojoResult CoreImpl::DuplicateBufferHandle( | 458 MojoResult Core::DuplicateBufferHandle( |
| 460 MojoHandle buffer_handle, | 459 MojoHandle buffer_handle, |
| 461 const MojoDuplicateBufferHandleOptions* options, | 460 const MojoDuplicateBufferHandleOptions* options, |
| 462 MojoHandle* new_buffer_handle) { | 461 MojoHandle* new_buffer_handle) { |
| 463 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); | 462 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); |
| 464 if (!dispatcher.get()) | 463 if (!dispatcher.get()) |
| 465 return MOJO_RESULT_INVALID_ARGUMENT; | 464 return MOJO_RESULT_INVALID_ARGUMENT; |
| 466 | 465 |
| 467 // Don't verify |options| here; that's the dispatcher's job. | 466 // Don't verify |options| here; that's the dispatcher's job. |
| 468 if (!VerifyUserPointer<MojoHandle>(new_buffer_handle, 1)) | 467 if (!VerifyUserPointer<MojoHandle>(new_buffer_handle, 1)) |
| 469 return MOJO_RESULT_INVALID_ARGUMENT; | 468 return MOJO_RESULT_INVALID_ARGUMENT; |
| 470 | 469 |
| 471 scoped_refptr<Dispatcher> new_dispatcher; | 470 scoped_refptr<Dispatcher> new_dispatcher; |
| 472 MojoResult result = dispatcher->DuplicateBufferHandle(options, | 471 MojoResult result = dispatcher->DuplicateBufferHandle(options, |
| 473 &new_dispatcher); | 472 &new_dispatcher); |
| 474 if (result != MOJO_RESULT_OK) | 473 if (result != MOJO_RESULT_OK) |
| 475 return result; | 474 return result; |
| 476 | 475 |
| 477 MojoHandle new_handle = AddDispatcher(new_dispatcher); | 476 MojoHandle new_handle = AddDispatcher(new_dispatcher); |
| 478 if (new_handle == MOJO_HANDLE_INVALID) { | 477 if (new_handle == MOJO_HANDLE_INVALID) { |
| 479 LOG(ERROR) << "Handle table full"; | 478 LOG(ERROR) << "Handle table full"; |
| 480 dispatcher->Close(); | 479 dispatcher->Close(); |
| 481 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 480 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 482 } | 481 } |
| 483 | 482 |
| 484 *new_buffer_handle = new_handle; | 483 *new_buffer_handle = new_handle; |
| 485 return MOJO_RESULT_OK; | 484 return MOJO_RESULT_OK; |
| 486 } | 485 } |
| 487 | 486 |
| 488 MojoResult CoreImpl::MapBuffer(MojoHandle buffer_handle, | 487 MojoResult Core::MapBuffer(MojoHandle buffer_handle, |
| 489 uint64_t offset, | 488 uint64_t offset, |
| 490 uint64_t num_bytes, | 489 uint64_t num_bytes, |
| 491 void** buffer, | 490 void** buffer, |
| 492 MojoMapBufferFlags flags) { | 491 MojoMapBufferFlags flags) { |
| 493 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); | 492 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); |
| 494 if (!dispatcher.get()) | 493 if (!dispatcher.get()) |
| 495 return MOJO_RESULT_INVALID_ARGUMENT; | 494 return MOJO_RESULT_INVALID_ARGUMENT; |
| 496 | 495 |
| 497 if (!VerifyUserPointer<void*>(buffer, 1)) | 496 if (!VerifyUserPointer<void*>(buffer, 1)) |
| 498 return MOJO_RESULT_INVALID_ARGUMENT; | 497 return MOJO_RESULT_INVALID_ARGUMENT; |
| 499 | 498 |
| 500 scoped_ptr<RawSharedBufferMapping> mapping; | 499 scoped_ptr<RawSharedBufferMapping> mapping; |
| 501 MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping); | 500 MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping); |
| 502 if (result != MOJO_RESULT_OK) | 501 if (result != MOJO_RESULT_OK) |
| 503 return result; | 502 return result; |
| 504 | 503 |
| 505 DCHECK(mapping); | 504 DCHECK(mapping); |
| 506 void* address = mapping->base(); | 505 void* address = mapping->base(); |
| 507 { | 506 { |
| 508 base::AutoLock locker(mapping_table_lock_); | 507 base::AutoLock locker(mapping_table_lock_); |
| 509 result = mapping_table_.AddMapping(mapping.Pass()); | 508 result = mapping_table_.AddMapping(mapping.Pass()); |
| 510 } | 509 } |
| 511 if (result != MOJO_RESULT_OK) | 510 if (result != MOJO_RESULT_OK) |
| 512 return result; | 511 return result; |
| 513 | 512 |
| 514 *buffer = address; | 513 *buffer = address; |
| 515 return MOJO_RESULT_OK; | 514 return MOJO_RESULT_OK; |
| 516 } | 515 } |
| 517 | 516 |
| 518 MojoResult CoreImpl::UnmapBuffer(void* buffer) { | 517 MojoResult Core::UnmapBuffer(void* buffer) { |
| 519 base::AutoLock locker(mapping_table_lock_); | 518 base::AutoLock locker(mapping_table_lock_); |
| 520 return mapping_table_.RemoveMapping(buffer); | 519 return mapping_table_.RemoveMapping(buffer); |
| 521 } | 520 } |
| 522 | 521 |
| 523 scoped_refptr<Dispatcher> CoreImpl::GetDispatcher(MojoHandle handle) { | 522 scoped_refptr<Dispatcher> Core::GetDispatcher(MojoHandle handle) { |
| 524 if (handle == MOJO_HANDLE_INVALID) | 523 if (handle == MOJO_HANDLE_INVALID) |
| 525 return NULL; | 524 return NULL; |
| 526 | 525 |
| 527 base::AutoLock locker(handle_table_lock_); | 526 base::AutoLock locker(handle_table_lock_); |
| 528 return handle_table_.GetDispatcher(handle); | 527 return handle_table_.GetDispatcher(handle); |
| 529 } | 528 } |
| 530 | 529 |
| 531 // Note: We allow |handles| to repeat the same handle multiple times, since | 530 // Note: We allow |handles| to repeat the same handle multiple times, since |
| 532 // different flags may be specified. | 531 // different flags may be specified. |
| 533 // TODO(vtl): This incurs a performance cost in |RemoveWaiter()|. Analyze this | 532 // TODO(vtl): This incurs a performance cost in |RemoveWaiter()|. Analyze this |
| 534 // more carefully and address it if necessary. | 533 // more carefully and address it if necessary. |
| 535 MojoResult CoreImpl::WaitManyInternal(const MojoHandle* handles, | 534 MojoResult Core::WaitManyInternal(const MojoHandle* handles, |
| 536 const MojoWaitFlags* flags, | 535 const MojoWaitFlags* flags, |
| 537 uint32_t num_handles, | 536 uint32_t num_handles, |
| 538 MojoDeadline deadline) { | 537 MojoDeadline deadline) { |
| 539 DCHECK_GT(num_handles, 0u); | 538 DCHECK_GT(num_handles, 0u); |
| 540 | 539 |
| 541 std::vector<scoped_refptr<Dispatcher> > dispatchers; | 540 std::vector<scoped_refptr<Dispatcher> > dispatchers; |
| 542 dispatchers.reserve(num_handles); | 541 dispatchers.reserve(num_handles); |
| 543 for (uint32_t i = 0; i < num_handles; i++) { | 542 for (uint32_t i = 0; i < num_handles; i++) { |
| 544 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]); | 543 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]); |
| 545 if (!dispatcher.get()) | 544 if (!dispatcher.get()) |
| 546 return MOJO_RESULT_INVALID_ARGUMENT; | 545 return MOJO_RESULT_INVALID_ARGUMENT; |
| 547 dispatchers.push_back(dispatcher); | 546 dispatchers.push_back(dispatcher); |
| 548 } | 547 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 571 // |Wait()|/|WaitMany()| call. (Only after doing this can |waiter| be | 570 // |Wait()|/|WaitMany()| call. (Only after doing this can |waiter| be |
| 572 // destroyed, but this would still be required if the waiter were in TLS.) | 571 // destroyed, but this would still be required if the waiter were in TLS.) |
| 573 for (i = 0; i < num_added; i++) | 572 for (i = 0; i < num_added; i++) |
| 574 dispatchers[i]->RemoveWaiter(&waiter); | 573 dispatchers[i]->RemoveWaiter(&waiter); |
| 575 | 574 |
| 576 return rv; | 575 return rv; |
| 577 } | 576 } |
| 578 | 577 |
| 579 } // namespace system | 578 } // namespace system |
| 580 } // namespace mojo | 579 } // namespace mojo |
| OLD | NEW |