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 3 // found in the LICENSE file. 4 5 #ifndef MOJO_EDK_SYSTEM_PORTS_MESSAGE_QUEUE_H_ 6 #define MOJO_EDK_SYSTEM_PORTS_MESSAGE_QUEUE_H_ 7 8 #include <stdint.h> 9 10 #include <deque> 11 #include <functional> 12 #include <limits> 13 #include <vector> 14 15 #include "base/macros.h" 16 #include "mojo/edk/system/ports/message.h" 17 18 namespace mojo { 19 namespace edk { 20 namespace ports { 21 22 const uint64_t kInitialSequenceNum = 1; 23 const uint64_t kInvalidSequenceNum = std::numeric_limits<uint64_t>::max(); 24 25 // An incoming message queue for a port. MessageQueue keeps track of the highest 26 // known sequence number and can indicate whether the next sequential message is 27 // available. Thus the queue enforces message ordering for the consumer without 28 // enforcing it for the producer (see AcceptMessage() below.) 29 class MessageQueue { 30 public: 31 explicit MessageQueue(); 32 explicit MessageQueue(uint64_t next_sequence_num); 33 ~MessageQueue(); 34 set_signalable(bool value)35 void set_signalable(bool value) { signalable_ = value; } 36 next_sequence_num()37 uint64_t next_sequence_num() const { return next_sequence_num_; } 38 39 bool HasNextMessage() const; 40 41 // Gives ownership of the message. The selector may be null. 42 void GetNextMessageIf(std::function<bool(const Message&)> selector, 43 ScopedMessage* message); 44 45 // Takes ownership of the message. Note: Messages are ordered, so while we 46 // have added a message to the queue, we may still be waiting on a message 47 // ahead of this one before we can let any of the messages be returned by 48 // GetNextMessage. 49 // 50 // Furthermore, once has_next_message is set to true, it will remain false 51 // until GetNextMessage is called enough times to return a null message. 52 // In other words, has_next_message acts like an edge trigger. 53 // 54 void AcceptMessage(ScopedMessage message, bool* has_next_message); 55 56 // Returns all of the ports referenced by messages in this message queue. 57 void GetReferencedPorts(std::deque<PortName>* ports); 58 59 private: 60 std::vector<ScopedMessage> heap_; 61 uint64_t next_sequence_num_; 62 bool signalable_ = true; 63 64 DISALLOW_COPY_AND_ASSIGN(MessageQueue); 65 }; 66 67 } // namespace ports 68 } // namespace edk 69 } // namespace mojo 70 71 #endif // MOJO_EDK_SYSTEM_PORTS_MESSAGE_QUEUE_H_ 72