Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "vm/isolate.h" | 5 #include "vm/isolate.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "platform/json.h" | 9 #include "platform/json.h" |
| 10 #include "lib/mirrors.h" | 10 #include "lib/mirrors.h" |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 is_runnable_(false), | 307 is_runnable_(false), |
| 308 gc_prologue_callbacks_(), | 308 gc_prologue_callbacks_(), |
| 309 gc_epilogue_callbacks_(), | 309 gc_epilogue_callbacks_(), |
| 310 defer_finalization_count_(0), | 310 defer_finalization_count_(0), |
| 311 deopt_context_(NULL), | 311 deopt_context_(NULL), |
| 312 stacktrace_(NULL), | 312 stacktrace_(NULL), |
| 313 stack_frame_index_(-1), | 313 stack_frame_index_(-1), |
| 314 object_histogram_(NULL), | 314 object_histogram_(NULL), |
| 315 object_id_ring_(NULL), | 315 object_id_ring_(NULL), |
| 316 profiler_data_(NULL), | 316 profiler_data_(NULL), |
| 317 thread_state_(NULL), | |
| 317 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_INITIALIZERS) | 318 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_INITIALIZERS) |
| 318 reusable_handles_() { | 319 reusable_handles_() { |
| 319 if (FLAG_print_object_histogram && (Dart::vm_isolate() != NULL)) { | 320 if (FLAG_print_object_histogram && (Dart::vm_isolate() != NULL)) { |
| 320 object_histogram_ = new ObjectHistogram(this); | 321 object_histogram_ = new ObjectHistogram(this); |
| 321 } | 322 } |
| 322 } | 323 } |
| 323 #undef REUSABLE_HANDLE_INITIALIZERS | 324 #undef REUSABLE_HANDLE_INITIALIZERS |
| 324 | 325 |
| 325 | 326 |
| 326 Isolate::~Isolate() { | 327 Isolate::~Isolate() { |
| 327 delete [] name_; | 328 delete [] name_; |
| 328 delete heap_; | 329 delete heap_; |
| 329 delete object_store_; | 330 delete object_store_; |
| 330 delete api_state_; | 331 delete api_state_; |
| 331 delete stub_code_; | 332 delete stub_code_; |
| 332 delete debugger_; | 333 delete debugger_; |
| 333 #if defined(USING_SIMULATOR) | 334 #if defined(USING_SIMULATOR) |
| 334 delete simulator_; | 335 delete simulator_; |
| 335 #endif | 336 #endif |
| 336 delete mutex_; | 337 delete mutex_; |
| 337 mutex_ = NULL; // Fail fast if interrupts are scheduled on a dead isolate. | 338 mutex_ = NULL; // Fail fast if interrupts are scheduled on a dead isolate. |
| 338 delete message_handler_; | 339 delete message_handler_; |
| 339 message_handler_ = NULL; // Fail fast if we send messages to a dead isolate. | 340 message_handler_ = NULL; // Fail fast if we send messages to a dead isolate. |
| 340 ASSERT(deopt_context_ == NULL); // No deopt in progress when isolate deleted. | 341 ASSERT(deopt_context_ == NULL); // No deopt in progress when isolate deleted. |
| 341 delete object_histogram_; | 342 delete object_histogram_; |
| 342 delete spawn_state_; | 343 delete spawn_state_; |
| 343 } | 344 } |
| 344 | 345 |
| 346 | |
| 345 void Isolate::SetCurrent(Isolate* current) { | 347 void Isolate::SetCurrent(Isolate* current) { |
| 346 Isolate* old_current = Current(); | 348 Isolate* old_current = Current(); |
| 347 if (old_current != current) { | 349 if (old_current != NULL) { |
| 350 old_current->set_thread_state(NULL); | |
| 348 Profiler::EndExecution(old_current); | 351 Profiler::EndExecution(old_current); |
| 349 Thread::SetThreadLocal(isolate_key, reinterpret_cast<uword>(current)); | 352 } |
| 353 Thread::SetThreadLocal(isolate_key, reinterpret_cast<uword>(current)); | |
| 354 if (current != NULL) { | |
|
siva
2014/02/05 19:09:15
We also need
ASSERT(current->thread_state() == NUL
Cutch
2014/02/05 23:00:31
Done.
| |
| 355 #if defined(DEBUG) | |
| 356 CheckNoIsolateHasThreadData(ThreadInterrupter::GetCurrentThreadState()); | |
| 357 #endif | |
| 350 Profiler::BeginExecution(current); | 358 Profiler::BeginExecution(current); |
| 359 current->set_thread_state(ThreadInterrupter::GetCurrentThreadState()); | |
|
siva
2014/02/05 19:09:15
Why not have a local variable
InterruptibleThreadS
Cutch
2014/02/05 23:00:31
Done.
| |
| 351 } | 360 } |
| 352 } | 361 } |
| 353 | 362 |
| 354 | 363 |
| 355 // The single thread local key which stores all the thread local data | 364 // The single thread local key which stores all the thread local data |
| 356 // for a thread. Since an Isolate is the central repository for | 365 // for a thread. Since an Isolate is the central repository for |
| 357 // storing all isolate specific information a single thread local key | 366 // storing all isolate specific information a single thread local key |
| 358 // is sufficient. | 367 // is sufficient. |
| 359 ThreadLocalKey Isolate::isolate_key = Thread::kUnsetThreadLocalKey; | 368 ThreadLocalKey Isolate::isolate_key = Thread::kUnsetThreadLocalKey; |
| 360 | 369 |
| 361 | 370 |
| 362 void Isolate::InitOnce() { | 371 void Isolate::InitOnce() { |
| 363 ASSERT(isolate_key == Thread::kUnsetThreadLocalKey); | 372 ASSERT(isolate_key == Thread::kUnsetThreadLocalKey); |
| 364 isolate_key = Thread::CreateThreadLocal(); | 373 isolate_key = Thread::CreateThreadLocal(); |
| 365 ASSERT(isolate_key != Thread::kUnsetThreadLocalKey); | 374 ASSERT(isolate_key != Thread::kUnsetThreadLocalKey); |
| 366 create_callback_ = NULL; | 375 create_callback_ = NULL; |
| 376 isolates_monitor_ = new Monitor(); | |
| 377 ASSERT(isolates_monitor_ != NULL); | |
| 367 } | 378 } |
| 368 | 379 |
| 369 | 380 |
| 370 Isolate* Isolate::Init(const char* name_prefix) { | 381 Isolate* Isolate::Init(const char* name_prefix) { |
| 371 Isolate* result = new Isolate(); | 382 Isolate* result = new Isolate(); |
| 372 ASSERT(result != NULL); | 383 ASSERT(result != NULL); |
| 373 | 384 |
| 374 // Setup for profiling. | 385 // Setup for profiling. |
| 375 Profiler::InitProfilingForIsolate(result); | 386 Profiler::InitProfilingForIsolate(result); |
| 376 | 387 |
| 388 // Add to isolate list. | |
| 389 IsolateCreated(result); | |
| 390 | |
| 377 // TODO(5411455): For now just set the recently created isolate as | 391 // TODO(5411455): For now just set the recently created isolate as |
| 378 // the current isolate. | 392 // the current isolate. |
| 379 SetCurrent(result); | 393 SetCurrent(result); |
| 380 | 394 |
| 381 // Setup the isolate specific resuable handles. | 395 // Setup the isolate specific resuable handles. |
| 382 #define REUSABLE_HANDLE_ALLOCATION(object) \ | 396 #define REUSABLE_HANDLE_ALLOCATION(object) \ |
| 383 result->object##_handle_ = result->AllocateReusableHandle<object>(); \ | 397 result->object##_handle_ = result->AllocateReusableHandle<object>(); \ |
| 384 | 398 |
| 385 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_ALLOCATION) | 399 REUSABLE_HANDLE_LIST(REUSABLE_HANDLE_ALLOCATION) |
| 386 #undef REUSABLE_HANDLE_ALLOCATION | 400 #undef REUSABLE_HANDLE_ALLOCATION |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 741 megamorphic_cache_table()->PrintSizes(); | 755 megamorphic_cache_table()->PrintSizes(); |
| 742 Symbols::DumpStats(); | 756 Symbols::DumpStats(); |
| 743 OS::Print("[-] Stopping isolate:\n" | 757 OS::Print("[-] Stopping isolate:\n" |
| 744 "\tisolate: %s\n", name()); | 758 "\tisolate: %s\n", name()); |
| 745 } | 759 } |
| 746 } | 760 } |
| 747 | 761 |
| 748 // TODO(5411455): For now just make sure there are no current isolates | 762 // TODO(5411455): For now just make sure there are no current isolates |
| 749 // as we are shutting down the isolate. | 763 // as we are shutting down the isolate. |
| 750 SetCurrent(NULL); | 764 SetCurrent(NULL); |
| 765 IsolateShutdown(this); | |
| 751 Profiler::ShutdownProfilingForIsolate(this); | 766 Profiler::ShutdownProfilingForIsolate(this); |
| 752 } | 767 } |
| 753 | 768 |
| 754 | 769 |
| 755 Dart_IsolateCreateCallback Isolate::create_callback_ = NULL; | 770 Dart_IsolateCreateCallback Isolate::create_callback_ = NULL; |
| 756 Dart_IsolateInterruptCallback Isolate::interrupt_callback_ = NULL; | 771 Dart_IsolateInterruptCallback Isolate::interrupt_callback_ = NULL; |
| 757 Dart_IsolateUnhandledExceptionCallback | 772 Dart_IsolateUnhandledExceptionCallback |
| 758 Isolate::unhandled_exception_callback_ = NULL; | 773 Isolate::unhandled_exception_callback_ = NULL; |
| 759 Dart_IsolateShutdownCallback Isolate::shutdown_callback_ = NULL; | 774 Dart_IsolateShutdownCallback Isolate::shutdown_callback_ = NULL; |
| 760 Dart_FileOpenCallback Isolate::file_open_callback_ = NULL; | 775 Dart_FileOpenCallback Isolate::file_open_callback_ = NULL; |
| 761 Dart_FileReadCallback Isolate::file_read_callback_ = NULL; | 776 Dart_FileReadCallback Isolate::file_read_callback_ = NULL; |
| 762 Dart_FileWriteCallback Isolate::file_write_callback_ = NULL; | 777 Dart_FileWriteCallback Isolate::file_write_callback_ = NULL; |
| 763 Dart_FileCloseCallback Isolate::file_close_callback_ = NULL; | 778 Dart_FileCloseCallback Isolate::file_close_callback_ = NULL; |
| 764 Dart_EntropySource Isolate::entropy_source_callback_ = NULL; | 779 Dart_EntropySource Isolate::entropy_source_callback_ = NULL; |
| 765 Dart_IsolateInterruptCallback Isolate::vmstats_callback_ = NULL; | 780 Dart_IsolateInterruptCallback Isolate::vmstats_callback_ = NULL; |
| 766 Dart_ServiceIsolateCreateCalback Isolate::service_create_callback_ = NULL; | 781 Dart_ServiceIsolateCreateCalback Isolate::service_create_callback_ = NULL; |
| 767 | 782 |
| 783 Monitor* Isolate::isolates_monitor_ = NULL; | |
| 784 Isolate** Isolate::isolates_ = NULL; | |
| 785 intptr_t Isolate::isolates_capacity_ = 0; | |
| 786 intptr_t Isolate::isolates_size_ = 0; | |
| 787 | |
| 768 void Isolate::VisitObjectPointers(ObjectPointerVisitor* visitor, | 788 void Isolate::VisitObjectPointers(ObjectPointerVisitor* visitor, |
| 769 bool visit_prologue_weak_handles, | 789 bool visit_prologue_weak_handles, |
| 770 bool validate_frames) { | 790 bool validate_frames) { |
| 771 ASSERT(visitor != NULL); | 791 ASSERT(visitor != NULL); |
| 772 | 792 |
| 773 // Visit objects in the object store. | 793 // Visit objects in the object store. |
| 774 object_store()->VisitObjectPointers(visitor); | 794 object_store()->VisitObjectPointers(visitor); |
| 775 | 795 |
| 776 // Visit objects in the class table. | 796 // Visit objects in the class table. |
| 777 class_table()->VisitObjectPointers(visitor); | 797 class_table()->VisitObjectPointers(visitor); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 } | 878 } |
| 859 | 879 |
| 860 const Library& lib = | 880 const Library& lib = |
| 861 Library::Handle(object_store()->root_library()); | 881 Library::Handle(object_store()->root_library()); |
| 862 jsobj.AddProperty("rootLib", lib); | 882 jsobj.AddProperty("rootLib", lib); |
| 863 | 883 |
| 864 timer_list().PrintTimersToJSONProperty(&jsobj); | 884 timer_list().PrintTimersToJSONProperty(&jsobj); |
| 865 } | 885 } |
| 866 | 886 |
| 867 | 887 |
| 888 void Isolate::IsolateCreated(Isolate* isolate) { | |
| 889 MonitorLocker ml(isolates_monitor_); | |
| 890 // Not currently registered in the isolates list. | |
| 891 ASSERT(FindIsolateIndex(isolate) < 0); | |
| 892 if (isolates_size_ == isolates_capacity_) { | |
| 893 // Full, grow isolates array. | |
| 894 ResizeIsolates(isolates_capacity_ == 0 ? 16 : isolates_capacity_ * 2); | |
| 895 } | |
| 896 // Add isolate to table. | |
| 897 isolates_[isolates_size_] = isolate; | |
| 898 isolates_size_++; | |
| 899 } | |
| 900 | |
| 901 | |
| 902 void Isolate::IsolateShutdown(Isolate* isolate) { | |
| 903 MonitorLocker ml(isolates_monitor_); | |
| 904 intptr_t index = FindIsolateIndex(isolate); | |
| 905 ASSERT(index >= 0); | |
| 906 intptr_t last = isolates_size_ - 1; | |
| 907 if (index != last) { | |
| 908 isolates_[index] = isolates_[last]; | |
| 909 } | |
| 910 isolates_[last] = NULL; | |
| 911 isolates_size_--; | |
| 912 } | |
| 913 | |
| 914 | |
| 915 void Isolate::ResizeIsolates(intptr_t new_capacity) { | |
| 916 // Must be called with isolates_monitor_ locked. | |
| 917 ASSERT(new_capacity > isolates_capacity_); | |
| 918 isolates_ = reinterpret_cast<Isolate**>( | |
| 919 realloc(isolates_, sizeof(*isolates_) * new_capacity)); | |
| 920 for (intptr_t i = isolates_capacity_; i < new_capacity; i++) { | |
| 921 isolates_[i] = NULL; | |
| 922 } | |
| 923 isolates_capacity_ = new_capacity; | |
| 924 } | |
|
siva
2014/02/05 19:09:15
See comment on using a linked list for isolates li
| |
| 925 | |
| 926 | |
| 927 intptr_t Isolate::FindIsolateIndex(Isolate* isolate) { | |
| 928 // Must be called with isolates_monitor_ locked. | |
| 929 for (intptr_t i = 0; i < isolates_size_; i++) { | |
| 930 if (isolates_[i] == isolate) { | |
| 931 return i; | |
| 932 } | |
| 933 } | |
| 934 return -1; | |
| 935 } | |
|
siva
2014/02/05 19:09:15
If a singly linked list is used for isolates this
| |
| 936 | |
| 937 | |
| 938 void Isolate::CheckNoIsolateHasThreadData(InterruptableThreadState* state) { | |
|
siva
2014/02/05 19:09:15
I would call this CheckForDuplicateThreadState and
Cutch
2014/02/05 23:00:31
Done.
| |
| 939 ASSERT(state != NULL); | |
| 940 MonitorLocker ml(isolates_monitor_); | |
| 941 for (intptr_t i = 0; i < isolates_size_; i++) { | |
| 942 Isolate* isolate = isolates_[i]; | |
| 943 ASSERT(isolate->thread_state() != state); | |
| 944 } | |
| 945 } | |
| 946 | |
| 947 | |
| 868 template<class T> | 948 template<class T> |
| 869 T* Isolate::AllocateReusableHandle() { | 949 T* Isolate::AllocateReusableHandle() { |
| 870 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); | 950 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); |
| 871 T::initializeHandle(handle, T::null()); | 951 T::initializeHandle(handle, T::null()); |
| 872 return handle; | 952 return handle; |
| 873 } | 953 } |
| 874 | 954 |
| 875 | 955 |
| 876 IsolateSpawnState::IsolateSpawnState(const Function& func) | 956 IsolateSpawnState::IsolateSpawnState(const Function& func) |
| 877 : isolate_(NULL), | 957 : isolate_(NULL), |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 970 return func.raw(); | 1050 return func.raw(); |
| 971 } | 1051 } |
| 972 | 1052 |
| 973 | 1053 |
| 974 void IsolateSpawnState::Cleanup() { | 1054 void IsolateSpawnState::Cleanup() { |
| 975 SwitchIsolateScope switch_scope(isolate()); | 1055 SwitchIsolateScope switch_scope(isolate()); |
| 976 Dart::ShutdownIsolate(); | 1056 Dart::ShutdownIsolate(); |
| 977 } | 1057 } |
| 978 | 1058 |
| 979 } // namespace dart | 1059 } // namespace dart |
| OLD | NEW |