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