Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: runtime/lib/isolate.cc

Issue 456983002: Refactor isolate startup code in preparation for making isolate spawning more truly non-blocking. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/main.cc ('k') | runtime/lib/isolate_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 "platform/assert.h" 5 #include "platform/assert.h"
6 #include "vm/bootstrap_natives.h" 6 #include "vm/bootstrap_natives.h"
7 #include "vm/class_finalizer.h" 7 #include "vm/class_finalizer.h"
8 #include "vm/dart.h" 8 #include "vm/dart.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_entry.h" 10 #include "vm/dart_entry.h"
(...skipping 20 matching lines...) Expand all
31 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); 31 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull());
32 uint64_t id = isolate->random()->NextUInt64(); 32 uint64_t id = isolate->random()->NextUInt64();
33 return Capability::New(id); 33 return Capability::New(id);
34 } 34 }
35 35
36 36
37 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_factory, 1) { 37 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_factory, 1) {
38 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); 38 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull());
39 Dart_Port port_id = 39 Dart_Port port_id =
40 PortMap::CreatePort(arguments->isolate()->message_handler()); 40 PortMap::CreatePort(arguments->isolate()->message_handler());
41 return ReceivePort::New(port_id); 41 return ReceivePort::New(port_id, false /* not control port */);
42 } 42 }
43 43
44 44
45 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_id, 1) { 45 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_id, 1) {
46 GET_NON_NULL_NATIVE_ARGUMENT(ReceivePort, port, arguments->NativeArgAt(0)); 46 GET_NON_NULL_NATIVE_ARGUMENT(ReceivePort, port, arguments->NativeArgAt(0));
47 return Integer::NewFromUint64(port.Id()); 47 return Integer::NewFromUint64(port.Id());
48 } 48 }
49 49
50 50
51 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_sendport, 1) { 51 DEFINE_NATIVE_ENTRY(RawReceivePortImpl_get_sendport, 1) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 IsolateSpawnState* state) { 170 IsolateSpawnState* state) {
171 // Create a new isolate. 171 // Create a new isolate.
172 char* error = NULL; 172 char* error = NULL;
173 if (!CreateIsolate(parent_isolate, state, &error)) { 173 if (!CreateIsolate(parent_isolate, state, &error)) {
174 delete state; 174 delete state;
175 const String& msg = String::Handle(String::New(error)); 175 const String& msg = String::Handle(String::New(error));
176 free(error); 176 free(error);
177 ThrowIsolateSpawnException(msg); 177 ThrowIsolateSpawnException(msg);
178 } 178 }
179 179
180 // The result of spawning an Isolate is an array with 3 elements:
181 // [main_port, pause_capability, terminate_capability]
182 const Array& result = Array::Handle(Array::New(3));
183
184 // Create a SendPort for the new isolate. 180 // Create a SendPort for the new isolate.
185 Isolate* spawned_isolate = state->isolate(); 181 Isolate* spawned_isolate = state->isolate();
186 const SendPort& port = SendPort::Handle( 182 const SendPort& port = SendPort::Handle(
187 SendPort::New(spawned_isolate->main_port())); 183 SendPort::New(spawned_isolate->main_port()));
188 result.SetAt(0, port);
189 Capability& capability = Capability::Handle();
190 capability = Capability::New(spawned_isolate->pause_capability());
191 result.SetAt(1, capability); // pauseCapability
192 capability = Capability::New(spawned_isolate->terminate_capability());
193 result.SetAt(2, capability); // terminateCapability
194 184
195 // Start the new isolate if it is already marked as runnable. 185 // Start the new isolate if it is already marked as runnable.
196 MutexLocker ml(spawned_isolate->mutex()); 186 MutexLocker ml(spawned_isolate->mutex());
197 spawned_isolate->set_spawn_state(state); 187 spawned_isolate->set_spawn_state(state);
198 if (spawned_isolate->is_runnable()) { 188 if (spawned_isolate->is_runnable()) {
199 spawned_isolate->Run(); 189 spawned_isolate->Run();
200 } 190 }
201 191
202 return result.raw(); 192 return port.raw();
203 } 193 }
204 194
205 195
206 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 1) { 196 DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 3) {
207 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0)); 197 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0));
198 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(1));
199 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(2));
208 if (closure.IsClosure()) { 200 if (closure.IsClosure()) {
209 Function& func = Function::Handle(); 201 Function& func = Function::Handle();
210 func = Closure::function(closure); 202 func = Closure::function(closure);
211 if (func.IsImplicitClosureFunction() && func.is_static()) { 203 if (func.IsImplicitClosureFunction() && func.is_static()) {
212 #if defined(DEBUG) 204 #if defined(DEBUG)
213 Context& ctx = Context::Handle(); 205 Context& ctx = Context::Handle();
214 ctx = Closure::context(closure); 206 ctx = Closure::context(closure);
215 ASSERT(ctx.num_variables() == 0); 207 ASSERT(ctx.num_variables() == 0);
216 #endif 208 #endif
217 return Spawn(isolate, new IsolateSpawnState(func)); 209 return Spawn(isolate, new IsolateSpawnState(port.Id(), func, message));
218 } 210 }
219 } 211 }
220 const String& msg = String::Handle(String::New( 212 const String& msg = String::Handle(String::New(
221 "Isolate.spawn expects to be passed a static or top-level function")); 213 "Isolate.spawn expects to be passed a static or top-level function"));
222 Exceptions::ThrowArgumentError(msg); 214 Exceptions::ThrowArgumentError(msg);
223 return Object::null(); 215 return Object::null();
224 } 216 }
225 217
226 218
227 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 1) { 219 DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 4) {
228 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(0)); 220 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0));
221 GET_NON_NULL_NATIVE_ARGUMENT(String, uri, arguments->NativeArgAt(1));
222 GET_NON_NULL_NATIVE_ARGUMENT(Instance, args, arguments->NativeArgAt(2));
223 GET_NON_NULL_NATIVE_ARGUMENT(Instance, message, arguments->NativeArgAt(3));
229 224
230 // Canonicalize the uri with respect to the current isolate. 225 // Canonicalize the uri with respect to the current isolate.
231 char* error = NULL; 226 char* error = NULL;
232 char* canonical_uri = NULL; 227 char* canonical_uri = NULL;
233 const Library& root_lib = 228 const Library& root_lib =
234 Library::Handle(arguments->isolate()->object_store()->root_library()); 229 Library::Handle(arguments->isolate()->object_store()->root_library());
235 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri, 230 if (!CanonicalizeUri(arguments->isolate(), root_lib, uri,
236 &canonical_uri, &error)) { 231 &canonical_uri, &error)) {
237 const String& msg = String::Handle(String::New(error)); 232 const String& msg = String::Handle(String::New(error));
238 ThrowIsolateSpawnException(msg); 233 ThrowIsolateSpawnException(msg);
239 } 234 }
240 235
241 return Spawn(isolate, new IsolateSpawnState(canonical_uri)); 236 return Spawn(isolate, new IsolateSpawnState(port.Id(), canonical_uri,
237 args, message));
242 } 238 }
243 239
244 240
245 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) { 241 DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 2) {
246 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0)); 242 GET_NON_NULL_NATIVE_ARGUMENT(SendPort, port, arguments->NativeArgAt(0));
247 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1)); 243 GET_NON_NULL_NATIVE_ARGUMENT(Array, msg, arguments->NativeArgAt(1));
248 244
249 // Make sure to route this request to the isolate library OOB mesage handler. 245 // Make sure to route this request to the isolate library OOB mesage handler.
250 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg))); 246 msg.SetAt(0, Smi::Handle(Smi::New(Message::kIsolateLibOOBMsg)));
251 247
252 uint8_t* data = NULL; 248 uint8_t* data = NULL;
253 MessageWriter writer(&data, &allocator); 249 MessageWriter writer(&data, &allocator);
254 writer.WriteMessage(msg); 250 writer.WriteMessage(msg);
255 251
256 PortMap::PostMessage(new Message(port.Id(), 252 PortMap::PostMessage(new Message(port.Id(),
257 data, writer.BytesWritten(), 253 data, writer.BytesWritten(),
258 Message::kOOBPriority)); 254 Message::kOOBPriority));
259 return Object::null(); 255 return Object::null();
260 } 256 }
261 257
262
263 DEFINE_NATIVE_ENTRY(Isolate_mainPort, 0) {
264 // The control port is being accessed as a regular port from Dart code. This
265 // is most likely due to the _startIsolate code in dart:isolate. Account for
266 // this by increasing the number of open control ports.
267 isolate->message_handler()->increment_control_ports();
268
269 return ReceivePort::New(isolate->main_port());
270 }
271
272 } // namespace dart 258 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/main.cc ('k') | runtime/lib/isolate_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698