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/edk/system/awakable_list.h"
7 #include "base/logging.h"
8 #include "mojo/edk/system/awakable.h"
9 #include "mojo/edk/system/handle_signals_state.h"
14 AwakableList::AwakableList() {
17 AwakableList::~AwakableList() {
18 DCHECK(awakables_
.empty());
21 void AwakableList::AwakeForStateChange(const HandleSignalsState
& state
) {
22 for (AwakeInfoList::iterator it
= awakables_
.begin(); it
!= awakables_
.end();
24 if (state
.satisfies(it
->signals
))
25 it
->awakable
->Awake(MOJO_RESULT_OK
, it
->context
);
26 else if (!state
.can_satisfy(it
->signals
))
27 it
->awakable
->Awake(MOJO_RESULT_FAILED_PRECONDITION
, it
->context
);
31 void AwakableList::CancelAll() {
32 for (AwakeInfoList::iterator it
= awakables_
.begin(); it
!= awakables_
.end();
34 it
->awakable
->Awake(MOJO_RESULT_CANCELLED
, it
->context
);
39 void AwakableList::Add(Awakable
* awakable
,
40 MojoHandleSignals signals
,
42 awakables_
.push_back(AwakeInfo(awakable
, signals
, context
));
45 void AwakableList::Remove(Awakable
* awakable
) {
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 (AwakeInfoList::iterator it
= awakables_
.begin();
49 it
!= awakables_
.end();) {
50 AwakeInfoList::iterator maybe_delete
= it
;
52 if (maybe_delete
->awakable
== awakable
)
53 awakables_
.erase(maybe_delete
);