| 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/edk/system/awakable_list.h" | 5 #include "mojo/edk/system/awakable_list.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "mojo/edk/system/awakable.h" | 10 #include "mojo/edk/system/awakable.h" |
| 11 #include "mojo/edk/system/handle_signals_state.h" | 11 #include "mojo/edk/system/handle_signals_state.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace system { | 14 namespace system { |
| 15 | 15 |
| 16 AwakableList::AwakableList() { | 16 AwakableList::AwakableList() {} |
| 17 } | |
| 18 | 17 |
| 19 AwakableList::~AwakableList() { | 18 AwakableList::~AwakableList() { |
| 20 DCHECK(awakables_.empty()); | 19 DCHECK(awakables_.empty()); |
| 21 } | 20 } |
| 22 | 21 |
| 23 void AwakableList::OnStateChange(const HandleSignalsState& old_state, | 22 void AwakableList::OnStateChange(const HandleSignalsState& old_state, |
| 24 const HandleSignalsState& new_state) { | 23 const HandleSignalsState& new_state) { |
| 25 // Instead of deleting elements in-place, swap them with the last element and | 24 // Instead of deleting elements in-place, swap them with the last element and |
| 26 // erase the elements from the end. | 25 // erase the elements from the end. |
| 27 auto last = awakables_.end(); | 26 auto last = awakables_.end(); |
| 28 for (AwakeInfoList::iterator it = awakables_.begin(); it != last;) { | 27 for (AwakeInfoList::iterator it = awakables_.begin(); it != last;) { |
| 29 bool keep = true; | 28 bool keep = true; |
| 30 if (new_state.satisfies(it->signals) && !old_state.satisfies(it->signals)) | 29 if (new_state.satisfies(it->signals) && !old_state.satisfies(it->signals)) { |
| 31 keep = it->awakable->Awake(MOJO_RESULT_OK, it->context); | 30 keep = it->awakable->Awake(it->context, Awakable::AwakeReason::SATISFIED, |
| 32 else if (!new_state.can_satisfy(it->signals) && | 31 new_state); |
| 33 old_state.can_satisfy(it->signals)) | 32 } else if (!new_state.can_satisfy(it->signals) && |
| 34 keep = it->awakable->Awake(MOJO_RESULT_FAILED_PRECONDITION, it->context); | 33 old_state.can_satisfy(it->signals)) { |
| 34 keep = it->awakable->Awake( |
| 35 it->context, Awakable::AwakeReason::UNSATISFIABLE, new_state); |
| 36 } |
| 35 | 37 |
| 36 if (!keep) { | 38 if (!keep) { |
| 37 --last; | 39 --last; |
| 38 std::swap(*it, *last); | 40 std::swap(*it, *last); |
| 39 } else { | 41 } else { |
| 40 ++it; | 42 ++it; |
| 41 } | 43 } |
| 42 } | 44 } |
| 43 awakables_.erase(last, awakables_.end()); | 45 awakables_.erase(last, awakables_.end()); |
| 44 } | 46 } |
| 45 | 47 |
| 46 void AwakableList::CancelAll() { | 48 void AwakableList::CancelAll() { |
| 47 for (AwakeInfoList::iterator it = awakables_.begin(); it != awakables_.end(); | 49 for (AwakeInfoList::iterator it = awakables_.begin(); it != awakables_.end(); |
| 48 ++it) { | 50 ++it) { |
| 49 it->awakable->Awake(MOJO_RESULT_CANCELLED, it->context); | 51 it->awakable->Awake(it->context, Awakable::AwakeReason::CANCELLED, |
| 52 HandleSignalsState()); |
| 50 } | 53 } |
| 51 awakables_.clear(); | 54 awakables_.clear(); |
| 52 } | 55 } |
| 53 | 56 |
| 54 void AwakableList::Add(Awakable* awakable, | 57 void AwakableList::Add(Awakable* awakable, |
| 55 MojoHandleSignals signals, | 58 MojoHandleSignals signals, |
| 56 uint64_t context) { | 59 uint64_t context) { |
| 57 awakables_.push_back(AwakeInfo(awakable, signals, context)); | 60 awakables_.push_back(AwakeInfo(awakable, signals, context)); |
| 58 } | 61 } |
| 59 | 62 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 82 std::swap(*it, *last); | 85 std::swap(*it, *last); |
| 83 } else { | 86 } else { |
| 84 ++it; | 87 ++it; |
| 85 } | 88 } |
| 86 } | 89 } |
| 87 awakables_.erase(last, awakables_.end()); | 90 awakables_.erase(last, awakables_.end()); |
| 88 } | 91 } |
| 89 | 92 |
| 90 } // namespace system | 93 } // namespace system |
| 91 } // namespace mojo | 94 } // namespace mojo |
| OLD | NEW |