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/isolate.h" | 7 #include "vm/isolate.h" |
8 #include "vm/thread.h" | 8 #include "vm/thread.h" |
9 #include "vm/utils.h" | 9 #include "vm/utils.h" |
10 | 10 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } else if (empty < deleted_) { | 83 } else if (empty < deleted_) { |
84 // Rehash without growing the table to flush the deleted slots out of the | 84 // Rehash without growing the table to flush the deleted slots out of the |
85 // map. | 85 // map. |
86 Rehash(capacity_); | 86 Rehash(capacity_); |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 | 90 |
91 Dart_Port PortMap::CreatePort() { | 91 Dart_Port PortMap::CreatePort() { |
92 Isolate* isolate = Isolate::Current(); | 92 Isolate* isolate = Isolate::Current(); |
| 93 Dart_Port port; |
| 94 { |
| 95 MutexLocker ml(mutex_); |
93 | 96 |
94 MutexLocker ml(mutex_); | 97 Entry entry; |
| 98 port = AllocatePort(); |
| 99 entry.port = port; |
| 100 entry.isolate = isolate; |
95 | 101 |
96 Entry entry; | 102 // Search for the first unused slot. Make use of the knowledge that here is |
97 entry.port = AllocatePort(); | 103 // currently no port with this id in the port map. |
98 entry.isolate = isolate; | 104 ASSERT(FindPort(entry.port) < 0); |
| 105 intptr_t index = entry.port % capacity_; |
| 106 Entry cur = map_[index]; |
| 107 // Stop the search at the first found unused (free or deleted) slot. |
| 108 while (cur.port != 0) { |
| 109 index = (index + 1) % capacity_; |
| 110 cur = map_[index]; |
| 111 } |
99 | 112 |
100 // Search for the first unused slot. Make use of the knowledge that here is | 113 // Insert the newly created port at the index. |
101 // currently no port with this id in the port map. | 114 ASSERT(index >= 0); |
102 ASSERT(FindPort(entry.port) < 0); | 115 ASSERT(index < capacity_); |
103 intptr_t index = entry.port % capacity_; | 116 ASSERT(map_[index].port == 0); |
104 Entry cur = map_[index]; | 117 ASSERT((map_[index].isolate == NULL) || |
105 // Stop the search at the first found unused (free or deleted) slot. | 118 (map_[index].isolate == deleted_entry_)); |
106 while (cur.port != 0) { | 119 if (map_[index].isolate == deleted_entry_) { |
107 index = (index + 1) % capacity_; | 120 // Consuming a deleted entry. |
108 cur = map_[index]; | 121 deleted_--; |
| 122 } |
| 123 map_[index] = entry; |
| 124 isolate->increment_active_ports(); |
| 125 |
| 126 // Increment number of used slots and grow if necessary. |
| 127 used_++; |
| 128 MaintainInvariants(); |
109 } | 129 } |
110 | 130 |
111 // Insert the newly created port at the index. | 131 // Notify the embedder that this port has been created. |
112 ASSERT(index >= 0); | 132 Dart_CreatePortCallback callback = isolate->create_port_callback(); |
113 ASSERT(index < capacity_); | 133 ASSERT(callback); |
114 ASSERT(map_[index].port == 0); | 134 (*callback)(isolate, port); |
115 ASSERT((map_[index].isolate == NULL) || | |
116 (map_[index].isolate == deleted_entry_)); | |
117 if (map_[index].isolate == deleted_entry_) { | |
118 // Consuming a deleted entry. | |
119 deleted_--; | |
120 } | |
121 map_[index] = entry; | |
122 isolate->increment_active_ports(); | |
123 | 135 |
124 // Increment number of used slots and grow if necessary. | 136 return port; |
125 used_++; | |
126 MaintainInvariants(); | |
127 | |
128 return entry.port; | |
129 } | 137 } |
130 | 138 |
131 | 139 |
132 void PortMap::ClosePort(Dart_Port port) { | 140 void PortMap::ClosePort(Dart_Port port) { |
133 Isolate* isolate = Isolate::Current(); | 141 Isolate* isolate = Isolate::Current(); |
134 { | 142 { |
135 MutexLocker ml(mutex_); | 143 MutexLocker ml(mutex_); |
136 intptr_t index = FindPort(port); | 144 intptr_t index = FindPort(port); |
137 if (index < 0) { | 145 if (index < 0) { |
138 return; | 146 return; |
(...skipping 16 matching lines...) Expand all Loading... |
155 // Notify the embedder that this port is closed. | 163 // Notify the embedder that this port is closed. |
156 Dart_ClosePortCallback callback = isolate->close_port_callback(); | 164 Dart_ClosePortCallback callback = isolate->close_port_callback(); |
157 ASSERT(callback); | 165 ASSERT(callback); |
158 ASSERT(port != kCloseAllPorts); | 166 ASSERT(port != kCloseAllPorts); |
159 (*callback)(isolate, port); | 167 (*callback)(isolate, port); |
160 } | 168 } |
161 | 169 |
162 | 170 |
163 void PortMap::ClosePorts() { | 171 void PortMap::ClosePorts() { |
164 Isolate* isolate = Isolate::Current(); | 172 Isolate* isolate = Isolate::Current(); |
| 173 if (isolate->active_ports() == 0) { |
| 174 return; |
| 175 } |
165 { | 176 { |
166 MutexLocker ml(mutex_); | 177 MutexLocker ml(mutex_); |
167 for (intptr_t i = 0; i < capacity_; i++) { | 178 for (intptr_t i = 0; i < capacity_; i++) { |
168 if (map_[i].isolate == isolate) { | 179 if (map_[i].isolate == isolate) { |
169 // Mark the slot as deleted. | 180 // Mark the slot as deleted. |
170 map_[i].port = 0; | 181 map_[i].port = 0; |
171 map_[i].isolate = deleted_entry_; | 182 map_[i].isolate = deleted_entry_; |
172 isolate->decrement_active_ports(); | 183 isolate->decrement_active_ports(); |
173 | 184 |
174 used_--; | 185 used_--; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); | 236 ASSERT(Utils::IsPowerOfTwo(kInitialCapacity)); |
226 map_ = new Entry[kInitialCapacity]; | 237 map_ = new Entry[kInitialCapacity]; |
227 memset(map_, 0, kInitialCapacity * sizeof(Entry)); | 238 memset(map_, 0, kInitialCapacity * sizeof(Entry)); |
228 capacity_ = kInitialCapacity; | 239 capacity_ = kInitialCapacity; |
229 used_ = 0; | 240 used_ = 0; |
230 deleted_ = 0; | 241 deleted_ = 0; |
231 } | 242 } |
232 | 243 |
233 | 244 |
234 } // namespace dart | 245 } // namespace dart |
OLD | NEW |