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

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

Issue 9924015: Use the ThreadPool for all isolates and native ports. Previously, (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 8 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 | « no previous file | runtime/vm/code_generator.cc » ('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"
11 #include "vm/exceptions.h" 11 #include "vm/exceptions.h"
12 #include "vm/longjump.h" 12 #include "vm/longjump.h"
13 #include "vm/message.h" 13 #include "vm/message_handler.h"
14 #include "vm/object.h" 14 #include "vm/object.h"
15 #include "vm/object_store.h" 15 #include "vm/object_store.h"
16 #include "vm/port.h" 16 #include "vm/port.h"
17 #include "vm/resolver.h" 17 #include "vm/resolver.h"
18 #include "vm/snapshot.h" 18 #include "vm/snapshot.h"
19 #include "vm/thread.h" 19 #include "vm/thread.h"
20 20
21 namespace dart { 21 namespace dart {
22 22
23 class IsolateStartData { 23 class IsolateStartData {
24 public: 24 public:
25 IsolateStartData(Isolate* isolate, 25 IsolateStartData(char* library_url,
26 char* library_url,
27 char* class_name, 26 char* class_name,
28 intptr_t port_id) 27 intptr_t port_id)
29 : isolate_(isolate), 28 : library_url_(library_url),
30 library_url_(library_url),
31 class_name_(class_name), 29 class_name_(class_name),
32 port_id_(port_id) {} 30 port_id_(port_id) {}
33 31
34 Isolate* isolate_;
35 char* library_url_; 32 char* library_url_;
36 char* class_name_; 33 char* class_name_;
37 intptr_t port_id_; 34 intptr_t port_id_;
38 }; 35 };
39 36
40 37
41 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 38 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
42 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size); 39 void* new_ptr = realloc(reinterpret_cast<void*>(ptr), new_size);
43 return reinterpret_cast<uint8_t*>(new_ptr); 40 return reinterpret_cast<uint8_t*>(new_ptr);
44 } 41 }
45 42
46 43
47 static uint8_t* SerializeObject(const Instance& obj) { 44 static uint8_t* SerializeObject(const Instance& obj) {
48 uint8_t* result = NULL; 45 uint8_t* result = NULL;
49 SnapshotWriter writer(Snapshot::kMessage, &result, &allocator); 46 SnapshotWriter writer(Snapshot::kMessage, &result, &allocator);
50 writer.WriteObject(obj.raw()); 47 writer.WriteObject(obj.raw());
51 writer.FinalizeBuffer(); 48 writer.FinalizeBuffer();
52 return result; 49 return result;
53 } 50 }
54 51
55 52
56 // TODO(turnidge): Taking down the whole vm when an isolate fails is 53 static void StoreError(Isolate* isolate, const Object& obj) {
57 // bad. Change this.
58 static void ProcessError(const Object& obj) {
59 ASSERT(obj.IsError()); 54 ASSERT(obj.IsError());
60 Error& error = Error::Handle(); 55 Error& error = Error::Handle();
61 error ^= obj.raw(); 56 error ^= obj.raw();
62 OS::PrintErr("%s\n", error.ToErrorCString()); 57 isolate->object_store()->set_sticky_error(error);
63 exit(255);
64 } 58 }
65 59
66 60
67 static void ThrowErrorException(Exceptions::ExceptionType type, 61 static void ThrowErrorException(Exceptions::ExceptionType type,
68 const char* error_msg, 62 const char* error_msg,
69 const char* library_url, 63 const char* library_url,
70 const char* class_name) { 64 const char* class_name) {
71 String& str = String::Handle(); 65 String& str = String::Handle();
72 String& name = String::Handle(); 66 String& name = String::Handle();
73 str ^= String::New(error_msg); 67 str ^= String::New(error_msg);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 arguments.Add(&Integer::Handle(Integer::New(port_id))); 100 arguments.Add(&Integer::Handle(Integer::New(port_id)));
107 const Object& result = Object::Handle( 101 const Object& result = Object::Handle(
108 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); 102 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames));
109 if (!result.IsError()) { 103 if (!result.IsError()) {
110 PortMap::SetLive(port_id); 104 PortMap::SetLive(port_id);
111 } 105 }
112 return result.raw(); 106 return result.raw();
113 } 107 }
114 108
115 109
116 static void RunIsolate(uword parameter) { 110 static bool RunIsolate(uword parameter) {
117 IsolateStartData* data = reinterpret_cast<IsolateStartData*>(parameter); 111 Isolate* isolate = reinterpret_cast<Isolate*>(parameter);
118 Isolate* isolate = data->isolate_; 112 IsolateStartData* data =
113 reinterpret_cast<IsolateStartData*>(isolate->spawn_data());
114 isolate->set_spawn_data(NULL);
119 char* library_url = data->library_url_; 115 char* library_url = data->library_url_;
120 char* class_name = data->class_name_; 116 char* class_name = data->class_name_;
121 intptr_t port_id = data->port_id_; 117 intptr_t port_id = data->port_id_;
122 delete data; 118 delete data;
123 119
124 Isolate::SetCurrent(isolate); 120 {
125 // Intialize stack limit in case we are running isolate in a 121 StartIsolateScope start_scope(isolate);
126 // different thread than in which it was initialized.
127 isolate->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&isolate));
128 LongJump* base = isolate->long_jump_base();
129 LongJump jump;
130 isolate->set_long_jump_base(&jump);
131 if (setjmp(*jump.Set()) == 0) {
132 Zone zone(isolate); 122 Zone zone(isolate);
133 HandleScope handle_scope(isolate); 123 HandleScope handle_scope(isolate);
134 ASSERT(ClassFinalizer::FinalizePendingClasses()); 124 ASSERT(ClassFinalizer::FinalizePendingClasses());
135 // Lookup the target class by name, create an instance and call the run 125 // Lookup the target class by name, create an instance and call the run
136 // method. 126 // method.
137 const String& lib_name = String::Handle(String::NewSymbol(library_url)); 127 const String& lib_name = String::Handle(String::NewSymbol(library_url));
128 free(library_url);
138 const Library& lib = Library::Handle(Library::LookupLibrary(lib_name)); 129 const Library& lib = Library::Handle(Library::LookupLibrary(lib_name));
139 ASSERT(!lib.IsNull()); 130 ASSERT(!lib.IsNull());
140 const String& cls_name = String::Handle(String::NewSymbol(class_name)); 131 const String& cls_name = String::Handle(String::NewSymbol(class_name));
132 free(class_name);
141 const Class& target_class = Class::Handle(lib.LookupClass(cls_name)); 133 const Class& target_class = Class::Handle(lib.LookupClass(cls_name));
142 // TODO(iposva): Deserialize or call the constructor after allocating. 134 // TODO(iposva): Deserialize or call the constructor after allocating.
143 // For now, we only support a non-parameterized or raw target class. 135 // For now, we only support a non-parameterized or raw target class.
144 const Instance& target = Instance::Handle(Instance::New(target_class)); 136 const Instance& target = Instance::Handle(Instance::New(target_class));
145 Object& result = Object::Handle(); 137 Object& result = Object::Handle();
146 138
147 // Invoke the default constructor. 139 // Invoke the default constructor.
148 const String& period = String::Handle(String::New(".")); 140 const String& period = String::Handle(String::New("."));
149 String& constructor_name = String::Handle(String::Concat(cls_name, period)); 141 String& constructor_name = String::Handle(String::Concat(cls_name, period));
150 const Function& default_constructor = 142 const Function& default_constructor =
151 Function::Handle(target_class.LookupConstructor(constructor_name)); 143 Function::Handle(target_class.LookupConstructor(constructor_name));
152 if (!default_constructor.IsNull()) { 144 if (!default_constructor.IsNull()) {
153 GrowableArray<const Object*> arguments(1); 145 GrowableArray<const Object*> arguments(1);
154 arguments.Add(&target); 146 arguments.Add(&target);
155 arguments.Add(&Smi::Handle(Smi::New(Function::kCtorPhaseAll))); 147 arguments.Add(&Smi::Handle(Smi::New(Function::kCtorPhaseAll)));
156 const Array& kNoArgumentNames = Array::Handle(); 148 const Array& kNoArgumentNames = Array::Handle();
157 result = DartEntry::InvokeStatic(default_constructor, 149 result = DartEntry::InvokeStatic(default_constructor,
158 arguments, 150 arguments,
159 kNoArgumentNames); 151 kNoArgumentNames);
160 if (result.IsError()) { 152 if (result.IsError()) {
161 ProcessError(result); 153 StoreError(isolate, result);
154 return false;
162 } 155 }
163 ASSERT(result.IsNull()); 156 ASSERT(result.IsNull());
164 } 157 }
165 158
166 // Invoke the "_run" method. 159 // Invoke the "_run" method.
167 const Function& target_function = Function::Handle(Resolver::ResolveDynamic( 160 const Function& target_function = Function::Handle(Resolver::ResolveDynamic(
168 target, String::Handle(String::NewSymbol("_run")), 2, 0)); 161 target, String::Handle(String::NewSymbol("_run")), 2, 0));
169 // TODO(iposva): Proper error checking here. 162 // TODO(iposva): Proper error checking here.
170 ASSERT(!target_function.IsNull()); 163 ASSERT(!target_function.IsNull());
171 // TODO(iposva): Allocate the proper port number here. 164 // TODO(iposva): Allocate the proper port number here.
172 const Object& local_port = Object::Handle(ReceivePortCreate(port_id)); 165 const Object& local_port = Object::Handle(ReceivePortCreate(port_id));
173 if (local_port.IsError()) { 166 if (local_port.IsError()) {
174 ProcessError(local_port); 167 StoreError(isolate, local_port);
168 return false;
175 } 169 }
176 GrowableArray<const Object*> arguments(1); 170 GrowableArray<const Object*> arguments(1);
177 arguments.Add(&local_port); 171 arguments.Add(&local_port);
178 const Array& kNoArgumentNames = Array::Handle(); 172 const Array& kNoArgumentNames = Array::Handle();
179 result = DartEntry::InvokeDynamic(target, 173 result = DartEntry::InvokeDynamic(target,
180 target_function, 174 target_function,
181 arguments, 175 arguments,
182 kNoArgumentNames); 176 kNoArgumentNames);
183 if (result.IsError()) { 177 if (result.IsError()) {
184 ProcessError(result); 178 StoreError(isolate, result);
179 return false;
185 } 180 }
186 ASSERT(result.IsNull()); 181 ASSERT(result.IsNull());
187 free(class_name);
188 free(library_url);
189 result = isolate->StandardRunLoop();
190 if (result.IsError()) {
191 ProcessError(result);
192 }
193 ASSERT(result.IsNull());
194
195 } else {
196 Zone zone(isolate);
197 HandleScope handle_scope(isolate);
198 const Error& error = Error::Handle(
199 Isolate::Current()->object_store()->sticky_error());
200 const char* errmsg = error.ToErrorCString();
201 OS::PrintErr("%s\n", errmsg);
202 exit(255);
203 } 182 }
204 isolate->set_long_jump_base(base); 183 return true;
205 Dart::ShutdownIsolate();
206 } 184 }
207 185
208 186
187 static void ShutdownIsolate(uword parameter) {
188 Isolate* isolate = reinterpret_cast<Isolate*>(parameter);
189 {
190 // Print the error if there is one. This may execute dart code to
191 // print the exception object, so we need to use a StartIsolateScope.
192 StartIsolateScope start_scope(isolate);
193 Zone zone(isolate);
194 HandleScope handle_scope(isolate);
195 Error& error = Error::Handle();
196 error = isolate->object_store()->sticky_error();
197 if (!error.IsNull()) {
198 OS::PrintErr("%s\n", error.ToErrorCString());
199 exit(255);
200 }
201 }
202 {
203 // Shut the isolate down.
204 SwitchIsolateScope switch_scope(isolate);
205 Dart::ShutdownIsolate();
206 }
207 }
208
209
209 static bool CheckArguments(const char* library_url, const char* class_name) { 210 static bool CheckArguments(const char* library_url, const char* class_name) {
210 Isolate* isolate = Isolate::Current(); 211 Isolate* isolate = Isolate::Current();
211 Zone zone(isolate); 212 Zone zone(isolate);
212 HandleScope handle_scope(isolate); 213 HandleScope handle_scope(isolate);
213 String& name = String::Handle(); 214 String& name = String::Handle();
214 if (!ClassFinalizer::FinalizePendingClasses()) { 215 if (!ClassFinalizer::FinalizePendingClasses()) {
215 return false; 216 return false;
216 } 217 }
217 // Lookup the target class by name, create an instance and call the run 218 // Lookup the target class by name, create an instance and call the run
218 // method. 219 // method.
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 const char* isolate_name = BuildIsolateName(library_url, class_name, "main"); 286 const char* isolate_name = BuildIsolateName(library_url, class_name, "main");
286 if (callback == NULL) { 287 if (callback == NULL) {
287 error = strdup("Null callback specified for isolate creation\n"); 288 error = strdup("Null callback specified for isolate creation\n");
288 } else if (callback(isolate_name, callback_data, &error)) { 289 } else if (callback(isolate_name, callback_data, &error)) {
289 spawned_isolate = Isolate::Current(); 290 spawned_isolate = Isolate::Current();
290 ASSERT(spawned_isolate != NULL); 291 ASSERT(spawned_isolate != NULL);
291 // Check arguments to see if the specified library and classes are 292 // Check arguments to see if the specified library and classes are
292 // loaded, this check will throw an exception if they are not loaded. 293 // loaded, this check will throw an exception if they are not loaded.
293 if (init_successful && CheckArguments(library_url, class_name)) { 294 if (init_successful && CheckArguments(library_url, class_name)) {
294 port_id = spawned_isolate->main_port(); 295 port_id = spawned_isolate->main_port();
295 uword data = reinterpret_cast<uword>( 296 spawned_isolate->set_spawn_data(
296 new IsolateStartData(spawned_isolate, 297 reinterpret_cast<uword>(
297 strdup(library_url), 298 new IsolateStartData(strdup(library_url),
298 strdup(class_name), 299 strdup(class_name),
299 port_id)); 300 port_id)));
300 int result = Thread::Start(RunIsolate, data); 301 Isolate::SetCurrent(NULL);
301 if (result != 0) { 302 spawned_isolate->message_handler()->Run(
302 FATAL1("Failed to start isolate thread %d", result); 303 Dart::thread_pool(), RunIsolate, ShutdownIsolate,
303 } 304 reinterpret_cast<uword>(spawned_isolate));
304 } else { 305 } else {
305 // Error spawning the isolate, maybe due to initialization errors or 306 // Error spawning the isolate, maybe due to initialization errors or
306 // errors while loading the application into spawned isolate, shut 307 // errors while loading the application into spawned isolate, shut
307 // it down and report error. 308 // it down and report error.
308 // Make sure to grab the error message out of the isolate before it has 309 // Make sure to grab the error message out of the isolate before it has
309 // been shutdown and to allocate it in the preserved isolates zone. 310 // been shutdown and to allocate it in the preserved isolates zone.
310 { 311 {
311 Zone zone(spawned_isolate); 312 Zone zone(spawned_isolate);
312 HandleScope scope(spawned_isolate); 313 HandleScope scope(spawned_isolate);
313 const Error& err_obj = Error::Handle( 314 const Error& err_obj = Error::Handle(
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 String::Handle(String::NewSymbol(function_name())); 419 String::Handle(String::NewSymbol(function_name()));
419 420
420 const Library& lib = Library::Handle(Library::LookupLibrary(lib_url)); 421 const Library& lib = Library::Handle(Library::LookupLibrary(lib_url));
421 if (lib.IsNull() || lib.IsError()) { 422 if (lib.IsNull() || lib.IsError()) {
422 return Function::null(); 423 return Function::null();
423 } 424 }
424 return lib.LookupLocalFunction(func_name); 425 return lib.LookupLocalFunction(func_name);
425 } 426 }
426 427
427 void Cleanup() { 428 void Cleanup() {
428 Isolate* saved = Isolate::Current(); 429 SwitchIsolateScope switch_scope(isolate());
429 Isolate::SetCurrent(isolate());
430 Dart::ShutdownIsolate(); 430 Dart::ShutdownIsolate();
431 Isolate::SetCurrent(saved);
432 } 431 }
433 432
434 private: 433 private:
435 Isolate* isolate_; 434 Isolate* isolate_;
436 char* library_url_; 435 char* library_url_;
437 char* function_name_; 436 char* function_name_;
438 }; 437 };
439 438
440 439
441 static bool CreateIsolate(SpawnState* state, char** error) { 440 static bool CreateIsolate(SpawnState* state, char** error) {
(...skipping 30 matching lines...) Expand all
472 } 471 }
473 } 472 }
474 if (!retval) { 473 if (!retval) {
475 Dart::ShutdownIsolate(); 474 Dart::ShutdownIsolate();
476 } 475 }
477 Isolate::SetCurrent(parent_isolate); 476 Isolate::SetCurrent(parent_isolate);
478 return retval; 477 return retval;
479 } 478 }
480 479
481 480
482 static void RunIsolate2(uword parameter) { 481 static bool RunIsolate2(uword parameter) {
483 SpawnState* state = reinterpret_cast<SpawnState*>(parameter); 482 Isolate* isolate = reinterpret_cast<Isolate*>(parameter);
484 Isolate* isolate = state->isolate(); 483 SpawnState* state = reinterpret_cast<SpawnState*>(isolate->spawn_data());
485 484 isolate->set_spawn_data(NULL);
486 Isolate::SetCurrent(isolate);
487 // Intialize stack limit in case we are running isolate in a
488 // different thread than in which it was initialized.
489 isolate->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&isolate));
490
491 { 485 {
486 StartIsolateScope start_scope(isolate);
492 Zone zone(isolate); 487 Zone zone(isolate);
493 HandleScope handle_scope(isolate); 488 HandleScope handle_scope(isolate);
494 ASSERT(ClassFinalizer::FinalizePendingClasses()); 489 ASSERT(ClassFinalizer::FinalizePendingClasses());
495 Object& result = Object::Handle(); 490 Object& result = Object::Handle();
496 491
497 const Function& func = Function::Handle(state->ResolveFunction()); 492 const Function& func = Function::Handle(state->ResolveFunction());
498 delete state; 493 delete state;
499 state = NULL; 494 state = NULL;
500 ASSERT(!func.IsNull()); 495 ASSERT(!func.IsNull());
501 496
502 GrowableArray<const Object*> args(0); 497 GrowableArray<const Object*> args(0);
503 const Array& kNoArgNames = Array::Handle(); 498 const Array& kNoArgNames = Array::Handle();
504 result = DartEntry::InvokeStatic(func, args, kNoArgNames); 499 result = DartEntry::InvokeStatic(func, args, kNoArgNames);
505 if (result.IsError()) { 500 if (result.IsError()) {
506 ProcessError(result); 501 StoreError(isolate, result);
502 return false;
507 } 503 }
508
509 result = isolate->StandardRunLoop();
510 if (result.IsError()) {
511 ProcessError(result);
512 }
513 ASSERT(result.IsNull());
514 } 504 }
515 Dart::ShutdownIsolate(); 505 return true;
516 } 506 }
517 507
518 508
519 DEFINE_NATIVE_ENTRY(isolate_spawnFunction, 1) { 509 DEFINE_NATIVE_ENTRY(isolate_spawnFunction, 1) {
520 GET_NATIVE_ARGUMENT(Closure, closure, arguments->At(0)); 510 GET_NATIVE_ARGUMENT(Closure, closure, arguments->At(0));
521 const Function& func = Function::Handle(closure.function()); 511 const Function& func = Function::Handle(closure.function());
522 const Class& cls = Class::Handle(func.owner()); 512 const Class& cls = Class::Handle(func.owner());
523 if (!func.IsClosureFunction() || !func.is_static() || !cls.IsTopLevel()) { 513 if (!func.IsClosureFunction() || !func.is_static() || !cls.IsTopLevel()) {
524 const String& msg = String::Handle(String::New( 514 const String& msg = String::Handle(String::New(
525 "spawnFunction expects to be passed a closure to a top-level static " 515 "spawnFunction expects to be passed a closure to a top-level static "
(...skipping 18 matching lines...) Expand all
544 // Try to create a SendPort for the new isolate. 534 // Try to create a SendPort for the new isolate.
545 const Object& port = Object::Handle( 535 const Object& port = Object::Handle(
546 DartLibraryCalls::NewSendPort(state->isolate()->main_port())); 536 DartLibraryCalls::NewSendPort(state->isolate()->main_port()));
547 if (port.IsError()) { 537 if (port.IsError()) {
548 state->Cleanup(); 538 state->Cleanup();
549 delete state; 539 delete state;
550 Exceptions::PropagateError(port); 540 Exceptions::PropagateError(port);
551 } 541 }
552 542
553 // Start the new isolate. 543 // Start the new isolate.
554 int result = Thread::Start(RunIsolate2, reinterpret_cast<uword>(state)); 544 state->isolate()->set_spawn_data(reinterpret_cast<uword>(state));
555 if (result != 0) { 545 state->isolate()->message_handler()->Run(
556 const String& msg = String::Handle(String::NewFormatted( 546 Dart::thread_pool(), RunIsolate2, ShutdownIsolate,
557 "Failed to start thread for isolate '%s'. Error code '%d'.", 547 reinterpret_cast<uword>(state->isolate()));
558 state->isolate()->name(), result));
559 state->Cleanup();
560 delete state;
561 ThrowIsolateSpawnException(msg);
562 }
563 548
564 arguments->SetReturn(port); 549 arguments->SetReturn(port);
565 } 550 }
566 551
567 552
568 DEFINE_NATIVE_ENTRY(isolate_getPortInternal, 0) { 553 DEFINE_NATIVE_ENTRY(isolate_getPortInternal, 0) {
569 const Object& port = Object::Handle(ReceivePortCreate(isolate->main_port())); 554 const Object& port = Object::Handle(ReceivePortCreate(isolate->main_port()));
570 if (port.IsError()) { 555 if (port.IsError()) {
571 Exceptions::PropagateError(port); 556 Exceptions::PropagateError(port);
572 } 557 }
573 arguments->SetReturn(port); 558 arguments->SetReturn(port);
574 } 559 }
575 560
576 } // namespace dart 561 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/code_generator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698