diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /ipc/chromium/src/mojo/core/ports/message_queue.h | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ipc/chromium/src/mojo/core/ports/message_queue.h')
-rw-r--r-- | ipc/chromium/src/mojo/core/ports/message_queue.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/ipc/chromium/src/mojo/core/ports/message_queue.h b/ipc/chromium/src/mojo/core/ports/message_queue.h new file mode 100644 index 0000000000..a9e08c761a --- /dev/null +++ b/ipc/chromium/src/mojo/core/ports/message_queue.h @@ -0,0 +1,85 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_CORE_PORTS_MESSAGE_QUEUE_H_ +#define MOJO_CORE_PORTS_MESSAGE_QUEUE_H_ + +#include <stdint.h> + +#include <limits> +#include <memory> +#include <vector> + +#include "mojo/core/ports/event.h" + +namespace mojo { +namespace core { +namespace ports { + +constexpr uint64_t kInitialSequenceNum = 1; +constexpr uint64_t kInvalidSequenceNum = std::numeric_limits<uint64_t>::max(); + +class MessageFilter; + +// An incoming message queue for a port. MessageQueue keeps track of the highest +// known sequence number and can indicate whether the next sequential message is +// available. Thus the queue enforces message ordering for the consumer without +// enforcing it for the producer (see AcceptMessage() below.) +class MessageQueue { + public: + explicit MessageQueue(); + explicit MessageQueue(uint64_t next_sequence_num); + ~MessageQueue(); + + MessageQueue(const MessageQueue&) = delete; + void operator=(const MessageQueue&) = delete; + + void set_signalable(bool value) { signalable_ = value; } + + uint64_t next_sequence_num() const { return next_sequence_num_; } + + bool HasNextMessage() const; + + // Gives ownership of the message. If |filter| is non-null, the next message + // will only be retrieved if the filter successfully matches it. + void GetNextMessage(mozilla::UniquePtr<UserMessageEvent>* message, + MessageFilter* filter); + + // Takes ownership of the message. Note: Messages are ordered, so while we + // have added a message to the queue, we may still be waiting on a message + // ahead of this one before we can let any of the messages be returned by + // GetNextMessage. + // + // Furthermore, once has_next_message is set to true, it will remain false + // until GetNextMessage is called enough times to return a null message. + // In other words, has_next_message acts like an edge trigger. + // + void AcceptMessage(mozilla::UniquePtr<UserMessageEvent> message, + bool* has_next_message); + + // Takes all messages from this queue. Used to safely destroy queued messages + // without holding any Port lock. + void TakeAllMessages( + std::vector<mozilla::UniquePtr<UserMessageEvent>>* messages); + + // The number of messages queued here, regardless of whether the next expected + // message has arrived yet. + size_t queued_message_count() const { return heap_.size(); } + + // The aggregate memory size in bytes of all messages queued here, regardless + // of whether the next expected message has arrived yet. + size_t queued_num_bytes() const { return total_queued_bytes_; } + + private: + std::vector<mozilla::UniquePtr<UserMessageEvent>> heap_; + uint64_t next_sequence_num_; + bool signalable_ = true; + size_t total_queued_bytes_ = 0; +}; + +} // namespace ports +} // namespace core +} // namespace mojo + +#endif // MOJO_CORE_PORTS_MESSAGE_QUEUE_H_ |