Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / system / core.cc
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
3 // found in the LICENSE file.
4
5 #include "mojo/system/core.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "mojo/embedder/platform_shared_buffer.h"
12 #include "mojo/embedder/simple_platform_support.h"  // TODO(vtl): Remove this.
13 #include "mojo/public/c/system/macros.h"
14 #include "mojo/system/constants.h"
15 #include "mojo/system/data_pipe.h"
16 #include "mojo/system/data_pipe_consumer_dispatcher.h"
17 #include "mojo/system/data_pipe_producer_dispatcher.h"
18 #include "mojo/system/dispatcher.h"
19 #include "mojo/system/handle_signals_state.h"
20 #include "mojo/system/local_data_pipe.h"
21 #include "mojo/system/memory.h"
22 #include "mojo/system/message_pipe.h"
23 #include "mojo/system/message_pipe_dispatcher.h"
24 #include "mojo/system/shared_buffer_dispatcher.h"
25 #include "mojo/system/waiter.h"
26
27 namespace mojo {
28 namespace system {
29
30 // Implementation notes
31 //
32 // Mojo primitives are implemented by the singleton |Core| object. Most calls
33 // are for a "primary" handle (the first argument). |Core::GetDispatcher()| is
34 // used to look up a |Dispatcher| object for a given handle. That object
35 // implements most primitives for that object. The wait primitives are not
36 // attached to objects and are implemented by |Core| itself.
37 //
38 // Some objects have multiple handles associated to them, e.g., message pipes
39 // (which have two). In such a case, there is still a |Dispatcher| (e.g.,
40 // |MessagePipeDispatcher|) for each handle, with each handle having a strong
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
43 // it did, it wouldn't be able to do anything with them due to lock order
44 // requirements -- see below).
45 //
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|
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).
50 // The |Dispatcher| then adds the |Waiter| to a |WaiterList| that's either owned
51 // by that |Dispatcher| (see |SimpleDispatcher|) or by a secondary object (e.g.,
52 // |MessagePipe|). To signal/wake a |Waiter|, the object in question -- either a
53 // |SimpleDispatcher| or a secondary object -- talks to its |WaiterList|.
54
55 // Thread-safety notes
56 //
57 // 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 // 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 // (which subclasses can use to protect their data).
62 //
63 // The lock ordering is as follows:
64 //   1. global handle table lock, global mapping table lock
65 //   2. |Dispatcher| locks
66 //   3. secondary object locks
67 //   ...
68 //   INF. |Waiter| locks
69 //
70 // Notes:
71 //    - While holding a |Dispatcher| lock, you may not unconditionally attempt
72 //      to take another |Dispatcher| lock. (This has consequences on the
73 //      concurrency semantics of |MojoWriteMessage()| when passing handles.)
74 //      Doing so would lead to deadlock.
75 //    - Locks at the "INF" level may not have any locks taken while they are
76 //      held.
77
78 Core::Core() {
79 }
80
81 Core::~Core() {
82 }
83
84 MojoHandle Core::AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher) {
85   base::AutoLock locker(handle_table_lock_);
86   return handle_table_.AddDispatcher(dispatcher);
87 }
88
89 scoped_refptr<Dispatcher> Core::GetDispatcher(MojoHandle handle) {
90   if (handle == MOJO_HANDLE_INVALID)
91     return NULL;
92
93   base::AutoLock locker(handle_table_lock_);
94   return handle_table_.GetDispatcher(handle);
95 }
96
97 MojoTimeTicks Core::GetTimeTicksNow() {
98   return base::TimeTicks::Now().ToInternalValue();
99 }
100
101 MojoResult Core::Close(MojoHandle handle) {
102   if (handle == MOJO_HANDLE_INVALID)
103     return MOJO_RESULT_INVALID_ARGUMENT;
104
105   scoped_refptr<Dispatcher> dispatcher;
106   {
107     base::AutoLock locker(handle_table_lock_);
108     MojoResult result =
109         handle_table_.GetAndRemoveDispatcher(handle, &dispatcher);
110     if (result != MOJO_RESULT_OK)
111       return result;
112   }
113
114   // The dispatcher doesn't have a say in being closed, but gets notified of it.
115   // Note: This is done outside of |handle_table_lock_|. As a result, there's a
116   // race condition that the dispatcher must handle; see the comment in
117   // |Dispatcher| in dispatcher.h.
118   return dispatcher->Close();
119 }
120
121 MojoResult Core::Wait(MojoHandle handle,
122                       MojoHandleSignals signals,
123                       MojoDeadline deadline,
124                       UserPointer<MojoHandleSignalsState> signals_state) {
125   uint32_t unused = static_cast<uint32_t>(-1);
126   HandleSignalsState hss;
127   MojoResult rv = WaitManyInternal(&handle,
128                                    &signals,
129                                    1,
130                                    deadline,
131                                    &unused,
132                                    signals_state.IsNull() ? NULL : &hss);
133   if (rv != MOJO_RESULT_INVALID_ARGUMENT && !signals_state.IsNull())
134     signals_state.Put(hss);
135   return rv;
136 }
137
138 MojoResult Core::WaitMany(UserPointer<const MojoHandle> handles,
139                           UserPointer<const MojoHandleSignals> signals,
140                           uint32_t num_handles,
141                           MojoDeadline deadline,
142                           UserPointer<uint32_t> result_index,
143                           UserPointer<MojoHandleSignalsState> signals_states) {
144   if (num_handles < 1)
145     return MOJO_RESULT_INVALID_ARGUMENT;
146   if (num_handles > kMaxWaitManyNumHandles)
147     return MOJO_RESULT_RESOURCE_EXHAUSTED;
148
149   UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles);
150   UserPointer<const MojoHandleSignals>::Reader signals_reader(signals,
151                                                               num_handles);
152   uint32_t index = static_cast<uint32_t>(-1);
153   MojoResult rv;
154   if (signals_states.IsNull()) {
155     rv = WaitManyInternal(handles_reader.GetPointer(),
156                           signals_reader.GetPointer(),
157                           num_handles,
158                           deadline,
159                           &index,
160                           NULL);
161   } else {
162     UserPointer<MojoHandleSignalsState>::Writer signals_states_writer(
163         signals_states, num_handles);
164     // Note: The |reinterpret_cast| is safe, since |HandleSignalsState| is a
165     // subclass of |MojoHandleSignalsState| that doesn't add any data members.
166     rv = WaitManyInternal(handles_reader.GetPointer(),
167                           signals_reader.GetPointer(),
168                           num_handles,
169                           deadline,
170                           &index,
171                           reinterpret_cast<HandleSignalsState*>(
172                               signals_states_writer.GetPointer()));
173     if (rv != MOJO_RESULT_INVALID_ARGUMENT)
174       signals_states_writer.Commit();
175   }
176   if (index != static_cast<uint32_t>(-1) && !result_index.IsNull())
177     result_index.Put(index);
178   return rv;
179 }
180
181 MojoResult Core::CreateMessagePipe(
182     UserPointer<const MojoCreateMessagePipeOptions> options,
183     UserPointer<MojoHandle> message_pipe_handle0,
184     UserPointer<MojoHandle> message_pipe_handle1) {
185   MojoCreateMessagePipeOptions validated_options = {};
186   MojoResult result =
187       MessagePipeDispatcher::ValidateCreateOptions(options, &validated_options);
188   if (result != MOJO_RESULT_OK)
189     return result;
190
191   scoped_refptr<MessagePipeDispatcher> dispatcher0(
192       new MessagePipeDispatcher(validated_options));
193   scoped_refptr<MessagePipeDispatcher> dispatcher1(
194       new MessagePipeDispatcher(validated_options));
195
196   std::pair<MojoHandle, MojoHandle> handle_pair;
197   {
198     base::AutoLock locker(handle_table_lock_);
199     handle_pair = handle_table_.AddDispatcherPair(dispatcher0, dispatcher1);
200   }
201   if (handle_pair.first == MOJO_HANDLE_INVALID) {
202     DCHECK_EQ(handle_pair.second, MOJO_HANDLE_INVALID);
203     LOG(ERROR) << "Handle table full";
204     dispatcher0->Close();
205     dispatcher1->Close();
206     return MOJO_RESULT_RESOURCE_EXHAUSTED;
207   }
208
209   scoped_refptr<MessagePipe> message_pipe(new MessagePipe());
210   dispatcher0->Init(message_pipe, 0);
211   dispatcher1->Init(message_pipe, 1);
212
213   message_pipe_handle0.Put(handle_pair.first);
214   message_pipe_handle1.Put(handle_pair.second);
215   return MOJO_RESULT_OK;
216 }
217
218 // Implementation note: To properly cancel waiters and avoid other races, this
219 // does not transfer dispatchers from one handle to another, even when sending a
220 // message in-process. Instead, it must transfer the "contents" of the
221 // dispatcher to a new dispatcher, and then close the old dispatcher. If this
222 // isn't done, in the in-process case, calls on the old handle may complete
223 // after the the message has been received and a new handle created (and
224 // possibly even after calls have been made on the new handle).
225 MojoResult Core::WriteMessage(MojoHandle message_pipe_handle,
226                               UserPointer<const void> bytes,
227                               uint32_t num_bytes,
228                               UserPointer<const MojoHandle> handles,
229                               uint32_t num_handles,
230                               MojoWriteMessageFlags flags) {
231   scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle));
232   if (!dispatcher)
233     return MOJO_RESULT_INVALID_ARGUMENT;
234
235   // Easy case: not sending any handles.
236   if (num_handles == 0)
237     return dispatcher->WriteMessage(bytes, num_bytes, NULL, flags);
238
239   // We have to handle |handles| here, since we have to mark them busy in the
240   // global handle table. We can't delegate this to the dispatcher, since the
241   // handle table lock must be acquired before the dispatcher lock.
242   //
243   // (This leads to an oddity: |handles|/|num_handles| are always verified for
244   // validity, even for dispatchers that don't support |WriteMessage()| and will
245   // simply return failure unconditionally. It also breaks the usual
246   // left-to-right verification order of arguments.)
247   if (num_handles > kMaxMessageNumHandles)
248     return MOJO_RESULT_RESOURCE_EXHAUSTED;
249
250   UserPointer<const MojoHandle>::Reader handles_reader(handles, num_handles);
251
252   // We'll need to hold on to the dispatchers so that we can pass them on to
253   // |WriteMessage()| and also so that we can unlock their locks afterwards
254   // without accessing the handle table. These can be dumb pointers, since their
255   // entries in the handle table won't get removed (since they'll be marked as
256   // busy).
257   std::vector<DispatcherTransport> transports(num_handles);
258
259   // When we pass handles, we have to try to take all their dispatchers' locks
260   // and mark the handles as busy. If the call succeeds, we then remove the
261   // handles from the handle table.
262   {
263     base::AutoLock locker(handle_table_lock_);
264     MojoResult result =
265         handle_table_.MarkBusyAndStartTransport(message_pipe_handle,
266                                                 handles_reader.GetPointer(),
267                                                 num_handles,
268                                                 &transports);
269     if (result != MOJO_RESULT_OK)
270       return result;
271   }
272
273   MojoResult rv =
274       dispatcher->WriteMessage(bytes, num_bytes, &transports, flags);
275
276   // We need to release the dispatcher locks before we take the handle table
277   // lock.
278   for (uint32_t i = 0; i < num_handles; i++)
279     transports[i].End();
280
281   {
282     base::AutoLock locker(handle_table_lock_);
283     if (rv == MOJO_RESULT_OK) {
284       handle_table_.RemoveBusyHandles(handles_reader.GetPointer(), num_handles);
285     } else {
286       handle_table_.RestoreBusyHandles(handles_reader.GetPointer(),
287                                        num_handles);
288     }
289   }
290
291   return rv;
292 }
293
294 MojoResult Core::ReadMessage(MojoHandle message_pipe_handle,
295                              UserPointer<void> bytes,
296                              UserPointer<uint32_t> num_bytes,
297                              UserPointer<MojoHandle> handles,
298                              UserPointer<uint32_t> num_handles,
299                              MojoReadMessageFlags flags) {
300   scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle));
301   if (!dispatcher)
302     return MOJO_RESULT_INVALID_ARGUMENT;
303
304   uint32_t num_handles_value = num_handles.IsNull() ? 0 : num_handles.Get();
305
306   MojoResult rv;
307   if (num_handles_value == 0) {
308     // Easy case: won't receive any handles.
309     rv = dispatcher->ReadMessage(
310         bytes, num_bytes, NULL, &num_handles_value, flags);
311   } else {
312     DispatcherVector dispatchers;
313     rv = dispatcher->ReadMessage(
314         bytes, num_bytes, &dispatchers, &num_handles_value, flags);
315     if (!dispatchers.empty()) {
316       DCHECK_EQ(rv, MOJO_RESULT_OK);
317       DCHECK(!num_handles.IsNull());
318       DCHECK_LE(dispatchers.size(), static_cast<size_t>(num_handles_value));
319
320       bool success;
321       UserPointer<MojoHandle>::Writer handles_writer(handles,
322                                                      dispatchers.size());
323       {
324         base::AutoLock locker(handle_table_lock_);
325         success = handle_table_.AddDispatcherVector(
326             dispatchers, handles_writer.GetPointer());
327       }
328       if (success) {
329         handles_writer.Commit();
330       } else {
331         LOG(ERROR) << "Received message with " << dispatchers.size()
332                    << " handles, but handle table full";
333         // Close dispatchers (outside the lock).
334         for (size_t i = 0; i < dispatchers.size(); i++) {
335           if (dispatchers[i])
336             dispatchers[i]->Close();
337         }
338         if (rv == MOJO_RESULT_OK)
339           rv = MOJO_RESULT_RESOURCE_EXHAUSTED;
340       }
341     }
342   }
343
344   if (!num_handles.IsNull())
345     num_handles.Put(num_handles_value);
346   return rv;
347 }
348
349 MojoResult Core::CreateDataPipe(
350     UserPointer<const MojoCreateDataPipeOptions> options,
351     UserPointer<MojoHandle> data_pipe_producer_handle,
352     UserPointer<MojoHandle> data_pipe_consumer_handle) {
353   MojoCreateDataPipeOptions validated_options = {};
354   MojoResult result =
355       DataPipe::ValidateCreateOptions(options, &validated_options);
356   if (result != MOJO_RESULT_OK)
357     return result;
358
359   scoped_refptr<DataPipeProducerDispatcher> producer_dispatcher(
360       new DataPipeProducerDispatcher());
361   scoped_refptr<DataPipeConsumerDispatcher> consumer_dispatcher(
362       new DataPipeConsumerDispatcher());
363
364   std::pair<MojoHandle, MojoHandle> handle_pair;
365   {
366     base::AutoLock locker(handle_table_lock_);
367     handle_pair = handle_table_.AddDispatcherPair(producer_dispatcher,
368                                                   consumer_dispatcher);
369   }
370   if (handle_pair.first == MOJO_HANDLE_INVALID) {
371     DCHECK_EQ(handle_pair.second, MOJO_HANDLE_INVALID);
372     LOG(ERROR) << "Handle table full";
373     producer_dispatcher->Close();
374     consumer_dispatcher->Close();
375     return MOJO_RESULT_RESOURCE_EXHAUSTED;
376   }
377   DCHECK_NE(handle_pair.second, MOJO_HANDLE_INVALID);
378
379   scoped_refptr<DataPipe> data_pipe(new LocalDataPipe(validated_options));
380   producer_dispatcher->Init(data_pipe);
381   consumer_dispatcher->Init(data_pipe);
382
383   data_pipe_producer_handle.Put(handle_pair.first);
384   data_pipe_consumer_handle.Put(handle_pair.second);
385   return MOJO_RESULT_OK;
386 }
387
388 MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle,
389                            UserPointer<const void> elements,
390                            UserPointer<uint32_t> num_bytes,
391                            MojoWriteDataFlags flags) {
392   scoped_refptr<Dispatcher> dispatcher(
393       GetDispatcher(data_pipe_producer_handle));
394   if (!dispatcher)
395     return MOJO_RESULT_INVALID_ARGUMENT;
396
397   return dispatcher->WriteData(elements, num_bytes, flags);
398 }
399
400 MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle,
401                                 UserPointer<void*> buffer,
402                                 UserPointer<uint32_t> buffer_num_bytes,
403                                 MojoWriteDataFlags flags) {
404   scoped_refptr<Dispatcher> dispatcher(
405       GetDispatcher(data_pipe_producer_handle));
406   if (!dispatcher)
407     return MOJO_RESULT_INVALID_ARGUMENT;
408
409   return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags);
410 }
411
412 MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle,
413                               uint32_t num_bytes_written) {
414   scoped_refptr<Dispatcher> dispatcher(
415       GetDispatcher(data_pipe_producer_handle));
416   if (!dispatcher)
417     return MOJO_RESULT_INVALID_ARGUMENT;
418
419   return dispatcher->EndWriteData(num_bytes_written);
420 }
421
422 MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle,
423                           UserPointer<void> elements,
424                           UserPointer<uint32_t> num_bytes,
425                           MojoReadDataFlags flags) {
426   scoped_refptr<Dispatcher> dispatcher(
427       GetDispatcher(data_pipe_consumer_handle));
428   if (!dispatcher)
429     return MOJO_RESULT_INVALID_ARGUMENT;
430
431   return dispatcher->ReadData(elements, num_bytes, flags);
432 }
433
434 MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle,
435                                UserPointer<const void*> buffer,
436                                UserPointer<uint32_t> buffer_num_bytes,
437                                MojoReadDataFlags flags) {
438   scoped_refptr<Dispatcher> dispatcher(
439       GetDispatcher(data_pipe_consumer_handle));
440   if (!dispatcher)
441     return MOJO_RESULT_INVALID_ARGUMENT;
442
443   return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags);
444 }
445
446 MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle,
447                              uint32_t num_bytes_read) {
448   scoped_refptr<Dispatcher> dispatcher(
449       GetDispatcher(data_pipe_consumer_handle));
450   if (!dispatcher)
451     return MOJO_RESULT_INVALID_ARGUMENT;
452
453   return dispatcher->EndReadData(num_bytes_read);
454 }
455
456 MojoResult Core::CreateSharedBuffer(
457     UserPointer<const MojoCreateSharedBufferOptions> options,
458     uint64_t num_bytes,
459     UserPointer<MojoHandle> shared_buffer_handle) {
460   MojoCreateSharedBufferOptions validated_options = {};
461   MojoResult result = SharedBufferDispatcher::ValidateCreateOptions(
462       options, &validated_options);
463   if (result != MOJO_RESULT_OK)
464     return result;
465
466   // TODO(vtl): |Core| should have a |PlatformSupport| passed in at creation
467   // time, and we should use that instead.
468   embedder::SimplePlatformSupport platform_support;
469   scoped_refptr<SharedBufferDispatcher> dispatcher;
470   result = SharedBufferDispatcher::Create(
471       &platform_support, validated_options, num_bytes, &dispatcher);
472   if (result != MOJO_RESULT_OK) {
473     DCHECK(!dispatcher);
474     return result;
475   }
476
477   MojoHandle h = AddDispatcher(dispatcher);
478   if (h == MOJO_HANDLE_INVALID) {
479     LOG(ERROR) << "Handle table full";
480     dispatcher->Close();
481     return MOJO_RESULT_RESOURCE_EXHAUSTED;
482   }
483
484   shared_buffer_handle.Put(h);
485   return MOJO_RESULT_OK;
486 }
487
488 MojoResult Core::DuplicateBufferHandle(
489     MojoHandle buffer_handle,
490     UserPointer<const MojoDuplicateBufferHandleOptions> options,
491     UserPointer<MojoHandle> new_buffer_handle) {
492   scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle));
493   if (!dispatcher)
494     return MOJO_RESULT_INVALID_ARGUMENT;
495
496   // Don't verify |options| here; that's the dispatcher's job.
497   scoped_refptr<Dispatcher> new_dispatcher;
498   MojoResult result =
499       dispatcher->DuplicateBufferHandle(options, &new_dispatcher);
500   if (result != MOJO_RESULT_OK)
501     return result;
502
503   MojoHandle new_handle = AddDispatcher(new_dispatcher);
504   if (new_handle == MOJO_HANDLE_INVALID) {
505     LOG(ERROR) << "Handle table full";
506     dispatcher->Close();
507     return MOJO_RESULT_RESOURCE_EXHAUSTED;
508   }
509
510   new_buffer_handle.Put(new_handle);
511   return MOJO_RESULT_OK;
512 }
513
514 MojoResult Core::MapBuffer(MojoHandle buffer_handle,
515                            uint64_t offset,
516                            uint64_t num_bytes,
517                            UserPointer<void*> buffer,
518                            MojoMapBufferFlags flags) {
519   scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle));
520   if (!dispatcher)
521     return MOJO_RESULT_INVALID_ARGUMENT;
522
523   scoped_ptr<embedder::PlatformSharedBufferMapping> mapping;
524   MojoResult result = dispatcher->MapBuffer(offset, num_bytes, flags, &mapping);
525   if (result != MOJO_RESULT_OK)
526     return result;
527
528   DCHECK(mapping);
529   void* address = mapping->GetBase();
530   {
531     base::AutoLock locker(mapping_table_lock_);
532     result = mapping_table_.AddMapping(mapping.Pass());
533   }
534   if (result != MOJO_RESULT_OK)
535     return result;
536
537   buffer.Put(address);
538   return MOJO_RESULT_OK;
539 }
540
541 MojoResult Core::UnmapBuffer(UserPointer<void> buffer) {
542   base::AutoLock locker(mapping_table_lock_);
543   return mapping_table_.RemoveMapping(buffer.GetPointerValue());
544 }
545
546 // Note: We allow |handles| to repeat the same handle multiple times, since
547 // different flags may be specified.
548 // TODO(vtl): This incurs a performance cost in |RemoveWaiter()|. Analyze this
549 // more carefully and address it if necessary.
550 MojoResult Core::WaitManyInternal(const MojoHandle* handles,
551                                   const MojoHandleSignals* signals,
552                                   uint32_t num_handles,
553                                   MojoDeadline deadline,
554                                   uint32_t* result_index,
555                                   HandleSignalsState* signals_states) {
556   DCHECK_GT(num_handles, 0u);
557   DCHECK_EQ(*result_index, static_cast<uint32_t>(-1));
558
559   DispatcherVector dispatchers;
560   dispatchers.reserve(num_handles);
561   for (uint32_t i = 0; i < num_handles; i++) {
562     scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handles[i]);
563     if (!dispatcher) {
564       *result_index = i;
565       return MOJO_RESULT_INVALID_ARGUMENT;
566     }
567     dispatchers.push_back(dispatcher);
568   }
569
570   // TODO(vtl): Should make the waiter live (permanently) in TLS.
571   Waiter waiter;
572   waiter.Init();
573
574   uint32_t i;
575   MojoResult rv = MOJO_RESULT_OK;
576   for (i = 0; i < num_handles; i++) {
577     rv = dispatchers[i]->AddWaiter(
578         &waiter, signals[i], i, signals_states ? &signals_states[i] : NULL);
579     if (rv != MOJO_RESULT_OK) {
580       *result_index = i;
581       break;
582     }
583   }
584   uint32_t num_added = i;
585
586   if (rv == MOJO_RESULT_ALREADY_EXISTS)
587     rv = MOJO_RESULT_OK;  // The i-th one is already "triggered".
588   else if (rv == MOJO_RESULT_OK)
589     rv = waiter.Wait(deadline, result_index);
590
591   // Make sure no other dispatchers try to wake |waiter| for the current
592   // |Wait()|/|WaitMany()| call. (Only after doing this can |waiter| be
593   // destroyed, but this would still be required if the waiter were in TLS.)
594   for (i = 0; i < num_added; i++) {
595     dispatchers[i]->RemoveWaiter(&waiter,
596                                  signals_states ? &signals_states[i] : NULL);
597   }
598   if (signals_states) {
599     for (; i < num_handles; i++)
600       signals_states[i] = dispatchers[i]->GetHandleSignalsState();
601   }
602
603   return rv;
604 }
605
606 }  // namespace system
607 }  // namespace mojo