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_H_ | 5 #ifndef VM_MESSAGE_H_ |
6 #define VM_MESSAGE_H_ | 6 #define VM_MESSAGE_H_ |
7 | 7 |
8 #include "vm/thread.h" | 8 #include "vm/thread.h" |
9 | 9 |
10 // Duplicated from dart_api.h to avoid including the whole header. | 10 // Duplicated from dart_api.h to avoid including the whole header. |
(...skipping 29 matching lines...) Expand all Loading... |
40 priority_(priority) {} | 40 priority_(priority) {} |
41 ~Message() { | 41 ~Message() { |
42 free(data_); | 42 free(data_); |
43 } | 43 } |
44 | 44 |
45 Dart_Port dest_port() const { return dest_port_; } | 45 Dart_Port dest_port() const { return dest_port_; } |
46 Dart_Port reply_port() const { return reply_port_; } | 46 Dart_Port reply_port() const { return reply_port_; } |
47 uint8_t* data() const { return data_; } | 47 uint8_t* data() const { return data_; } |
48 Priority priority() const { return priority_; } | 48 Priority priority() const { return priority_; } |
49 | 49 |
| 50 bool IsOOB() const { return priority_ == Message::kOOBPriority; } |
| 51 |
50 private: | 52 private: |
51 friend class MessageQueue; | 53 friend class MessageQueue; |
52 | 54 |
53 Message* next_; | 55 Message* next_; |
54 Dart_Port dest_port_; | 56 Dart_Port dest_port_; |
55 Dart_Port reply_port_; | 57 Dart_Port reply_port_; |
56 uint8_t* data_; | 58 uint8_t* data_; |
57 Priority priority_; | 59 Priority priority_; |
58 | 60 |
59 DISALLOW_COPY_AND_ASSIGN(Message); | 61 DISALLOW_COPY_AND_ASSIGN(Message); |
60 }; | 62 }; |
61 | 63 |
62 // There is a message queue per isolate. | 64 // There is a message queue per isolate. |
63 class MessageQueue { | 65 class MessageQueue { |
64 public: | 66 public: |
65 MessageQueue(); | 67 MessageQueue(); |
66 ~MessageQueue(); | 68 ~MessageQueue(); |
67 | 69 |
68 void Enqueue(Message* msg); | 70 void Enqueue(Message* msg); |
69 | 71 |
70 // Gets the next message from the message queue, possibly blocking | 72 // Gets the next message from the message queue or NULL if no |
71 // if no message is available. 'millis' is a timeout in | 73 // message is available. This function will not block. |
72 // milliseconds. If 'millis' is 0, then this means to block | 74 Message* Dequeue(); |
73 // indefinitely. May block if no message is available. May return | |
74 // NULL even if 'millis' is 0 due to spurious wakeups. | |
75 Message* Dequeue(int64_t millis); | |
76 | |
77 // Gets the next message from the message queue if available. Will | |
78 // not block. | |
79 Message* DequeueNoWait(); | |
80 | |
81 // Gets the next message of the specified priority or greater from | |
82 // the message queue if available. Will not block. | |
83 Message* DequeueNoWaitWithPriority(Message::Priority min_priority); | |
84 | 75 |
85 void Flush(Dart_Port port); | 76 void Flush(Dart_Port port); |
86 void FlushAll(); | 77 void FlushAll(); |
87 | 78 |
88 private: | 79 private: |
89 friend class MessageQueueTestPeer; | 80 friend class MessageQueueTestPeer; |
90 | 81 |
91 Message* DequeueNoWaitHoldsLock(Message::Priority min_priority); | 82 Message* head_; |
92 | 83 Message* tail_; |
93 Monitor monitor_; | |
94 Message* head_[Message::kNumPriorities]; | |
95 Message* tail_[Message::kNumPriorities]; | |
96 | 84 |
97 DISALLOW_COPY_AND_ASSIGN(MessageQueue); | 85 DISALLOW_COPY_AND_ASSIGN(MessageQueue); |
98 }; | 86 }; |
99 | 87 |
100 // A MessageHandler is an entity capable of accepting messages. | |
101 class MessageHandler { | |
102 protected: | |
103 MessageHandler(); | |
104 | |
105 // Allows subclasses to provide custom message notification. | |
106 virtual void MessageNotify(Message::Priority priority); | |
107 | |
108 public: | |
109 virtual ~MessageHandler(); | |
110 | |
111 // Allow subclasses to provide a handler name. | |
112 virtual const char* name() const; | |
113 | |
114 #if defined(DEBUG) | |
115 // Check that it is safe to access this message handler. | |
116 // | |
117 // For example, if this MessageHandler is an isolate, then it is | |
118 // only safe to access it when the MessageHandler is the current | |
119 // isolate. | |
120 virtual void CheckAccess(); | |
121 #endif | |
122 | |
123 void PostMessage(Message* message); | |
124 void ClosePort(Dart_Port port); | |
125 void CloseAllPorts(); | |
126 | |
127 // A message handler tracks how many live ports it has. | |
128 bool HasLivePorts() const { return live_ports_ > 0; } | |
129 void increment_live_ports() { | |
130 #if defined(DEBUG) | |
131 CheckAccess(); | |
132 #endif | |
133 live_ports_++; | |
134 } | |
135 void decrement_live_ports() { | |
136 #if defined(DEBUG) | |
137 CheckAccess(); | |
138 #endif | |
139 live_ports_--; | |
140 } | |
141 | |
142 // Returns true if the handler is owned by the PortMap. | |
143 // | |
144 // This is used to delete handlers when their last live port is closed. | |
145 virtual bool OwnedByPortMap() const { return false; } | |
146 | |
147 MessageQueue* queue() const { return queue_; } | |
148 | |
149 private: | |
150 intptr_t live_ports_; | |
151 MessageQueue* queue_; | |
152 }; | |
153 | |
154 } // namespace dart | 88 } // namespace dart |
155 | 89 |
156 #endif // VM_MESSAGE_H_ | 90 #endif // VM_MESSAGE_H_ |
OLD | NEW |