| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ports/message_queue.h" | 5 #include "mojo/edk/system/ports/message_queue.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/ports/event.h" | 10 #include "mojo/edk/system/ports/event.h" |
| 11 #include "mojo/edk/system/ports/message_filter.h" |
| 11 | 12 |
| 12 namespace mojo { | 13 namespace mojo { |
| 13 namespace edk { | 14 namespace edk { |
| 14 namespace ports { | 15 namespace ports { |
| 15 | 16 |
| 16 inline uint64_t GetSequenceNum(const ScopedMessage& message) { | 17 inline uint64_t GetSequenceNum(const ScopedMessage& message) { |
| 17 return GetEventData<UserEventData>(*message)->sequence_num; | 18 return GetEventData<UserEventData>(*message)->sequence_num; |
| 18 } | 19 } |
| 19 | 20 |
| 20 // Used by std::{push,pop}_heap functions | 21 // Used by std::{push,pop}_heap functions |
| (...skipping 16 matching lines...) Expand all Loading... |
| 37 num_leaked_ports += message->num_ports(); | 38 num_leaked_ports += message->num_ports(); |
| 38 DVLOG_IF(1, num_leaked_ports > 0) | 39 DVLOG_IF(1, num_leaked_ports > 0) |
| 39 << "Leaking " << num_leaked_ports << " ports in unreceived messages"; | 40 << "Leaking " << num_leaked_ports << " ports in unreceived messages"; |
| 40 #endif | 41 #endif |
| 41 } | 42 } |
| 42 | 43 |
| 43 bool MessageQueue::HasNextMessage() const { | 44 bool MessageQueue::HasNextMessage() const { |
| 44 return !heap_.empty() && GetSequenceNum(heap_[0]) == next_sequence_num_; | 45 return !heap_.empty() && GetSequenceNum(heap_[0]) == next_sequence_num_; |
| 45 } | 46 } |
| 46 | 47 |
| 47 void MessageQueue::GetNextMessageIf( | 48 void MessageQueue::GetNextMessage(ScopedMessage* message, |
| 48 std::function<bool(const Message&)> selector, | 49 MessageFilter* filter) { |
| 49 ScopedMessage* message) { | 50 if (!HasNextMessage() || (filter && !filter->Match(*heap_[0].get()))) { |
| 50 if (!HasNextMessage() || (selector && !selector(*heap_[0].get()))) { | |
| 51 message->reset(); | 51 message->reset(); |
| 52 return; | 52 return; |
| 53 } | 53 } |
| 54 | 54 |
| 55 std::pop_heap(heap_.begin(), heap_.end()); | 55 std::pop_heap(heap_.begin(), heap_.end()); |
| 56 *message = std::move(heap_.back()); | 56 *message = std::move(heap_.back()); |
| 57 heap_.pop_back(); | 57 heap_.pop_back(); |
| 58 | 58 |
| 59 next_sequence_num_++; | 59 next_sequence_num_++; |
| 60 } | 60 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 78 void MessageQueue::GetReferencedPorts(std::deque<PortName>* port_names) { | 78 void MessageQueue::GetReferencedPorts(std::deque<PortName>* port_names) { |
| 79 for (const auto& message : heap_) { | 79 for (const auto& message : heap_) { |
| 80 for (size_t i = 0; i < message->num_ports(); ++i) | 80 for (size_t i = 0; i < message->num_ports(); ++i) |
| 81 port_names->push_back(message->ports()[i]); | 81 port_names->push_back(message->ports()[i]); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 } // namespace ports | 85 } // namespace ports |
| 86 } // namespace edk | 86 } // namespace edk |
| 87 } // namespace mojo | 87 } // namespace mojo |
| OLD | NEW |