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 class MessageFilter; 26 27 // An incoming message queue for a port. MessageQueue keeps track of the highest 28 // known sequence number and can indicate whether the next sequential message is 29 // available. Thus the queue enforces message ordering for the consumer without 30 // enforcing it for the producer (see AcceptMessage() below.) 31 class MessageQueue { 32 public: 33 explicit MessageQueue(); 34 explicit MessageQueue(uint64_t next_sequence_num); 35 ~MessageQueue(); 36 37 void set_signalable(bool value) { signalable_ = value; } 38 39 uint64_t next_sequence_num() const { return next_sequence_num_; } 40 41 bool HasNextMessage() const; 42 43 // Gives ownership of the message. If |filter| is non-null, the next message 44 // will only be retrieved if the filter successfully matches it. 45 void GetNextMessage(ScopedMessage* message, MessageFilter* filter); 46 47 // Takes ownership of the message. Note: Messages are ordered, so while we 48 // have added a message to the queue, we may still be waiting on a message 49 // ahead of this one before we can let any of the messages be returned by 50 // GetNextMessage. 51 // 52 // Furthermore, once has_next_message is set to true, it will remain false 53 // until GetNextMessage is called enough times to return a null message. 54 // In other words, has_next_message acts like an edge trigger. 55 // 56 void AcceptMessage(ScopedMessage message, bool* has_next_message); 57 58 // Returns all of the ports referenced by messages in this message queue. 59 void GetReferencedPorts(std::deque<PortName>* ports); 60 61 private: 62 std::vector<ScopedMessage> heap_; 63 uint64_t next_sequence_num_; 64 bool signalable_ = true; 65 66 DISALLOW_COPY_AND_ASSIGN(MessageQueue); 67 }; 68 69 } // namespace ports 70 } // namespace edk 71 } // namespace mojo 72 73 #endif // MOJO_EDK_SYSTEM_PORTS_MESSAGE_QUEUE_H_ 74