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.
5 #include "mojo/system/waiter_list.h"
7 #include "base/logging.h"
8 #include "mojo/system/waiter.h"
13 WaiterList::WaiterList() {
16 WaiterList::~WaiterList() {
17 DCHECK(waiters_
.empty());
20 void WaiterList::AwakeWaitersForStateChange(MojoWaitFlags satisfied_flags
,
21 MojoWaitFlags satisfiable_flags
) {
22 for (WaiterInfoList::iterator it
= waiters_
.begin(); it
!= waiters_
.end();
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
);
31 void WaiterList::CancelAllWaiters() {
32 for (WaiterInfoList::iterator it
= waiters_
.begin(); it
!= waiters_
.end();
34 it
->waiter
->Awake(MOJO_RESULT_CANCELLED
);
39 void WaiterList::AddWaiter(Waiter
* waiter
,
41 MojoResult wake_result
) {
42 waiters_
.push_back(WaiterInfo(waiter
, flags
, wake_result
));
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
;
51 if (maybe_delete
->waiter
== waiter
)
52 waiters_
.erase(maybe_delete
);