OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_MESSAGE_QUEUE_H_ | 5 #ifndef VM_MESSAGE_QUEUE_H_ |
6 #define VM_MESSAGE_QUEUE_H_ | 6 #define VM_MESSAGE_QUEUE_H_ |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 #include "vm/thread.h" | 9 #include "vm/thread.h" |
10 | 10 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 class PortMessage { | 13 class Message { |
14 public: | 14 public: |
| 15 typedef enum { |
| 16 kNormalPriority = 0, // Deliver message when idle. |
| 17 kOOBPriority = 1, // Deliver message asap. |
| 18 |
| 19 // Iteration. |
| 20 kFirstPriority = 0, |
| 21 kNumPriorities = 2, |
| 22 } Priority; |
| 23 |
| 24 // A port number which is never used. |
| 25 static const int kIllegalPort = 0; |
| 26 |
15 // A new message to be sent between two isolates. The data handed to this | 27 // A new message to be sent between two isolates. The data handed to this |
16 // message will be disposed by calling free() once the message object is | 28 // message will be disposed by calling free() once the message object is |
17 // being destructed (after delivery or when the receiving port is closed). | 29 // being destructed (after delivery or when the receiving port is closed). |
18 PortMessage(Dart_Port dest_port, Dart_Port reply_port, Dart_Message data) | 30 // |
| 31 // If reply_port is kIllegalPort, then there is no reply port. |
| 32 Message(Dart_Port dest_port, Dart_Port reply_port, |
| 33 uint8_t* data, Priority priority) |
19 : next_(NULL), | 34 : next_(NULL), |
20 dest_port_(dest_port), | 35 dest_port_(dest_port), |
21 reply_port_(reply_port), | 36 reply_port_(reply_port), |
22 data_(data) {} | 37 data_(data), |
23 ~PortMessage() { | 38 priority_(priority) {} |
| 39 ~Message() { |
24 free(data_); | 40 free(data_); |
25 } | 41 } |
26 | 42 |
27 Dart_Port dest_port() const { return dest_port_; } | 43 Dart_Port dest_port() const { return dest_port_; } |
28 Dart_Port reply_port() const { return reply_port_; } | 44 Dart_Port reply_port() const { return reply_port_; } |
29 Dart_Message data() const { return data_; } | 45 uint8_t* data() const { return data_; } |
| 46 Priority priority() const { return priority_; } |
30 | 47 |
31 private: | 48 private: |
32 friend class MessageQueue; | 49 friend class MessageQueue; |
33 | 50 |
34 PortMessage* next_; | 51 Message* next_; |
35 Dart_Port dest_port_; | 52 Dart_Port dest_port_; |
36 Dart_Port reply_port_; | 53 Dart_Port reply_port_; |
37 Dart_Message data_; | 54 uint8_t* data_; |
| 55 Priority priority_; |
38 | 56 |
39 DISALLOW_COPY_AND_ASSIGN(PortMessage); | 57 DISALLOW_COPY_AND_ASSIGN(Message); |
40 }; | 58 }; |
41 | 59 |
42 | 60 |
43 // There is a message queue per isolate. | 61 // There is a message queue per isolate. |
44 class MessageQueue { | 62 class MessageQueue { |
45 public: | 63 public: |
46 MessageQueue() : head_(NULL), tail_(NULL) {} | 64 MessageQueue(); |
47 ~MessageQueue(); | 65 ~MessageQueue(); |
48 | 66 |
49 void Enqueue(PortMessage* msg); | 67 void Enqueue(Message* msg); |
50 | 68 |
51 // Gets the next message from the message queue, possibly blocking | 69 // Gets the next message from the message queue, possibly blocking |
52 // if no message is available. 'millis' is a timeout in | 70 // if no message is available. 'millis' is a timeout in |
53 // milliseconds. If 'millis' is 0, then this means to block | 71 // milliseconds. If 'millis' is 0, then this means to block |
54 // indefinitely. May block if no message is available. May return | 72 // indefinitely. May block if no message is available. May return |
55 // NULL even if 'millis' is 0 due to spurious wakeups. | 73 // NULL even if 'millis' is 0 due to spurious wakeups. |
56 PortMessage* Dequeue(int64_t millis); | 74 Message* Dequeue(int64_t millis); |
| 75 |
| 76 // Gets the next message from the message queue if available. Will |
| 77 // not block. |
| 78 Message* DequeueNoWait(); |
57 | 79 |
58 void Flush(Dart_Port port); | 80 void Flush(Dart_Port port); |
59 void FlushAll(); | 81 void FlushAll(); |
60 | 82 |
61 private: | 83 private: |
62 friend class MessageQueueTestPeer; | 84 friend class MessageQueueTestPeer; |
63 | 85 |
| 86 Message* DequeueNoWaitHoldsLock(); |
| 87 |
64 Monitor monitor_; | 88 Monitor monitor_; |
65 PortMessage* head_; | 89 Message* head_[Message::kNumPriorities]; |
66 PortMessage* tail_; | 90 Message* tail_[Message::kNumPriorities]; |
67 | 91 |
68 DISALLOW_COPY_AND_ASSIGN(MessageQueue); | 92 DISALLOW_COPY_AND_ASSIGN(MessageQueue); |
69 }; | 93 }; |
70 | 94 |
71 } // namespace dart | 95 } // namespace dart |
72 | 96 |
73 #endif // VM_MESSAGE_QUEUE_H_ | 97 #endif // VM_MESSAGE_QUEUE_H_ |
OLD | NEW |