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