Home | History | Annotate | Download | only in system
      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/waiter_list.h"
      6 
      7 #include "base/logging.h"
      8 #include "mojo/system/waiter.h"
      9 
     10 namespace mojo {
     11 namespace system {
     12 
     13 WaiterList::WaiterList() {
     14 }
     15 
     16 WaiterList::~WaiterList() {
     17   DCHECK(waiters_.empty());
     18 }
     19 
     20 void WaiterList::AwakeWaitersForStateChange(MojoWaitFlags satisfied_flags,
     21                                             MojoWaitFlags satisfiable_flags) {
     22   for (WaiterInfoList::iterator it = waiters_.begin(); it != waiters_.end();
     23        ++it) {
     24     if (it->flags & satisfied_flags)
     25       it->waiter->Awake(it->wake_result);
     26     else if (!(it->flags & satisfiable_flags))
     27       it->waiter->Awake(MOJO_RESULT_FAILED_PRECONDITION);
     28   }
     29 }
     30 
     31 void WaiterList::CancelAllWaiters() {
     32   for (WaiterInfoList::iterator it = waiters_.begin(); it != waiters_.end();
     33        ++it) {
     34     it->waiter->Awake(MOJO_RESULT_CANCELLED);
     35   }
     36   waiters_.clear();
     37 }
     38 
     39 void WaiterList::AddWaiter(Waiter* waiter,
     40                            MojoWaitFlags flags,
     41                            MojoResult wake_result) {
     42   waiters_.push_back(WaiterInfo(waiter, flags, wake_result));
     43 }
     44 
     45 void WaiterList::RemoveWaiter(Waiter* waiter) {
     46   // We allow a thread to wait on the same handle multiple times simultaneously,
     47   // so we need to scan the entire list and remove all occurrences of |waiter|.
     48   for (WaiterInfoList::iterator it = waiters_.begin(); it != waiters_.end();) {
     49     WaiterInfoList::iterator maybe_delete = it;
     50     ++it;
     51     if (maybe_delete->waiter == waiter)
     52       waiters_.erase(maybe_delete);
     53   }
     54 }
     55 
     56 }  // namespace system
     57 }  // namespace mojo
     58