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 #include "vm/message.h" | 5 #include "vm/message.h" |
6 | 6 |
7 #include "vm/dart_entry.h" | 7 #include "vm/dart_entry.h" |
8 #include "vm/json_stream.h" | 8 #include "vm/json_stream.h" |
9 #include "vm/object.h" | 9 #include "vm/object.h" |
10 #include "vm/port.h" | 10 #include "vm/port.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 intptr_t Message::Id() const { | 24 intptr_t Message::Id() const { |
25 // Messages are allocated on the C heap. Use the raw address as the id. | 25 // Messages are allocated on the C heap. Use the raw address as the id. |
26 return reinterpret_cast<intptr_t>(this); | 26 return reinterpret_cast<intptr_t>(this); |
27 } | 27 } |
28 | 28 |
29 const char* Message::PriorityAsString(Priority priority) { | 29 const char* Message::PriorityAsString(Priority priority) { |
30 switch (priority) { | 30 switch (priority) { |
31 case kNormalPriority: | 31 case kNormalPriority: |
32 return "Normal"; | 32 return "Normal"; |
33 break; | 33 break; |
34 case kOOBPriority: | 34 case kOOBPriority: |
35 return "OOB"; | 35 return "OOB"; |
36 break; | 36 break; |
37 default: | 37 default: |
38 UNIMPLEMENTED(); | 38 UNIMPLEMENTED(); |
39 return NULL; | 39 return NULL; |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 | 43 |
44 MessageQueue::MessageQueue() { | 44 MessageQueue::MessageQueue() { |
45 head_ = NULL; | 45 head_ = NULL; |
46 tail_ = NULL; | 46 tail_ = NULL; |
(...skipping 11 matching lines...) Expand all Loading... |
58 // Make sure messages are not reused. | 58 // Make sure messages are not reused. |
59 ASSERT(msg->next_ == NULL); | 59 ASSERT(msg->next_ == NULL); |
60 if (head_ == NULL) { | 60 if (head_ == NULL) { |
61 // Only element in the queue. | 61 // Only element in the queue. |
62 ASSERT(tail_ == NULL); | 62 ASSERT(tail_ == NULL); |
63 head_ = msg; | 63 head_ = msg; |
64 tail_ = msg; | 64 tail_ = msg; |
65 } else { | 65 } else { |
66 ASSERT(tail_ != NULL); | 66 ASSERT(tail_ != NULL); |
67 if (!before_events) { | 67 if (!before_events) { |
68 // Append at the tail. | 68 // Append at the tail. |
69 tail_->next_ = msg; | 69 tail_->next_ = msg; |
70 tail_ = msg; | 70 tail_ = msg; |
71 } else { | 71 } else { |
72 ASSERT(msg->dest_port() == Message::kIllegalPort); | 72 ASSERT(msg->dest_port() == Message::kIllegalPort); |
73 if (head_->dest_port() != Message::kIllegalPort) { | 73 if (head_->dest_port() != Message::kIllegalPort) { |
74 msg->next_ = head_; | 74 msg->next_ = head_; |
75 head_ = msg; | 75 head_ = msg; |
76 } else { | 76 } else { |
77 Message* cur = head_; | 77 Message* cur = head_; |
78 while (cur->next_ != NULL) { | 78 while (cur->next_ != NULL) { |
79 if (cur->next_->dest_port() != Message::kIllegalPort) { | 79 if (cur->next_->dest_port() != Message::kIllegalPort) { |
80 // Splice in the new message at the break. | 80 // Splice in the new message at the break. |
(...skipping 18 matching lines...) Expand all Loading... |
99 Message* MessageQueue::Dequeue() { | 99 Message* MessageQueue::Dequeue() { |
100 Message* result = head_; | 100 Message* result = head_; |
101 if (result != NULL) { | 101 if (result != NULL) { |
102 head_ = result->next_; | 102 head_ = result->next_; |
103 // The following update to tail_ is not strictly needed. | 103 // The following update to tail_ is not strictly needed. |
104 if (head_ == NULL) { | 104 if (head_ == NULL) { |
105 tail_ = NULL; | 105 tail_ = NULL; |
106 } | 106 } |
107 #if defined(DEBUG) | 107 #if defined(DEBUG) |
108 result->next_ = result; // Make sure to trigger ASSERT in Enqueue. | 108 result->next_ = result; // Make sure to trigger ASSERT in Enqueue. |
109 #endif // DEBUG | 109 #endif // DEBUG |
110 return result; | 110 return result; |
111 } | 111 } |
112 return NULL; | 112 return NULL; |
113 } | 113 } |
114 | 114 |
115 | 115 |
116 void MessageQueue::Clear() { | 116 void MessageQueue::Clear() { |
117 Message* cur = head_; | 117 Message* cur = head_; |
118 head_ = NULL; | 118 head_ = NULL; |
119 tail_ = NULL; | 119 tail_ = NULL; |
120 while (cur != NULL) { | 120 while (cur != NULL) { |
121 Message* next = cur->next_; | 121 Message* next = cur->next_; |
122 if (cur->RedirectToDeliveryFailurePort()) { | 122 if (cur->RedirectToDeliveryFailurePort()) { |
123 PortMap::PostMessage(cur); | 123 PortMap::PostMessage(cur); |
124 } else { | 124 } else { |
125 delete cur; | 125 delete cur; |
126 } | 126 } |
127 cur = next; | 127 cur = next; |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 | 131 |
132 MessageQueue::Iterator::Iterator(const MessageQueue* queue) | 132 MessageQueue::Iterator::Iterator(const MessageQueue* queue) : next_(NULL) { |
133 : next_(NULL) { | |
134 Reset(queue); | 133 Reset(queue); |
135 } | 134 } |
136 | 135 |
137 | 136 |
138 MessageQueue::Iterator::~Iterator() { | 137 MessageQueue::Iterator::~Iterator() {} |
139 } | |
140 | 138 |
141 void MessageQueue::Iterator::Reset(const MessageQueue* queue) { | 139 void MessageQueue::Iterator::Reset(const MessageQueue* queue) { |
142 ASSERT(queue != NULL); | 140 ASSERT(queue != NULL); |
143 next_ = queue->head_; | 141 next_ = queue->head_; |
144 } | 142 } |
145 | 143 |
146 // returns false when there are no more messages left. | 144 // returns false when there are no more messages left. |
147 bool MessageQueue::Iterator::HasNext() { | 145 bool MessageQueue::Iterator::HasNext() { |
148 return next_ != NULL; | 146 return next_ != NULL; |
149 } | 147 } |
(...skipping 14 matching lines...) Expand all Loading... |
164 length++; | 162 length++; |
165 } | 163 } |
166 return length; | 164 return length; |
167 } | 165 } |
168 | 166 |
169 | 167 |
170 Message* MessageQueue::FindMessageById(intptr_t id) { | 168 Message* MessageQueue::FindMessageById(intptr_t id) { |
171 MessageQueue::Iterator it(this); | 169 MessageQueue::Iterator it(this); |
172 while (it.HasNext()) { | 170 while (it.HasNext()) { |
173 Message* current = it.Next(); | 171 Message* current = it.Next(); |
174 ASSERT(current != NULL); | 172 ASSERT(current != NULL); |
175 if (current->Id() == id) { | 173 if (current->Id() == id) { |
176 return current; | 174 return current; |
177 } | 175 } |
178 } | 176 } |
179 return NULL; | 177 return NULL; |
180 } | 178 } |
181 | 179 |
182 | 180 |
183 void MessageQueue::PrintJSON(JSONStream* stream) { | 181 void MessageQueue::PrintJSON(JSONStream* stream) { |
184 #ifndef PRODUCT | 182 #ifndef PRODUCT |
185 if (!FLAG_support_service) { | 183 if (!FLAG_support_service) { |
186 return; | 184 return; |
187 } | 185 } |
188 JSONArray messages(stream); | 186 JSONArray messages(stream); |
189 | 187 |
190 Object& msg_handler = Object::Handle(); | 188 Object& msg_handler = Object::Handle(); |
191 | 189 |
192 MessageQueue::Iterator it(this); | 190 MessageQueue::Iterator it(this); |
193 intptr_t depth = 0; | 191 intptr_t depth = 0; |
194 while (it.HasNext()) { | 192 while (it.HasNext()) { |
195 Message* current = it.Next(); | 193 Message* current = it.Next(); |
196 JSONObject message(&messages); | 194 JSONObject message(&messages); |
197 message.AddProperty("type", "Message"); | 195 message.AddProperty("type", "Message"); |
198 message.AddPropertyF("name", "Isolate Message (%" Px ")", current->Id()); | 196 message.AddPropertyF("name", "Isolate Message (%" Px ")", current->Id()); |
199 message.AddPropertyF("messageObjectId", "messages/%" Px "", | 197 message.AddPropertyF("messageObjectId", "messages/%" Px "", current->Id()); |
200 current->Id()); | |
201 message.AddProperty("size", current->len()); | 198 message.AddProperty("size", current->len()); |
202 message.AddProperty("index", depth++); | 199 message.AddProperty("index", depth++); |
203 message.AddPropertyF("_destinationPort", "%" Pd64 "", | 200 message.AddPropertyF("_destinationPort", "%" Pd64 "", |
204 static_cast<int64_t>(current->dest_port())); | 201 static_cast<int64_t>(current->dest_port())); |
205 message.AddProperty("_priority", | 202 message.AddProperty("_priority", |
206 Message::PriorityAsString(current->priority())); | 203 Message::PriorityAsString(current->priority())); |
207 // TODO(johnmccutchan): Move port -> handler map out of Dart and into the | 204 // TODO(johnmccutchan): Move port -> handler map out of Dart and into the |
208 // VM, that way we can lookup the handler without invoking Dart code. | 205 // VM, that way we can lookup the handler without invoking Dart code. |
209 msg_handler = DartLibraryCalls::LookupHandler(current->dest_port()); | 206 msg_handler = DartLibraryCalls::LookupHandler(current->dest_port()); |
210 if (msg_handler.IsClosure()) { | 207 if (msg_handler.IsClosure()) { |
211 // Grab function from closure. | 208 // Grab function from closure. |
212 msg_handler = Closure::Cast(msg_handler).function(); | 209 msg_handler = Closure::Cast(msg_handler).function(); |
213 } | 210 } |
214 if (msg_handler.IsFunction()) { | 211 if (msg_handler.IsFunction()) { |
215 const Function& function = Function::Cast(msg_handler); | 212 const Function& function = Function::Cast(msg_handler); |
216 message.AddProperty("handler", function); | 213 message.AddProperty("handler", function); |
217 | 214 |
218 const Script& script = Script::Handle(function.script()); | 215 const Script& script = Script::Handle(function.script()); |
219 if (!script.IsNull()) { | 216 if (!script.IsNull()) { |
220 message.AddLocation(script, function.token_pos(), | 217 message.AddLocation(script, function.token_pos(), |
221 function.end_token_pos()); | 218 function.end_token_pos()); |
222 } | 219 } |
223 } | 220 } |
224 } | 221 } |
225 #endif // !PRODUCT | 222 #endif // !PRODUCT |
226 } | 223 } |
227 | 224 |
228 } // namespace dart | 225 } // namespace dart |
OLD | NEW |