| 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/port.h" | 5 #include "vm/port.h" |
| 6 | 6 |
| 7 #include "vm/dart_api_impl.h" |
| 7 #include "vm/isolate.h" | 8 #include "vm/isolate.h" |
| 8 #include "vm/thread.h" | 9 #include "vm/thread.h" |
| 9 #include "vm/utils.h" | 10 #include "vm/utils.h" |
| 10 | 11 |
| 11 namespace dart { | 12 namespace dart { |
| 12 | 13 |
| 13 | 14 |
| 14 Mutex* PortMap::mutex_ = NULL; | 15 Mutex* PortMap::mutex_ = NULL; |
| 15 | 16 |
| 16 PortMap::Entry* PortMap::map_ = NULL; | 17 PortMap::Entry* PortMap::map_ = NULL; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 162 |
| 162 used_--; | 163 used_--; |
| 163 deleted_++; | 164 deleted_++; |
| 164 MaintainInvariants(); | 165 MaintainInvariants(); |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Notify the embedder that this port is closed. | 168 // Notify the embedder that this port is closed. |
| 168 Dart_ClosePortCallback callback = isolate->close_port_callback(); | 169 Dart_ClosePortCallback callback = isolate->close_port_callback(); |
| 169 ASSERT(callback); | 170 ASSERT(callback); |
| 170 ASSERT(port != kCloseAllPorts); | 171 ASSERT(port != kCloseAllPorts); |
| 171 (*callback)(isolate, port); | 172 (*callback)(Api::CastIsolate(isolate), port); |
| 172 } | 173 } |
| 173 | 174 |
| 174 | 175 |
| 175 void PortMap::ClosePorts() { | 176 void PortMap::ClosePorts() { |
| 176 Isolate* isolate = Isolate::Current(); | 177 Isolate* isolate = Isolate::Current(); |
| 177 { | 178 { |
| 178 MutexLocker ml(mutex_); | 179 MutexLocker ml(mutex_); |
| 179 for (intptr_t i = 0; i < capacity_; i++) { | 180 for (intptr_t i = 0; i < capacity_; i++) { |
| 180 if (map_[i].isolate == isolate) { | 181 if (map_[i].isolate == isolate) { |
| 181 // Mark the slot as deleted. | 182 // Mark the slot as deleted. |
| 182 map_[i].port = 0; | 183 map_[i].port = 0; |
| 183 map_[i].isolate = deleted_entry_; | 184 map_[i].isolate = deleted_entry_; |
| 184 isolate->decrement_num_ports(); | 185 isolate->decrement_num_ports(); |
| 185 | 186 |
| 186 used_--; | 187 used_--; |
| 187 deleted_++; | 188 deleted_++; |
| 188 } | 189 } |
| 189 } | 190 } |
| 190 MaintainInvariants(); | 191 MaintainInvariants(); |
| 191 } | 192 } |
| 192 | 193 |
| 193 // Notify the embedder that all ports are closed. | 194 // Notify the embedder that all ports are closed. |
| 194 Dart_ClosePortCallback callback = isolate->close_port_callback(); | 195 Dart_ClosePortCallback callback = isolate->close_port_callback(); |
| 195 ASSERT(callback); | 196 ASSERT(callback); |
| 196 (*callback)(isolate, kCloseAllPorts); | 197 (*callback)(Api::CastIsolate(isolate), kCloseAllPorts); |
| 197 } | 198 } |
| 198 | 199 |
| 199 | 200 |
| 200 bool PortMap::PostMessage(Dart_Port dest_port, | 201 bool PortMap::PostMessage(Dart_Port dest_port, |
| 201 Dart_Port reply_port, | 202 Dart_Port reply_port, |
| 202 Dart_Message message) { | 203 Dart_Message message) { |
| 203 mutex_->Lock(); | 204 mutex_->Lock(); |
| 204 intptr_t index = FindPort(dest_port); | 205 intptr_t index = FindPort(dest_port); |
| 205 if (index < 0) { | 206 if (index < 0) { |
| 206 free(message); | 207 free(message); |
| 207 mutex_->Unlock(); | 208 mutex_->Unlock(); |
| 208 return false; | 209 return false; |
| 209 } | 210 } |
| 210 ASSERT(index >= 0); | 211 ASSERT(index >= 0); |
| 211 ASSERT(index < capacity_); | 212 ASSERT(index < capacity_); |
| 212 Isolate* isolate = map_[index].isolate; | 213 Isolate* isolate = map_[index].isolate; |
| 213 ASSERT(map_[index].port != 0); | 214 ASSERT(map_[index].port != 0); |
| 214 ASSERT((isolate != NULL) && (isolate != deleted_entry_)); | 215 ASSERT((isolate != NULL) && (isolate != deleted_entry_)); |
| 215 | 216 |
| 216 // Delegate message delivery to the embedder. | 217 // Delegate message delivery to the embedder. |
| 217 Dart_PostMessageCallback callback = isolate->post_message_callback(); | 218 Dart_PostMessageCallback callback = isolate->post_message_callback(); |
| 218 ASSERT(callback); | 219 ASSERT(callback); |
| 219 bool result = (*callback)(isolate, dest_port, reply_port, message); | 220 bool result = |
| 221 (*callback)(Api::CastIsolate(isolate), dest_port, reply_port, message); |
| 220 | 222 |
| 221 mutex_->Unlock(); | 223 mutex_->Unlock(); |
| 222 return result; | 224 return result; |
| 223 } | 225 } |
| 224 | 226 |
| 225 | 227 |
| 226 void PortMap::InitOnce() { | 228 void PortMap::InitOnce() { |
| 227 mutex_ = new Mutex(); | 229 mutex_ = new Mutex(); |
| 228 | 230 |
| 229 static const intptr_t kInitialCapacity = 8; | 231 static const intptr_t kInitialCapacity = 8; |
| 230 // TODO(iposva): Verify whether we want to keep exponentially growing. | 232 // TODO(iposva): Verify whether we want to keep exponentially growing. |
| 231 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); | 233 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); |
| 232 map_ = new Entry[kInitialCapacity]; | 234 map_ = new Entry[kInitialCapacity]; |
| 233 memset(map_, 0, kInitialCapacity * sizeof(Entry)); | 235 memset(map_, 0, kInitialCapacity * sizeof(Entry)); |
| 234 capacity_ = kInitialCapacity; | 236 capacity_ = kInitialCapacity; |
| 235 used_ = 0; | 237 used_ = 0; |
| 236 deleted_ = 0; | 238 deleted_ = 0; |
| 237 } | 239 } |
| 238 | 240 |
| 239 | 241 |
| 240 } // namespace dart | 242 } // namespace dart |
| OLD | NEW |