OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #ifndef VM_MESSAGE_QUEUE_H_ | |
6 #define VM_MESSAGE_QUEUE_H_ | |
7 | |
8 #include "include/dart_api.h" | |
9 #include "vm/thread.h" | |
10 | |
11 namespace dart { | |
12 | |
13 class Message { | |
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 | |
27 // A new message to be sent between two isolates. The data handed to this | |
28 // message will be disposed by calling free() once the message object is | |
29 // being destructed (after delivery or when the receiving port is closed). | |
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) | |
34 : next_(NULL), | |
35 dest_port_(dest_port), | |
36 reply_port_(reply_port), | |
37 data_(data), | |
38 priority_(priority) {} | |
39 ~Message() { | |
40 free(data_); | |
41 } | |
42 | |
43 Dart_Port dest_port() const { return dest_port_; } | |
44 Dart_Port reply_port() const { return reply_port_; } | |
45 uint8_t* data() const { return data_; } | |
46 Priority priority() const { return priority_; } | |
47 | |
48 private: | |
49 friend class MessageQueue; | |
50 | |
51 Message* next_; | |
52 Dart_Port dest_port_; | |
53 Dart_Port reply_port_; | |
54 uint8_t* data_; | |
55 Priority priority_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(Message); | |
58 }; | |
59 | |
60 | |
61 // There is a message queue per isolate. | |
62 class MessageQueue { | |
63 public: | |
64 MessageQueue(); | |
65 ~MessageQueue(); | |
66 | |
67 void Enqueue(Message* msg); | |
68 | |
69 // Gets the next message from the message queue, possibly blocking | |
70 // if no message is available. 'millis' is a timeout in | |
71 // milliseconds. If 'millis' is 0, then this means to block | |
72 // indefinitely. May block if no message is available. May return | |
73 // NULL even if 'millis' is 0 due to spurious wakeups. | |
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(); | |
79 | |
80 void Flush(Dart_Port port); | |
81 void FlushAll(); | |
82 | |
83 private: | |
84 friend class MessageQueueTestPeer; | |
85 | |
86 Message* DequeueNoWaitHoldsLock(); | |
87 | |
88 Monitor monitor_; | |
89 Message* head_[Message::kNumPriorities]; | |
90 Message* tail_[Message::kNumPriorities]; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(MessageQueue); | |
93 }; | |
94 | |
95 } // namespace dart | |
96 | |
97 #endif // VM_MESSAGE_QUEUE_H_ | |
OLD | NEW |