| 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" |
| 11 #include "mojo/edk/embedder/platform_shared_buffer.h" | 11 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 12 #include "mojo/edk/embedder/platform_support.h" | 12 #include "mojo/edk/embedder/platform_support.h" |
| 13 #include "mojo/edk/system/configuration.h" | 13 #include "mojo/edk/system/constants.h" |
| 14 #include "mojo/edk/system/data_pipe.h" | 14 #include "mojo/edk/system/data_pipe.h" |
| 15 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" | 15 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" |
| 16 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" | 16 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" |
| 17 #include "mojo/edk/system/dispatcher.h" | 17 #include "mojo/edk/system/dispatcher.h" |
| 18 #include "mojo/edk/system/handle_signals_state.h" | 18 #include "mojo/edk/system/handle_signals_state.h" |
| 19 #include "mojo/edk/system/local_data_pipe.h" | 19 #include "mojo/edk/system/local_data_pipe.h" |
| 20 #include "mojo/edk/system/memory.h" | 20 #include "mojo/edk/system/memory.h" |
| 21 #include "mojo/edk/system/message_pipe.h" | 21 #include "mojo/edk/system/message_pipe.h" |
| 22 #include "mojo/edk/system/message_pipe_dispatcher.h" | 22 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 23 #include "mojo/edk/system/shared_buffer_dispatcher.h" | 23 #include "mojo/edk/system/shared_buffer_dispatcher.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 // |Dispatcher| in dispatcher.h. | 119 // |Dispatcher| in dispatcher.h. |
| 120 return dispatcher->Close(); | 120 return dispatcher->Close(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 MojoResult Core::Wait(MojoHandle handle, | 123 MojoResult Core::Wait(MojoHandle handle, |
| 124 MojoHandleSignals signals, | 124 MojoHandleSignals signals, |
| 125 MojoDeadline deadline, | 125 MojoDeadline deadline, |
| 126 UserPointer<MojoHandleSignalsState> signals_state) { | 126 UserPointer<MojoHandleSignalsState> signals_state) { |
| 127 uint32_t unused = static_cast<uint32_t>(-1); | 127 uint32_t unused = static_cast<uint32_t>(-1); |
| 128 HandleSignalsState hss; | 128 HandleSignalsState hss; |
| 129 MojoResult rv = WaitManyInternal(&handle, &signals, 1, deadline, &unused, | 129 MojoResult rv = WaitManyInternal(&handle, |
| 130 &signals, |
| 131 1, |
| 132 deadline, |
| 133 &unused, |
| 130 signals_state.IsNull() ? nullptr : &hss); | 134 signals_state.IsNull() ? nullptr : &hss); |
| 131 if (rv != MOJO_RESULT_INVALID_ARGUMENT && !signals_state.IsNull()) | 135 if (rv != MOJO_RESULT_INVALID_ARGUMENT && !signals_state.IsNull()) |
| 132 signals_state.Put(hss); | 136 signals_state.Put(hss); |
| 133 return rv; | 137 return rv; |
| 134 } | 138 } |
| 135 | 139 |
| 136 MojoResult Core::WaitMany(UserPointer<const MojoHandle> handles, | 140 MojoResult Core::WaitMany(UserPointer<const MojoHandle> handles, |
| 137 UserPointer<const MojoHandleSignals> signals, | 141 UserPointer<const MojoHandleSignals> signals, |
| 138 uint32_t num_handles, | 142 uint32_t num_handles, |
| 139 MojoDeadline deadline, | 143 MojoDeadline deadline, |
| 140 UserPointer<uint32_t> result_index, | 144 UserPointer<uint32_t> result_index, |
| 141 UserPointer<MojoHandleSignalsState> signals_states) { | 145 UserPointer<MojoHandleSignalsState> signals_states) { |
| 142 if (num_handles < 1) | 146 if (num_handles < 1) |
| 143 return MOJO_RESULT_INVALID_ARGUMENT; | 147 return MOJO_RESULT_INVALID_ARGUMENT; |
| 144 if (num_handles > GetConfiguration().max_wait_many_num_handles) | 148 if (num_handles > kMaxWaitManyNumHandles) |
| 145 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 149 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 146 | 150 |
| 147 UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles); | 151 UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles); |
| 148 UserPointer<const MojoHandleSignals>::Reader signals_reader(signals, | 152 UserPointer<const MojoHandleSignals>::Reader signals_reader(signals, |
| 149 num_handles); | 153 num_handles); |
| 150 uint32_t index = static_cast<uint32_t>(-1); | 154 uint32_t index = static_cast<uint32_t>(-1); |
| 151 MojoResult rv; | 155 MojoResult rv; |
| 152 if (signals_states.IsNull()) { | 156 if (signals_states.IsNull()) { |
| 153 rv = WaitManyInternal(handles_reader.GetPointer(), | 157 rv = WaitManyInternal(handles_reader.GetPointer(), |
| 154 signals_reader.GetPointer(), num_handles, deadline, | 158 signals_reader.GetPointer(), |
| 155 &index, nullptr); | 159 num_handles, |
| 160 deadline, |
| 161 &index, |
| 162 nullptr); |
| 156 } else { | 163 } else { |
| 157 UserPointer<MojoHandleSignalsState>::Writer signals_states_writer( | 164 UserPointer<MojoHandleSignalsState>::Writer signals_states_writer( |
| 158 signals_states, num_handles); | 165 signals_states, num_handles); |
| 159 // Note: The |reinterpret_cast| is safe, since |HandleSignalsState| is a | 166 // Note: The |reinterpret_cast| is safe, since |HandleSignalsState| is a |
| 160 // subclass of |MojoHandleSignalsState| that doesn't add any data members. | 167 // subclass of |MojoHandleSignalsState| that doesn't add any data members. |
| 161 rv = WaitManyInternal(handles_reader.GetPointer(), | 168 rv = WaitManyInternal(handles_reader.GetPointer(), |
| 162 signals_reader.GetPointer(), num_handles, deadline, | 169 signals_reader.GetPointer(), |
| 163 &index, reinterpret_cast<HandleSignalsState*>( | 170 num_handles, |
| 164 signals_states_writer.GetPointer())); | 171 deadline, |
| 172 &index, |
| 173 reinterpret_cast<HandleSignalsState*>( |
| 174 signals_states_writer.GetPointer())); |
| 165 if (rv != MOJO_RESULT_INVALID_ARGUMENT) | 175 if (rv != MOJO_RESULT_INVALID_ARGUMENT) |
| 166 signals_states_writer.Commit(); | 176 signals_states_writer.Commit(); |
| 167 } | 177 } |
| 168 if (index != static_cast<uint32_t>(-1) && !result_index.IsNull()) | 178 if (index != static_cast<uint32_t>(-1) && !result_index.IsNull()) |
| 169 result_index.Put(index); | 179 result_index.Put(index); |
| 170 return rv; | 180 return rv; |
| 171 } | 181 } |
| 172 | 182 |
| 173 MojoResult Core::CreateMessagePipe( | 183 MojoResult Core::CreateMessagePipe( |
| 174 UserPointer<const MojoCreateMessagePipeOptions> options, | 184 UserPointer<const MojoCreateMessagePipeOptions> options, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 return dispatcher->WriteMessage(bytes, num_bytes, nullptr, flags); | 239 return dispatcher->WriteMessage(bytes, num_bytes, nullptr, flags); |
| 230 | 240 |
| 231 // We have to handle |handles| here, since we have to mark them busy in the | 241 // We have to handle |handles| here, since we have to mark them busy in the |
| 232 // global handle table. We can't delegate this to the dispatcher, since the | 242 // global handle table. We can't delegate this to the dispatcher, since the |
| 233 // handle table lock must be acquired before the dispatcher lock. | 243 // handle table lock must be acquired before the dispatcher lock. |
| 234 // | 244 // |
| 235 // (This leads to an oddity: |handles|/|num_handles| are always verified for | 245 // (This leads to an oddity: |handles|/|num_handles| are always verified for |
| 236 // validity, even for dispatchers that don't support |WriteMessage()| and will | 246 // validity, even for dispatchers that don't support |WriteMessage()| and will |
| 237 // simply return failure unconditionally. It also breaks the usual | 247 // simply return failure unconditionally. It also breaks the usual |
| 238 // left-to-right verification order of arguments.) | 248 // left-to-right verification order of arguments.) |
| 239 if (num_handles > GetConfiguration().max_message_num_handles) | 249 if (num_handles > kMaxMessageNumHandles) |
| 240 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 250 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 241 | 251 |
| 242 UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles); | 252 UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles); |
| 243 | 253 |
| 244 // We'll need to hold on to the dispatchers so that we can pass them on to | 254 // We'll need to hold on to the dispatchers so that we can pass them on to |
| 245 // |WriteMessage()| and also so that we can unlock their locks afterwards | 255 // |WriteMessage()| and also so that we can unlock their locks afterwards |
| 246 // without accessing the handle table. These can be dumb pointers, since their | 256 // without accessing the handle table. These can be dumb pointers, since their |
| 247 // entries in the handle table won't get removed (since they'll be marked as | 257 // entries in the handle table won't get removed (since they'll be marked as |
| 248 // busy). | 258 // busy). |
| 249 std::vector<DispatcherTransport> transports(num_handles); | 259 std::vector<DispatcherTransport> transports(num_handles); |
| 250 | 260 |
| 251 // When we pass handles, we have to try to take all their dispatchers' locks | 261 // When we pass handles, we have to try to take all their dispatchers' locks |
| 252 // and mark the handles as busy. If the call succeeds, we then remove the | 262 // and mark the handles as busy. If the call succeeds, we then remove the |
| 253 // handles from the handle table. | 263 // handles from the handle table. |
| 254 { | 264 { |
| 255 base::AutoLock locker(handle_table_lock_); | 265 base::AutoLock locker(handle_table_lock_); |
| 256 MojoResult result = handle_table_.MarkBusyAndStartTransport( | 266 MojoResult result = |
| 257 message_pipe_handle, handles_reader.GetPointer(), num_handles, | 267 handle_table_.MarkBusyAndStartTransport(message_pipe_handle, |
| 258 &transports); | 268 handles_reader.GetPointer(), |
| 269 num_handles, |
| 270 &transports); |
| 259 if (result != MOJO_RESULT_OK) | 271 if (result != MOJO_RESULT_OK) |
| 260 return result; | 272 return result; |
| 261 } | 273 } |
| 262 | 274 |
| 263 MojoResult rv = | 275 MojoResult rv = |
| 264 dispatcher->WriteMessage(bytes, num_bytes, &transports, flags); | 276 dispatcher->WriteMessage(bytes, num_bytes, &transports, flags); |
| 265 | 277 |
| 266 // We need to release the dispatcher locks before we take the handle table | 278 // We need to release the dispatcher locks before we take the handle table |
| 267 // lock. | 279 // lock. |
| 268 for (uint32_t i = 0; i < num_handles; i++) | 280 for (uint32_t i = 0; i < num_handles; i++) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 289 MojoReadMessageFlags flags) { | 301 MojoReadMessageFlags flags) { |
| 290 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); | 302 scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); |
| 291 if (!dispatcher.get()) | 303 if (!dispatcher.get()) |
| 292 return MOJO_RESULT_INVALID_ARGUMENT; | 304 return MOJO_RESULT_INVALID_ARGUMENT; |
| 293 | 305 |
| 294 uint32_t num_handles_value = num_handles.IsNull() ? 0 : num_handles.Get(); | 306 uint32_t num_handles_value = num_handles.IsNull() ? 0 : num_handles.Get(); |
| 295 | 307 |
| 296 MojoResult rv; | 308 MojoResult rv; |
| 297 if (num_handles_value == 0) { | 309 if (num_handles_value == 0) { |
| 298 // Easy case: won't receive any handles. | 310 // Easy case: won't receive any handles. |
| 299 rv = dispatcher->ReadMessage(bytes, num_bytes, nullptr, &num_handles_value, | 311 rv = dispatcher->ReadMessage( |
| 300 flags); | 312 bytes, num_bytes, nullptr, &num_handles_value, flags); |
| 301 } else { | 313 } else { |
| 302 DispatcherVector dispatchers; | 314 DispatcherVector dispatchers; |
| 303 rv = dispatcher->ReadMessage(bytes, num_bytes, &dispatchers, | 315 rv = dispatcher->ReadMessage( |
| 304 &num_handles_value, flags); | 316 bytes, num_bytes, &dispatchers, &num_handles_value, flags); |
| 305 if (!dispatchers.empty()) { | 317 if (!dispatchers.empty()) { |
| 306 DCHECK_EQ(rv, MOJO_RESULT_OK); | 318 DCHECK_EQ(rv, MOJO_RESULT_OK); |
| 307 DCHECK(!num_handles.IsNull()); | 319 DCHECK(!num_handles.IsNull()); |
| 308 DCHECK_LE(dispatchers.size(), static_cast<size_t>(num_handles_value)); | 320 DCHECK_LE(dispatchers.size(), static_cast<size_t>(num_handles_value)); |
| 309 | 321 |
| 310 bool success; | 322 bool success; |
| 311 UserPointer<MojoHandle>::Writer handles_writer(handles, | 323 UserPointer<MojoHandle>::Writer handles_writer(handles, |
| 312 dispatchers.size()); | 324 dispatchers.size()); |
| 313 { | 325 { |
| 314 base::AutoLock locker(handle_table_lock_); | 326 base::AutoLock locker(handle_table_lock_); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 UserPointer<const MojoCreateSharedBufferOptions> options, | 459 UserPointer<const MojoCreateSharedBufferOptions> options, |
| 448 uint64_t num_bytes, | 460 uint64_t num_bytes, |
| 449 UserPointer<MojoHandle> shared_buffer_handle) { | 461 UserPointer<MojoHandle> shared_buffer_handle) { |
| 450 MojoCreateSharedBufferOptions validated_options = {}; | 462 MojoCreateSharedBufferOptions validated_options = {}; |
| 451 MojoResult result = SharedBufferDispatcher::ValidateCreateOptions( | 463 MojoResult result = SharedBufferDispatcher::ValidateCreateOptions( |
| 452 options, &validated_options); | 464 options, &validated_options); |
| 453 if (result != MOJO_RESULT_OK) | 465 if (result != MOJO_RESULT_OK) |
| 454 return result; | 466 return result; |
| 455 | 467 |
| 456 scoped_refptr<SharedBufferDispatcher> dispatcher; | 468 scoped_refptr<SharedBufferDispatcher> dispatcher; |
| 457 result = SharedBufferDispatcher::Create(platform_support(), validated_options, | 469 result = SharedBufferDispatcher::Create( |
| 458 num_bytes, &dispatcher); | 470 platform_support(), validated_options, num_bytes, &dispatcher); |
| 459 if (result != MOJO_RESULT_OK) { | 471 if (result != MOJO_RESULT_OK) { |
| 460 DCHECK(!dispatcher.get()); | 472 DCHECK(!dispatcher.get()); |
| 461 return result; | 473 return result; |
| 462 } | 474 } |
| 463 | 475 |
| 464 MojoHandle h = AddDispatcher(dispatcher); | 476 MojoHandle h = AddDispatcher(dispatcher); |
| 465 if (h == MOJO_HANDLE_INVALID) { | 477 if (h == MOJO_HANDLE_INVALID) { |
| 466 LOG(ERROR) << "Handle table full"; | 478 LOG(ERROR) << "Handle table full"; |
| 467 dispatcher->Close(); | 479 dispatcher->Close(); |
| 468 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 480 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 if (signals_states) { | 597 if (signals_states) { |
| 586 for (; i < num_handles; i++) | 598 for (; i < num_handles; i++) |
| 587 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); | 599 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); |
| 588 } | 600 } |
| 589 | 601 |
| 590 return rv; | 602 return rv; |
| 591 } | 603 } |
| 592 | 604 |
| 593 } // namespace system | 605 } // namespace system |
| 594 } // namespace mojo | 606 } // namespace mojo |
| OLD | NEW |