| 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" |
| 11 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 | 13 |
| 14 bool Message::RedirectToDeliveryFailurePort() { | 14 bool Message::RedirectToDeliveryFailurePort() { |
| 15 if (delivery_failure_port_ == kIllegalPort) { | 15 if (delivery_failure_port_ == kIllegalPort) { |
| 16 return false; | 16 return false; |
| 17 } | 17 } |
| 18 dest_port_ = delivery_failure_port_; | 18 dest_port_ = delivery_failure_port_; |
| 19 delivery_failure_port_ = kIllegalPort; | 19 delivery_failure_port_ = kIllegalPort; |
| 20 return true; | 20 return true; |
| 21 } | 21 } |
| 22 | 22 |
| 23 | |
| 24 intptr_t Message::Id() const { | 23 intptr_t Message::Id() const { |
| 25 // Messages are allocated on the C heap. Use the raw address as the id. | 24 // Messages are allocated on the C heap. Use the raw address as the id. |
| 26 return reinterpret_cast<intptr_t>(this); | 25 return reinterpret_cast<intptr_t>(this); |
| 27 } | 26 } |
| 28 | 27 |
| 29 const char* Message::PriorityAsString(Priority priority) { | 28 const char* Message::PriorityAsString(Priority priority) { |
| 30 switch (priority) { | 29 switch (priority) { |
| 31 case kNormalPriority: | 30 case kNormalPriority: |
| 32 return "Normal"; | 31 return "Normal"; |
| 33 break; | 32 break; |
| 34 case kOOBPriority: | 33 case kOOBPriority: |
| 35 return "OOB"; | 34 return "OOB"; |
| 36 break; | 35 break; |
| 37 default: | 36 default: |
| 38 UNIMPLEMENTED(); | 37 UNIMPLEMENTED(); |
| 39 return NULL; | 38 return NULL; |
| 40 } | 39 } |
| 41 } | 40 } |
| 42 | 41 |
| 43 | |
| 44 MessageQueue::MessageQueue() { | 42 MessageQueue::MessageQueue() { |
| 45 head_ = NULL; | 43 head_ = NULL; |
| 46 tail_ = NULL; | 44 tail_ = NULL; |
| 47 } | 45 } |
| 48 | 46 |
| 49 | |
| 50 MessageQueue::~MessageQueue() { | 47 MessageQueue::~MessageQueue() { |
| 51 // Ensure that all pending messages have been released. | 48 // Ensure that all pending messages have been released. |
| 52 Clear(); | 49 Clear(); |
| 53 ASSERT(head_ == NULL); | 50 ASSERT(head_ == NULL); |
| 54 } | 51 } |
| 55 | 52 |
| 56 | |
| 57 void MessageQueue::Enqueue(Message* msg, bool before_events) { | 53 void MessageQueue::Enqueue(Message* msg, bool before_events) { |
| 58 // Make sure messages are not reused. | 54 // Make sure messages are not reused. |
| 59 ASSERT(msg->next_ == NULL); | 55 ASSERT(msg->next_ == NULL); |
| 60 if (head_ == NULL) { | 56 if (head_ == NULL) { |
| 61 // Only element in the queue. | 57 // Only element in the queue. |
| 62 ASSERT(tail_ == NULL); | 58 ASSERT(tail_ == NULL); |
| 63 head_ = msg; | 59 head_ = msg; |
| 64 tail_ = msg; | 60 tail_ = msg; |
| 65 } else { | 61 } else { |
| 66 ASSERT(tail_ != NULL); | 62 ASSERT(tail_ != NULL); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 88 // the tail. | 84 // the tail. |
| 89 ASSERT(tail_ == cur); | 85 ASSERT(tail_ == cur); |
| 90 ASSERT(tail_->dest_port() == Message::kIllegalPort); | 86 ASSERT(tail_->dest_port() == Message::kIllegalPort); |
| 91 tail_->next_ = msg; | 87 tail_->next_ = msg; |
| 92 tail_ = msg; | 88 tail_ = msg; |
| 93 } | 89 } |
| 94 } | 90 } |
| 95 } | 91 } |
| 96 } | 92 } |
| 97 | 93 |
| 98 | |
| 99 Message* MessageQueue::Dequeue() { | 94 Message* MessageQueue::Dequeue() { |
| 100 Message* result = head_; | 95 Message* result = head_; |
| 101 if (result != NULL) { | 96 if (result != NULL) { |
| 102 head_ = result->next_; | 97 head_ = result->next_; |
| 103 // The following update to tail_ is not strictly needed. | 98 // The following update to tail_ is not strictly needed. |
| 104 if (head_ == NULL) { | 99 if (head_ == NULL) { |
| 105 tail_ = NULL; | 100 tail_ = NULL; |
| 106 } | 101 } |
| 107 #if defined(DEBUG) | 102 #if defined(DEBUG) |
| 108 result->next_ = result; // Make sure to trigger ASSERT in Enqueue. | 103 result->next_ = result; // Make sure to trigger ASSERT in Enqueue. |
| 109 #endif // DEBUG | 104 #endif // DEBUG |
| 110 return result; | 105 return result; |
| 111 } | 106 } |
| 112 return NULL; | 107 return NULL; |
| 113 } | 108 } |
| 114 | 109 |
| 115 | |
| 116 void MessageQueue::Clear() { | 110 void MessageQueue::Clear() { |
| 117 Message* cur = head_; | 111 Message* cur = head_; |
| 118 head_ = NULL; | 112 head_ = NULL; |
| 119 tail_ = NULL; | 113 tail_ = NULL; |
| 120 while (cur != NULL) { | 114 while (cur != NULL) { |
| 121 Message* next = cur->next_; | 115 Message* next = cur->next_; |
| 122 if (cur->RedirectToDeliveryFailurePort()) { | 116 if (cur->RedirectToDeliveryFailurePort()) { |
| 123 PortMap::PostMessage(cur); | 117 PortMap::PostMessage(cur); |
| 124 } else { | 118 } else { |
| 125 delete cur; | 119 delete cur; |
| 126 } | 120 } |
| 127 cur = next; | 121 cur = next; |
| 128 } | 122 } |
| 129 } | 123 } |
| 130 | 124 |
| 131 | |
| 132 MessageQueue::Iterator::Iterator(const MessageQueue* queue) : next_(NULL) { | 125 MessageQueue::Iterator::Iterator(const MessageQueue* queue) : next_(NULL) { |
| 133 Reset(queue); | 126 Reset(queue); |
| 134 } | 127 } |
| 135 | 128 |
| 136 | |
| 137 MessageQueue::Iterator::~Iterator() {} | 129 MessageQueue::Iterator::~Iterator() {} |
| 138 | 130 |
| 139 void MessageQueue::Iterator::Reset(const MessageQueue* queue) { | 131 void MessageQueue::Iterator::Reset(const MessageQueue* queue) { |
| 140 ASSERT(queue != NULL); | 132 ASSERT(queue != NULL); |
| 141 next_ = queue->head_; | 133 next_ = queue->head_; |
| 142 } | 134 } |
| 143 | 135 |
| 144 // returns false when there are no more messages left. | 136 // returns false when there are no more messages left. |
| 145 bool MessageQueue::Iterator::HasNext() { | 137 bool MessageQueue::Iterator::HasNext() { |
| 146 return next_ != NULL; | 138 return next_ != NULL; |
| 147 } | 139 } |
| 148 | 140 |
| 149 // Returns the current message and moves forward. | 141 // Returns the current message and moves forward. |
| 150 Message* MessageQueue::Iterator::Next() { | 142 Message* MessageQueue::Iterator::Next() { |
| 151 Message* current = next_; | 143 Message* current = next_; |
| 152 next_ = next_->next_; | 144 next_ = next_->next_; |
| 153 return current; | 145 return current; |
| 154 } | 146 } |
| 155 | 147 |
| 156 | |
| 157 intptr_t MessageQueue::Length() const { | 148 intptr_t MessageQueue::Length() const { |
| 158 MessageQueue::Iterator it(this); | 149 MessageQueue::Iterator it(this); |
| 159 intptr_t length = 0; | 150 intptr_t length = 0; |
| 160 while (it.HasNext()) { | 151 while (it.HasNext()) { |
| 161 it.Next(); | 152 it.Next(); |
| 162 length++; | 153 length++; |
| 163 } | 154 } |
| 164 return length; | 155 return length; |
| 165 } | 156 } |
| 166 | 157 |
| 167 | |
| 168 Message* MessageQueue::FindMessageById(intptr_t id) { | 158 Message* MessageQueue::FindMessageById(intptr_t id) { |
| 169 MessageQueue::Iterator it(this); | 159 MessageQueue::Iterator it(this); |
| 170 while (it.HasNext()) { | 160 while (it.HasNext()) { |
| 171 Message* current = it.Next(); | 161 Message* current = it.Next(); |
| 172 ASSERT(current != NULL); | 162 ASSERT(current != NULL); |
| 173 if (current->Id() == id) { | 163 if (current->Id() == id) { |
| 174 return current; | 164 return current; |
| 175 } | 165 } |
| 176 } | 166 } |
| 177 return NULL; | 167 return NULL; |
| 178 } | 168 } |
| 179 | 169 |
| 180 | |
| 181 void MessageQueue::PrintJSON(JSONStream* stream) { | 170 void MessageQueue::PrintJSON(JSONStream* stream) { |
| 182 #ifndef PRODUCT | 171 #ifndef PRODUCT |
| 183 if (!FLAG_support_service) { | 172 if (!FLAG_support_service) { |
| 184 return; | 173 return; |
| 185 } | 174 } |
| 186 JSONArray messages(stream); | 175 JSONArray messages(stream); |
| 187 | 176 |
| 188 Object& msg_handler = Object::Handle(); | 177 Object& msg_handler = Object::Handle(); |
| 189 | 178 |
| 190 MessageQueue::Iterator it(this); | 179 MessageQueue::Iterator it(this); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 216 if (!script.IsNull()) { | 205 if (!script.IsNull()) { |
| 217 message.AddLocation(script, function.token_pos(), | 206 message.AddLocation(script, function.token_pos(), |
| 218 function.end_token_pos()); | 207 function.end_token_pos()); |
| 219 } | 208 } |
| 220 } | 209 } |
| 221 } | 210 } |
| 222 #endif // !PRODUCT | 211 #endif // !PRODUCT |
| 223 } | 212 } |
| 224 | 213 |
| 225 } // namespace dart | 214 } // namespace dart |
| OLD | NEW |