1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
mojo / core / ports / message_queue.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/core/ports/message_queue.h"
#include <algorithm>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "mojo/core/ports/message_filter.h"
namespace mojo {
namespace core {
namespace ports {
// Used by std::{push,pop}_heap functions
inline bool operator<(const std::unique_ptr<UserMessageEvent>& a,
const std::unique_ptr<UserMessageEvent>& b) {
return a->sequence_num() > b->sequence_num();
}
MessageQueue::MessageQueue() : MessageQueue(kInitialSequenceNum) {}
MessageQueue::MessageQueue(uint64_t next_sequence_num)
: next_sequence_num_(next_sequence_num) {
// The message queue is blocked waiting for a message with sequence number
// equal to |next_sequence_num|.
}
MessageQueue::~MessageQueue() {
#if DCHECK_IS_ON()
size_t num_leaked_ports = 0;
for (const auto& message : heap_)
num_leaked_ports += message->num_ports();
DVLOG_IF(1, num_leaked_ports > 0)
<< "Leaking " << num_leaked_ports << " ports in unreceived messages";
#endif
}
bool MessageQueue::HasNextMessage() const {
return !heap_.empty() && heap_[0]->sequence_num() == next_sequence_num_;
}
void MessageQueue::GetNextMessage(std::unique_ptr<UserMessageEvent>* message,
MessageFilter* filter) {
if (!HasNextMessage() || (filter && !filter->Match(*heap_[0]))) {
message->reset();
return;
}
std::pop_heap(heap_.begin(), heap_.end());
*message = std::move(heap_.back());
total_queued_bytes_ -= (*message)->GetSizeIfSerialized();
heap_.pop_back();
// We keep the capacity of |heap_| in check so that a large batch of incoming
// messages doesn't permanently wreck available memory. The choice of interval
// here is somewhat arbitrary.
constexpr size_t kHeapMinimumShrinkSize = 16;
constexpr size_t kHeapShrinkInterval = 512;
if (heap_.size() > kHeapMinimumShrinkSize &&
heap_.size() % kHeapShrinkInterval == 0) [[unlikely]] {
heap_.shrink_to_fit();
}
}
void MessageQueue::AcceptMessage(std::unique_ptr<UserMessageEvent> message,
bool* has_next_message) {
// TODO: Handle sequence number roll-over.
total_queued_bytes_ += message->GetSizeIfSerialized();
heap_.emplace_back(std::move(message));
std::push_heap(heap_.begin(), heap_.end());
if (!signalable_) {
*has_next_message = false;
} else {
*has_next_message = (heap_[0]->sequence_num() == next_sequence_num_);
}
}
void MessageQueue::TakeAllMessages(
std::vector<std::unique_ptr<UserMessageEvent>>* messages) {
*messages = std::move(heap_);
total_queued_bytes_ = 0;
}
void MessageQueue::MessageProcessed() {
next_sequence_num_++;
}
} // namespace ports
} // namespace core
} // namespace mojo