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

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 1541073002: Implement safepointing of threads (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: self-review-comments Created 4 years, 11 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "include/dart_mirrors_api.h" 6 #include "include/dart_mirrors_api.h"
7 #include "include/dart_native_api.h" 7 #include "include/dart_native_api.h"
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "vm/class_finalizer.h" 10 #include "vm/class_finalizer.h"
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 if (function.IsNull()) { 350 if (function.IsNull()) {
351 return ApiError::New(String::Handle(String::New(""))); 351 return ApiError::New(String::Handle(String::New("")));
352 } 352 }
353 const Array& args = Array::Handle(Array::New(kNumArgs)); 353 const Array& args = Array::Handle(Array::New(kNumArgs));
354 args.SetAt(0, receiver); 354 args.SetAt(0, receiver);
355 args.SetAt(1, argument); 355 args.SetAt(1, argument);
356 return DartEntry::InvokeFunction(function, args); 356 return DartEntry::InvokeFunction(function, args);
357 } 357 }
358 358
359 359
360 static const char* GetErrorString(Thread* thread, const Object& obj) {
361 if (obj.IsError()) {
362 const Error& error = Error::Cast(obj);
363 const char* str = error.ToErrorCString();
364 intptr_t len = strlen(str) + 1;
365 char* str_copy = Api::TopScope(thread)->zone()->Alloc<char>(len);
366 strncpy(str_copy, str, len);
367 // Strip a possible trailing '\n'.
368 if ((len > 1) && (str_copy[len - 2] == '\n')) {
369 str_copy[len - 2] = '\0';
370 }
371 return str_copy;
372 } else {
373 return "";
374 }
375 }
376
377
360 Dart_Handle Api::InitNewHandle(Thread* thread, RawObject* raw) { 378 Dart_Handle Api::InitNewHandle(Thread* thread, RawObject* raw) {
361 LocalHandles* local_handles = Api::TopScope(thread)->local_handles(); 379 LocalHandles* local_handles = Api::TopScope(thread)->local_handles();
362 ASSERT(local_handles != NULL); 380 ASSERT(local_handles != NULL);
363 LocalHandle* ref = local_handles->AllocateHandle(); 381 LocalHandle* ref = local_handles->AllocateHandle();
364 ref->set_raw(raw); 382 ref->set_raw(raw);
365 return ref->apiHandle(); 383 return ref->apiHandle();
366 } 384 }
367 385
368 386
369 Dart_Handle Api::NewHandle(Thread* thread, RawObject* raw) { 387 Dart_Handle Api::NewHandle(Thread* thread, RawObject* raw) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 return Api::NewHandle(thread, isolate->object_store()->sticky_error()); 462 return Api::NewHandle(thread, isolate->object_store()->sticky_error());
445 } 463 }
446 464
447 465
448 Dart_Isolate Api::CastIsolate(Isolate* isolate) { 466 Dart_Isolate Api::CastIsolate(Isolate* isolate) {
449 return reinterpret_cast<Dart_Isolate>(isolate); 467 return reinterpret_cast<Dart_Isolate>(isolate);
450 } 468 }
451 469
452 470
453 Dart_Handle Api::NewError(const char* format, ...) { 471 Dart_Handle Api::NewError(const char* format, ...) {
454 DARTSCOPE(Thread::Current()); 472 Thread* T = Thread::Current();
473 CHECK_API_SCOPE(T);
474 HANDLESCOPE(T);
455 CHECK_CALLBACK_STATE(T); 475 CHECK_CALLBACK_STATE(T);
456 476
457 va_list args; 477 va_list args;
458 va_start(args, format); 478 va_start(args, format);
459 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 479 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
460 va_end(args); 480 va_end(args);
461 481
462 char* buffer = Z->Alloc<char>(len + 1); 482 char* buffer = Z->Alloc<char>(len + 1);
463 va_list args2; 483 va_list args2;
464 va_start(args2, format); 484 va_start(args2, format);
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 DARTSCOPE(Thread::Current()); 761 DARTSCOPE(Thread::Current());
742 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 762 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
743 return (obj.IsUnwindError() && UnwindError::Cast(obj).is_vm_restart()); 763 return (obj.IsUnwindError() && UnwindError::Cast(obj).is_vm_restart());
744 } 764 }
745 765
746 766
747 DART_EXPORT const char* Dart_GetError(Dart_Handle handle) { 767 DART_EXPORT const char* Dart_GetError(Dart_Handle handle) {
748 API_TIMELINE_DURATION; 768 API_TIMELINE_DURATION;
749 DARTSCOPE(Thread::Current()); 769 DARTSCOPE(Thread::Current());
750 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 770 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
751 if (obj.IsError()) { 771 return GetErrorString(T, obj);
752 const Error& error = Error::Cast(obj);
753 const char* str = error.ToErrorCString();
754 intptr_t len = strlen(str) + 1;
755 char* str_copy = Api::TopScope(T)->zone()->Alloc<char>(len);
756 strncpy(str_copy, str, len);
757 // Strip a possible trailing '\n'.
758 if ((len > 1) && (str_copy[len - 2] == '\n')) {
759 str_copy[len - 2] = '\0';
760 }
761 return str_copy;
762 } else {
763 return "";
764 }
765 } 772 }
766 773
767 774
768 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) { 775 DART_EXPORT bool Dart_ErrorHasException(Dart_Handle handle) {
769 DARTSCOPE(Thread::Current()); 776 DARTSCOPE(Thread::Current());
770 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle)); 777 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(handle));
771 return obj.IsUnhandledException(); 778 return obj.IsUnhandledException();
772 } 779 }
773 780
774 781
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 } 818 }
812 819
813 820
814 DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception) { 821 DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception) {
815 DARTSCOPE(Thread::Current()); 822 DARTSCOPE(Thread::Current());
816 CHECK_CALLBACK_STATE(T); 823 CHECK_CALLBACK_STATE(T);
817 824
818 Instance& obj = Instance::Handle(Z); 825 Instance& obj = Instance::Handle(Z);
819 intptr_t class_id = Api::ClassId(exception); 826 intptr_t class_id = Api::ClassId(exception);
820 if ((class_id == kApiErrorCid) || (class_id == kLanguageErrorCid)) { 827 if ((class_id == kApiErrorCid) || (class_id == kLanguageErrorCid)) {
821 obj = String::New(::Dart_GetError(exception)); 828 const Object& excp = Object::Handle(Z, Api::UnwrapHandle(exception));
829 obj = String::New(GetErrorString(T, excp));
822 } else { 830 } else {
823 obj = Api::UnwrapInstanceHandle(Z, exception).raw(); 831 obj = Api::UnwrapInstanceHandle(Z, exception).raw();
824 if (obj.IsNull()) { 832 if (obj.IsNull()) {
825 RETURN_TYPE_ERROR(Z, exception, Instance); 833 RETURN_TYPE_ERROR(Z, exception, Instance);
826 } 834 }
827 } 835 }
828 const Stacktrace& stacktrace = Stacktrace::Handle(Z); 836 const Stacktrace& stacktrace = Stacktrace::Handle(Z);
829 return Api::NewHandle(T, UnhandledException::New(obj, stacktrace)); 837 return Api::NewHandle(T, UnhandledException::New(obj, stacktrace));
830 } 838 }
831 839
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1026 DART_EXPORT Dart_WeakPersistentHandle Dart_NewWeakPersistentHandle( 1034 DART_EXPORT Dart_WeakPersistentHandle Dart_NewWeakPersistentHandle(
1027 Dart_Handle object, 1035 Dart_Handle object,
1028 void* peer, 1036 void* peer,
1029 intptr_t external_allocation_size, 1037 intptr_t external_allocation_size,
1030 Dart_WeakPersistentHandleFinalizer callback) { 1038 Dart_WeakPersistentHandleFinalizer callback) {
1031 Thread* thread = Thread::Current(); 1039 Thread* thread = Thread::Current();
1032 CHECK_ISOLATE(thread->isolate()); 1040 CHECK_ISOLATE(thread->isolate());
1033 if (callback == NULL) { 1041 if (callback == NULL) {
1034 return NULL; 1042 return NULL;
1035 } 1043 }
1044 TransitionNativeToVM transition(thread);
1036 return AllocateFinalizableHandle(thread, 1045 return AllocateFinalizableHandle(thread,
1037 object, 1046 object,
1038 peer, 1047 peer,
1039 external_allocation_size, 1048 external_allocation_size,
1040 callback); 1049 callback);
1041 } 1050 }
1042 1051
1043 1052
1044 DART_EXPORT void Dart_DeletePersistentHandle(Dart_PersistentHandle object) { 1053 DART_EXPORT void Dart_DeletePersistentHandle(Dart_PersistentHandle object) {
1045 Isolate* isolate = Isolate::Current(); 1054 Isolate* isolate = Isolate::Current();
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1244 const Error& error_obj = 1253 const Error& error_obj =
1245 Error::Handle(Z, Dart::InitializeIsolate(snapshot, callback_data)); 1254 Error::Handle(Z, Dart::InitializeIsolate(snapshot, callback_data));
1246 if (error_obj.IsNull()) { 1255 if (error_obj.IsNull()) {
1247 #if defined(DART_NO_SNAPSHOT) 1256 #if defined(DART_NO_SNAPSHOT)
1248 if (FLAG_check_function_fingerprints) { 1257 if (FLAG_check_function_fingerprints) {
1249 Library::CheckFunctionFingerprints(); 1258 Library::CheckFunctionFingerprints();
1250 } 1259 }
1251 #endif // defined(DART_NO_SNAPSHOT). 1260 #endif // defined(DART_NO_SNAPSHOT).
1252 // We exit the API scope entered above. 1261 // We exit the API scope entered above.
1253 Dart_ExitScope(); 1262 Dart_ExitScope();
1263 T->set_execution_state(Thread::kThreadInNative);
1264 T->EnterSafepoint();
1254 return Api::CastIsolate(I); 1265 return Api::CastIsolate(I);
1255 } 1266 }
1256 *error = strdup(error_obj.ToErrorCString()); 1267 *error = strdup(error_obj.ToErrorCString());
1257 // We exit the API scope entered above. 1268 // We exit the API scope entered above.
1258 Dart_ExitScope(); 1269 Dart_ExitScope();
1259 } 1270 }
1260 Dart::ShutdownIsolate(); 1271 Dart::ShutdownIsolate();
1261 return reinterpret_cast<Dart_Isolate>(NULL); 1272 return reinterpret_cast<Dart_Isolate>(NULL);
1262 } 1273 }
1263 1274
1264 1275
1265 DART_EXPORT void Dart_ShutdownIsolate() { 1276 DART_EXPORT void Dart_ShutdownIsolate() {
1266 Thread* T = Thread::Current(); 1277 Thread* T = Thread::Current();
1267 Isolate* I = T->isolate(); 1278 Isolate* I = T->isolate();
1268 CHECK_ISOLATE(I); 1279 CHECK_ISOLATE(I);
1269 { 1280 {
1270 StackZone zone(T); 1281 StackZone zone(T);
1271 HandleScope handle_scope(T); 1282 HandleScope handle_scope(T);
1272 Dart::RunShutdownCallback(); 1283 Dart::RunShutdownCallback();
1273 } 1284 }
1285 T->ExitSafepoint();
1286 T->set_execution_state(Thread::kThreadInVM);
1274 Dart::ShutdownIsolate(); 1287 Dart::ShutdownIsolate();
1275 } 1288 }
1276 1289
1277 1290
1278 DART_EXPORT Dart_Isolate Dart_CurrentIsolate() { 1291 DART_EXPORT Dart_Isolate Dart_CurrentIsolate() {
1279 return Api::CastIsolate(Isolate::Current()); 1292 return Api::CastIsolate(Isolate::Current());
1280 } 1293 }
1281 1294
1282 1295
1283 DART_EXPORT void* Dart_CurrentIsolateData() { 1296 DART_EXPORT void* Dart_CurrentIsolateData() {
(...skipping 23 matching lines...) Expand all
1307 DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate) { 1320 DART_EXPORT void Dart_EnterIsolate(Dart_Isolate isolate) {
1308 CHECK_NO_ISOLATE(Isolate::Current()); 1321 CHECK_NO_ISOLATE(Isolate::Current());
1309 // TODO(16615): Validate isolate parameter. 1322 // TODO(16615): Validate isolate parameter.
1310 Isolate* iso = reinterpret_cast<Isolate*>(isolate); 1323 Isolate* iso = reinterpret_cast<Isolate*>(isolate);
1311 if (iso->HasMutatorThread()) { 1324 if (iso->HasMutatorThread()) {
1312 FATAL("Multiple mutators within one isolate is not supported."); 1325 FATAL("Multiple mutators within one isolate is not supported.");
1313 } 1326 }
1314 if (!Thread::EnterIsolate(iso)) { 1327 if (!Thread::EnterIsolate(iso)) {
1315 FATAL("Unable to Enter Isolate as Dart VM is shutting down"); 1328 FATAL("Unable to Enter Isolate as Dart VM is shutting down");
1316 } 1329 }
1330 Thread* T = Thread::Current();
1331 T->set_execution_state(Thread::kThreadInNative);
1332 T->EnterSafepoint();
1317 } 1333 }
1318 1334
1319 1335
1320 DART_EXPORT void Dart_ThreadDisableProfiling() { 1336 DART_EXPORT void Dart_ThreadDisableProfiling() {
1321 OSThread* os_thread = OSThread::Current(); 1337 OSThread* os_thread = OSThread::Current();
1322 if (os_thread == NULL) { 1338 if (os_thread == NULL) {
1323 return; 1339 return;
1324 } 1340 }
1325 os_thread->DisableThreadInterrupts(); 1341 os_thread->DisableThreadInterrupts();
1326 } 1342 }
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 data.done = false; 1541 data.done = false;
1526 I->message_handler()->Run( 1542 I->message_handler()->Run(
1527 Dart::thread_pool(), 1543 Dart::thread_pool(),
1528 NULL, RunLoopDone, reinterpret_cast<uword>(&data)); 1544 NULL, RunLoopDone, reinterpret_cast<uword>(&data));
1529 while (!data.done) { 1545 while (!data.done) {
1530 ml.Wait(); 1546 ml.Wait();
1531 } 1547 }
1532 if (!Thread::EnterIsolate(I)) { 1548 if (!Thread::EnterIsolate(I)) {
1533 FATAL("Inconsistent state, VM shutting down while in run loop."); 1549 FATAL("Inconsistent state, VM shutting down while in run loop.");
1534 } 1550 }
1551 Thread* thread = Thread::Current();
zra 2016/01/08 23:32:07 It looks like TransitionNativeToVM transition(thre
siva 2016/01/12 21:26:22 DARTSCOPE has a TransitionNativeToVM in it which t
1552 thread->set_execution_state(Thread::kThreadInNative);
1553 thread->EnterSafepoint();
1535 } 1554 }
1536 if (I->object_store()->sticky_error() != Object::null()) { 1555 if (I->object_store()->sticky_error() != Object::null()) {
1537 Dart_Handle error = Api::NewHandle(T, I->object_store()->sticky_error()); 1556 Dart_Handle error = Api::NewHandle(T, I->object_store()->sticky_error());
1538 I->object_store()->clear_sticky_error(); 1557 I->object_store()->clear_sticky_error();
1539 return error; 1558 return error;
1540 } 1559 }
1541 if (FLAG_print_class_table) { 1560 if (FLAG_print_class_table) {
1542 HANDLESCOPE(T); 1561 HANDLESCOPE(T);
1543 I->class_table()->Print(); 1562 I->class_table()->Print();
1544 } 1563 }
1545 return Api::Success(); 1564 return Api::Success();
1546 } 1565 }
1547 1566
1548 1567
1549 DART_EXPORT Dart_Handle Dart_HandleMessage() { 1568 DART_EXPORT Dart_Handle Dart_HandleMessage() {
1550 Thread* T = Thread::Current(); 1569 Thread* T = Thread::Current();
1551 Isolate* I = T->isolate(); 1570 Isolate* I = T->isolate();
1552 CHECK_API_SCOPE(T); 1571 CHECK_API_SCOPE(T);
1553 CHECK_CALLBACK_STATE(T); 1572 CHECK_CALLBACK_STATE(T);
1554 API_TIMELINE_BEGIN_END; 1573 API_TIMELINE_BEGIN_END;
1574 TransitionNativeToVM trainsition(T);
1555 if (I->message_handler()->HandleNextMessage() != MessageHandler::kOK) { 1575 if (I->message_handler()->HandleNextMessage() != MessageHandler::kOK) {
1556 Dart_Handle error = Api::NewHandle(T, I->object_store()->sticky_error()); 1576 Dart_Handle error = Api::NewHandle(T, I->object_store()->sticky_error());
1557 I->object_store()->clear_sticky_error(); 1577 I->object_store()->clear_sticky_error();
1558 return error; 1578 return error;
1559 } 1579 }
1560 return Api::Success(); 1580 return Api::Success();
1561 } 1581 }
1562 1582
1563 1583
1564 DART_EXPORT bool Dart_HandleServiceMessages() { 1584 DART_EXPORT bool Dart_HandleServiceMessages() {
1565 Thread* T = Thread::Current(); 1585 Thread* T = Thread::Current();
1566 Isolate* I = T->isolate(); 1586 Isolate* I = T->isolate();
1567 CHECK_API_SCOPE(T); 1587 CHECK_API_SCOPE(T);
1568 CHECK_CALLBACK_STATE(T); 1588 CHECK_CALLBACK_STATE(T);
1569 API_TIMELINE_DURATION; 1589 API_TIMELINE_DURATION;
1590 TransitionNativeToVM trainsition(T);
1570 ASSERT(I->GetAndClearResumeRequest() == false); 1591 ASSERT(I->GetAndClearResumeRequest() == false);
1571 MessageHandler::MessageStatus status = 1592 MessageHandler::MessageStatus status =
1572 I->message_handler()->HandleOOBMessages(); 1593 I->message_handler()->HandleOOBMessages();
1573 bool resume = I->GetAndClearResumeRequest(); 1594 bool resume = I->GetAndClearResumeRequest();
1574 return (status != MessageHandler::kOK) || resume; 1595 return (status != MessageHandler::kOK) || resume;
1575 } 1596 }
1576 1597
1577 1598
1578 DART_EXPORT bool Dart_HasServiceMessages() { 1599 DART_EXPORT bool Dart_HasServiceMessages() {
1579 Isolate* isolate = Isolate::Current(); 1600 Isolate* isolate = Isolate::Current();
(...skipping 3260 matching lines...) Expand 10 before | Expand all | Expand 10 after
4840 ASSERT(isolate->api_state() != NULL && 4861 ASSERT(isolate->api_state() != NULL &&
4841 (isolate->api_state()->IsValidWeakPersistentHandle(rval))); 4862 (isolate->api_state()->IsValidWeakPersistentHandle(rval)));
4842 #endif 4863 #endif
4843 Api::SetWeakHandleReturnValue(arguments, rval); 4864 Api::SetWeakHandleReturnValue(arguments, rval);
4844 } 4865 }
4845 4866
4846 4867
4847 // --- Environment --- 4868 // --- Environment ---
4848 RawString* Api::CallEnvironmentCallback(Thread* thread, const String& name) { 4869 RawString* Api::CallEnvironmentCallback(Thread* thread, const String& name) {
4849 Isolate* isolate = thread->isolate(); 4870 Isolate* isolate = thread->isolate();
4850 Scope api_scope(thread);
4851 Dart_EnvironmentCallback callback = isolate->environment_callback(); 4871 Dart_EnvironmentCallback callback = isolate->environment_callback();
4852 String& result = String::Handle(thread->zone()); 4872 String& result = String::Handle(thread->zone());
4853 if (callback != NULL) { 4873 if (callback != NULL) {
4874 TransitionVMToNative transition(thread);
4875 Scope api_scope(thread);
4854 Dart_Handle response = callback(Api::NewHandle(thread, name.raw())); 4876 Dart_Handle response = callback(Api::NewHandle(thread, name.raw()));
4855 if (::Dart_IsString(response)) { 4877 if (::Dart_IsString(response)) {
4856 result ^= Api::UnwrapHandle(response); 4878 result ^= Api::UnwrapHandle(response);
4857 } else if (::Dart_IsError(response)) { 4879 } else if (::Dart_IsError(response)) {
4858 const Object& error = Object::Handle( 4880 const Object& error = Object::Handle(
4859 thread->zone(), Api::UnwrapHandle(response)); 4881 thread->zone(), Api::UnwrapHandle(response));
4860 Exceptions::ThrowArgumentError( 4882 Exceptions::ThrowArgumentError(
4861 String::Handle(String::New(Error::Cast(error).ToErrorCString()))); 4883 String::Handle(String::New(Error::Cast(error).ToErrorCString())));
4862 } else if (!::Dart_IsNull(response)) { 4884 } else if (!::Dart_IsNull(response)) {
4863 // At this point everything except null are invalid environment values. 4885 // At this point everything except null are invalid environment values.
(...skipping 1133 matching lines...) Expand 10 before | Expand all | Expand 10 after
5997 return Api::Success(); 6019 return Api::Success();
5998 } 6020 }
5999 #endif // DART_PRECOMPILED 6021 #endif // DART_PRECOMPILED
6000 6022
6001 6023
6002 DART_EXPORT bool Dart_IsRunningPrecompiledCode() { 6024 DART_EXPORT bool Dart_IsRunningPrecompiledCode() {
6003 return Dart::IsRunningPrecompiledCode(); 6025 return Dart::IsRunningPrecompiledCode();
6004 } 6026 }
6005 6027
6006 } // namespace dart 6028 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698