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 "include/dart_native_api.h" | |
| 8 #include "platform/assert.h" | 9 #include "platform/assert.h" |
| 9 #include "platform/json.h" | 10 #include "platform/json.h" |
| 10 #include "vm/code_observers.h" | 11 #include "vm/code_observers.h" |
| 11 #include "vm/compiler_stats.h" | 12 #include "vm/compiler_stats.h" |
| 12 #include "vm/coverage.h" | 13 #include "vm/coverage.h" |
| 13 #include "vm/dart_api_state.h" | 14 #include "vm/dart_api_state.h" |
| 14 #include "vm/dart_entry.h" | 15 #include "vm/dart_entry.h" |
| 15 #include "vm/debugger.h" | 16 #include "vm/debugger.h" |
| 16 #include "vm/deopt_instructions.h" | 17 #include "vm/deopt_instructions.h" |
| 17 #include "vm/heap.h" | 18 #include "vm/heap.h" |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 void NotifyPauseOnStart(); | 151 void NotifyPauseOnStart(); |
| 151 void NotifyPauseOnExit(); | 152 void NotifyPauseOnExit(); |
| 152 | 153 |
| 153 #if defined(DEBUG) | 154 #if defined(DEBUG) |
| 154 // Check that it is safe to access this handler. | 155 // Check that it is safe to access this handler. |
| 155 void CheckAccess(); | 156 void CheckAccess(); |
| 156 #endif | 157 #endif |
| 157 bool IsCurrentIsolate() const; | 158 bool IsCurrentIsolate() const; |
| 158 virtual Isolate* isolate() const { return isolate_; } | 159 virtual Isolate* isolate() const { return isolate_; } |
| 159 | 160 |
| 160 private: | |
| 161 // Keep both these enums in sync with isolate_patch.dart. | 161 // Keep both these enums in sync with isolate_patch.dart. |
| 162 // The different Isolate API message types. | 162 // The different Isolate API message types. |
| 163 enum { | 163 enum { |
| 164 kPauseMsg = 1, | 164 kPauseMsg = 1, |
| 165 kResumeMsg = 2, | 165 kResumeMsg = 2, |
| 166 kPingMsg = 3, | 166 kPingMsg = 3, |
| 167 kKillMsg = 4, | 167 kKillMsg = 4, |
| 168 kAddExitMsg = 5, | 168 kAddExitMsg = 5, |
| 169 kDelExitMsg = 6, | 169 kDelExitMsg = 6, |
| 170 kAddErrorMsg = 7, | 170 kAddErrorMsg = 7, |
| 171 kDelErrorMsg = 8, | 171 kDelErrorMsg = 8, |
| 172 kErrorFatalMsg = 9, | 172 kErrorFatalMsg = 9, |
| 173 }; | 173 }; |
| 174 // The different Isolate API message priorities for ping and kill messages. | 174 // The different Isolate API message priorities for ping and kill messages. |
| 175 enum { | 175 enum { |
| 176 kImmediateAction = 0, | 176 kImmediateAction = 0, |
| 177 kBeforeNextEventAction = 1, | 177 kBeforeNextEventAction = 1, |
| 178 kAsEventAction = 2 | 178 kAsEventAction = 2 |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 private: | |
| 181 // A result of false indicates that the isolate should terminate the | 182 // A result of false indicates that the isolate should terminate the |
| 182 // processing of further events. | 183 // processing of further events. |
| 183 bool HandleLibMessage(const Array& message); | 184 bool HandleLibMessage(const Array& message); |
| 184 | 185 |
| 185 bool ProcessUnhandledException(const Error& result); | 186 bool ProcessUnhandledException(const Error& result); |
| 186 Isolate* isolate_; | 187 Isolate* isolate_; |
| 187 }; | 188 }; |
| 188 | 189 |
| 189 | 190 |
| 190 IsolateMessageHandler::IsolateMessageHandler(Isolate* isolate) | 191 IsolateMessageHandler::IsolateMessageHandler(Isolate* isolate) |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 bool Isolate::IsIsolateOf(Thread* thread) { | 736 bool Isolate::IsIsolateOf(Thread* thread) { |
| 736 return this == thread->isolate(); | 737 return this == thread->isolate(); |
| 737 } | 738 } |
| 738 #endif // DEBUG | 739 #endif // DEBUG |
| 739 | 740 |
| 740 | 741 |
| 741 void Isolate::InitOnce() { | 742 void Isolate::InitOnce() { |
| 742 create_callback_ = NULL; | 743 create_callback_ = NULL; |
| 743 isolates_list_monitor_ = new Monitor(); | 744 isolates_list_monitor_ = new Monitor(); |
| 744 ASSERT(isolates_list_monitor_ != NULL); | 745 ASSERT(isolates_list_monitor_ != NULL); |
| 746 creation_enabled_ = true; | |
|
turnidge
2015/08/04 21:39:02
Consider calling EnableIsolateCreation here?
zra
2015/08/05 06:23:06
Done.
| |
| 745 } | 747 } |
| 746 | 748 |
| 747 | 749 |
| 748 Isolate* Isolate::Init(const char* name_prefix, | 750 Isolate* Isolate::Init(const char* name_prefix, |
| 749 const Dart_IsolateFlags& api_flags, | 751 const Dart_IsolateFlags& api_flags, |
| 750 bool is_vm_isolate) { | 752 bool is_vm_isolate) { |
| 751 Isolate* result = new Isolate(api_flags); | 753 Isolate* result = new Isolate(api_flags); |
| 752 ASSERT(result != NULL); | 754 ASSERT(result != NULL); |
| 753 | 755 |
| 754 // Initialize metrics. | 756 // Initialize metrics. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 807 if (FLAG_trace_isolates) { | 809 if (FLAG_trace_isolates) { |
| 808 if (name_prefix == NULL || strcmp(name_prefix, "vm-isolate") != 0) { | 810 if (name_prefix == NULL || strcmp(name_prefix, "vm-isolate") != 0) { |
| 809 OS::Print("[+] Starting isolate:\n" | 811 OS::Print("[+] Starting isolate:\n" |
| 810 "\tisolate: %s\n", result->name()); | 812 "\tisolate: %s\n", result->name()); |
| 811 } | 813 } |
| 812 } | 814 } |
| 813 if (FLAG_compiler_stats) { | 815 if (FLAG_compiler_stats) { |
| 814 result->compiler_stats_ = new CompilerStats(result); | 816 result->compiler_stats_ = new CompilerStats(result); |
| 815 } | 817 } |
| 816 ObjectIdRing::Init(result); | 818 ObjectIdRing::Init(result); |
| 817 // Add to isolate list. | 819 |
| 818 AddIsolateTolist(result); | 820 // Add to isolate list. Shutdown and delete the isolate on failure. |
| 821 if (!AddIsolateToList(result)) { | |
| 822 result->Shutdown(); | |
| 823 delete result; | |
| 824 return NULL; | |
| 825 } | |
| 819 | 826 |
| 820 return result; | 827 return result; |
| 821 } | 828 } |
| 822 | 829 |
| 823 | 830 |
| 824 void Isolate::InitializeStackLimit() { | 831 void Isolate::InitializeStackLimit() { |
| 825 SetStackLimitFromStackBase(Isolate::GetCurrentStackPointer()); | 832 SetStackLimitFromStackBase(Isolate::GetCurrentStackPointer()); |
| 826 } | 833 } |
| 827 | 834 |
| 828 | 835 |
| (...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1451 } | 1458 } |
| 1452 #endif // DEBUG | 1459 #endif // DEBUG |
| 1453 | 1460 |
| 1454 // First, perform higher-level cleanup that may need to allocate. | 1461 // First, perform higher-level cleanup that may need to allocate. |
| 1455 { | 1462 { |
| 1456 // Ensure we have a zone and handle scope so that we can call VM functions. | 1463 // Ensure we have a zone and handle scope so that we can call VM functions. |
| 1457 StackZone stack_zone(this); | 1464 StackZone stack_zone(this); |
| 1458 HandleScope handle_scope(this); | 1465 HandleScope handle_scope(this); |
| 1459 | 1466 |
| 1460 // Write out the coverage data if collection has been enabled. | 1467 // Write out the coverage data if collection has been enabled. |
| 1461 CodeCoverage::Write(this); | 1468 if ((this != Dart::vm_isolate()) && |
| 1469 !ServiceIsolate::IsServiceIsolateDescendant(this)) { | |
| 1470 CodeCoverage::Write(this); | |
| 1471 } | |
| 1462 | 1472 |
| 1463 if ((timeline_event_recorder_ != NULL) && | 1473 if ((timeline_event_recorder_ != NULL) && |
| 1464 (FLAG_timeline_trace_dir != NULL)) { | 1474 (FLAG_timeline_trace_dir != NULL)) { |
| 1465 timeline_event_recorder_->WriteTo(FLAG_timeline_trace_dir); | 1475 timeline_event_recorder_->WriteTo(FLAG_timeline_trace_dir); |
| 1466 } | 1476 } |
| 1467 } | 1477 } |
| 1468 | 1478 |
| 1469 // Remove this isolate from the list *before* we start tearing it down, to | 1479 // Remove this isolate from the list *before* we start tearing it down, to |
| 1470 // avoid exposing it in a state of decay. | 1480 // avoid exposing it in a state of decay. |
| 1471 RemoveIsolateFromList(this); | 1481 RemoveIsolateFromList(this); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1548 Isolate::unhandled_exception_callback_ = NULL; | 1558 Isolate::unhandled_exception_callback_ = NULL; |
| 1549 Dart_IsolateShutdownCallback Isolate::shutdown_callback_ = NULL; | 1559 Dart_IsolateShutdownCallback Isolate::shutdown_callback_ = NULL; |
| 1550 Dart_FileOpenCallback Isolate::file_open_callback_ = NULL; | 1560 Dart_FileOpenCallback Isolate::file_open_callback_ = NULL; |
| 1551 Dart_FileReadCallback Isolate::file_read_callback_ = NULL; | 1561 Dart_FileReadCallback Isolate::file_read_callback_ = NULL; |
| 1552 Dart_FileWriteCallback Isolate::file_write_callback_ = NULL; | 1562 Dart_FileWriteCallback Isolate::file_write_callback_ = NULL; |
| 1553 Dart_FileCloseCallback Isolate::file_close_callback_ = NULL; | 1563 Dart_FileCloseCallback Isolate::file_close_callback_ = NULL; |
| 1554 Dart_EntropySource Isolate::entropy_source_callback_ = NULL; | 1564 Dart_EntropySource Isolate::entropy_source_callback_ = NULL; |
| 1555 | 1565 |
| 1556 Monitor* Isolate::isolates_list_monitor_ = NULL; | 1566 Monitor* Isolate::isolates_list_monitor_ = NULL; |
| 1557 Isolate* Isolate::isolates_list_head_ = NULL; | 1567 Isolate* Isolate::isolates_list_head_ = NULL; |
| 1558 | 1568 bool Isolate::creation_enabled_ = true; |
|
turnidge
2015/08/04 21:39:02
Maybe we should start this as false.
zra
2015/08/05 06:23:06
Done.
| |
| 1559 | 1569 |
| 1560 void Isolate::IterateObjectPointers(ObjectPointerVisitor* visitor, | 1570 void Isolate::IterateObjectPointers(ObjectPointerVisitor* visitor, |
| 1561 bool visit_prologue_weak_handles, | 1571 bool visit_prologue_weak_handles, |
| 1562 bool validate_frames) { | 1572 bool validate_frames) { |
| 1563 HeapIterationScope heap_iteration_scope; | 1573 HeapIterationScope heap_iteration_scope; |
| 1564 VisitObjectPointers(visitor, visit_prologue_weak_handles, validate_frames); | 1574 VisitObjectPointers(visitor, visit_prologue_weak_handles, validate_frames); |
| 1565 } | 1575 } |
| 1566 | 1576 |
| 1567 | 1577 |
| 1568 void Isolate::VisitObjectPointers(ObjectPointerVisitor* visitor, | 1578 void Isolate::VisitObjectPointers(ObjectPointerVisitor* visitor, |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1897 intptr_t count = 0; | 1907 intptr_t count = 0; |
| 1898 Isolate* current = isolates_list_head_; | 1908 Isolate* current = isolates_list_head_; |
| 1899 while (current != NULL) { | 1909 while (current != NULL) { |
| 1900 count++; | 1910 count++; |
| 1901 current = current->next_; | 1911 current = current->next_; |
| 1902 } | 1912 } |
| 1903 return count; | 1913 return count; |
| 1904 } | 1914 } |
| 1905 | 1915 |
| 1906 | 1916 |
| 1907 void Isolate::AddIsolateTolist(Isolate* isolate) { | 1917 bool Isolate::AddIsolateToList(Isolate* isolate) { |
| 1908 MonitorLocker ml(isolates_list_monitor_); | 1918 MonitorLocker ml(isolates_list_monitor_); |
| 1919 if (!creation_enabled_) { | |
| 1920 return false; | |
| 1921 } | |
| 1909 ASSERT(isolate != NULL); | 1922 ASSERT(isolate != NULL); |
| 1910 ASSERT(isolate->next_ == NULL); | 1923 ASSERT(isolate->next_ == NULL); |
| 1911 isolate->next_ = isolates_list_head_; | 1924 isolate->next_ = isolates_list_head_; |
| 1912 isolates_list_head_ = isolate; | 1925 isolates_list_head_ = isolate; |
| 1926 return true; | |
| 1913 } | 1927 } |
| 1914 | 1928 |
| 1915 | 1929 |
| 1916 void Isolate::RemoveIsolateFromList(Isolate* isolate) { | 1930 void Isolate::RemoveIsolateFromList(Isolate* isolate) { |
| 1917 MonitorLocker ml(isolates_list_monitor_); | 1931 MonitorLocker ml(isolates_list_monitor_); |
| 1918 ASSERT(isolate != NULL); | 1932 ASSERT(isolate != NULL); |
| 1919 if (isolate == isolates_list_head_) { | 1933 if (isolate == isolates_list_head_) { |
| 1920 isolates_list_head_ = isolate->next_; | 1934 isolates_list_head_ = isolate->next_; |
| 1921 return; | 1935 return; |
| 1922 } | 1936 } |
| 1923 Isolate* previous = NULL; | 1937 Isolate* previous = NULL; |
| 1924 Isolate* current = isolates_list_head_; | 1938 Isolate* current = isolates_list_head_; |
| 1925 while (current) { | 1939 while (current) { |
| 1926 if (current == isolate) { | 1940 if (current == isolate) { |
| 1927 ASSERT(previous != NULL); | 1941 ASSERT(previous != NULL); |
| 1928 previous->next_ = current->next_; | 1942 previous->next_ = current->next_; |
| 1929 return; | 1943 return; |
| 1930 } | 1944 } |
| 1931 previous = current; | 1945 previous = current; |
| 1932 current = current->next_; | 1946 current = current->next_; |
| 1933 } | 1947 } |
| 1934 UNREACHABLE(); | 1948 // If we are shutting down an isolate that tried to start after the VM |
| 1949 // started going down, then it may not be in the list. | |
|
turnidge
2015/08/04 21:39:02
I feel like there is a simpler version of this com
zra
2015/08/05 06:23:06
Done.
| |
| 1950 ASSERT(!creation_enabled_); | |
| 1935 } | 1951 } |
| 1936 | 1952 |
| 1937 | 1953 |
| 1938 #if defined(DEBUG) | 1954 #if defined(DEBUG) |
| 1939 void Isolate::CheckForDuplicateThreadState(InterruptableThreadState* state) { | 1955 void Isolate::CheckForDuplicateThreadState(InterruptableThreadState* state) { |
| 1940 MonitorLocker ml(isolates_list_monitor_); | 1956 MonitorLocker ml(isolates_list_monitor_); |
| 1941 ASSERT(state != NULL); | 1957 ASSERT(state != NULL); |
| 1942 Isolate* current = isolates_list_head_; | 1958 Isolate* current = isolates_list_head_; |
| 1943 while (current) { | 1959 while (current) { |
| 1944 ASSERT(current->thread_state() != state); | 1960 ASSERT(current->thread_state() != state); |
| 1945 current = current->next_; | 1961 current = current->next_; |
| 1946 } | 1962 } |
| 1947 } | 1963 } |
| 1964 | |
| 1965 | |
| 1966 int Isolate::IsolateCount() { | |
| 1967 MonitorLocker ml(isolates_list_monitor_); | |
| 1968 Isolate* current = isolates_list_head_; | |
| 1969 int count = 0; | |
| 1970 while (current) { | |
| 1971 count++; | |
| 1972 current = current->next_; | |
| 1973 } | |
| 1974 return count; | |
| 1975 } | |
| 1948 #endif | 1976 #endif |
| 1949 | 1977 |
| 1950 | 1978 |
| 1979 void Isolate::DisableIsolateCreation() { | |
| 1980 MonitorLocker ml(isolates_list_monitor_); | |
| 1981 creation_enabled_ = false; | |
| 1982 } | |
| 1983 | |
| 1984 | |
| 1985 void Isolate::EnableIsolateCreation() { | |
| 1986 MonitorLocker ml(isolates_list_monitor_); | |
| 1987 creation_enabled_ = true; | |
| 1988 } | |
| 1989 | |
| 1990 | |
| 1951 template<class T> | 1991 template<class T> |
| 1952 T* Isolate::AllocateReusableHandle() { | 1992 T* Isolate::AllocateReusableHandle() { |
| 1953 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); | 1993 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); |
| 1954 T::initializeHandle(handle, T::null()); | 1994 T::initializeHandle(handle, T::null()); |
| 1955 return handle; | 1995 return handle; |
| 1956 } | 1996 } |
| 1957 | 1997 |
| 1958 | 1998 |
| 1999 void Isolate::KillIsolate(Isolate* isolate) { | |
| 2000 Dart_CObject kill_msg; | |
| 2001 Dart_CObject* list_values[4]; | |
| 2002 kill_msg.type = Dart_CObject_kArray; | |
| 2003 kill_msg.value.as_array.length = 4; | |
| 2004 kill_msg.value.as_array.values = list_values; | |
| 2005 | |
| 2006 Dart_CObject oob; | |
| 2007 oob.type = Dart_CObject_kInt32; | |
| 2008 oob.value.as_int32 = Message::kIsolateLibOOBMsg; | |
| 2009 list_values[0] = &oob; | |
| 2010 | |
| 2011 Dart_CObject kill; | |
| 2012 kill.type = Dart_CObject_kInt32; | |
| 2013 kill.value.as_int32 = IsolateMessageHandler::kKillMsg; | |
| 2014 list_values[1] = &kill; | |
| 2015 | |
| 2016 Dart_CObject cap; | |
| 2017 cap.type = Dart_CObject_kCapability; | |
| 2018 cap.value.as_capability.id = isolate->terminate_capability(); | |
| 2019 list_values[2] = ∩ | |
| 2020 | |
| 2021 Dart_CObject imm; | |
| 2022 imm.type = Dart_CObject_kInt32; | |
| 2023 imm.value.as_int32 = IsolateMessageHandler::kImmediateAction; | |
| 2024 list_values[3] = &imm; | |
| 2025 | |
| 2026 isolate->ScheduleInterrupts(Isolate::kMessageInterrupt); | |
| 2027 Dart_PostOOBCObject(isolate->main_port(), &kill_msg); | |
| 2028 } | |
| 2029 | |
| 2030 | |
| 2031 class IsolateKillerVisitor : public IsolateVisitor { | |
| 2032 public: | |
| 2033 IsolateKillerVisitor() {} | |
| 2034 | |
| 2035 virtual ~IsolateKillerVisitor() {} | |
| 2036 | |
| 2037 void VisitIsolate(Isolate* isolate) { | |
| 2038 ASSERT(isolate != NULL); | |
| 2039 if (ServiceIsolate::IsServiceIsolateDescendant(isolate) || | |
| 2040 (isolate == Dart::vm_isolate())) { | |
| 2041 return; | |
| 2042 } | |
| 2043 Isolate::KillIsolate(isolate); | |
| 2044 } | |
| 2045 }; | |
| 2046 | |
| 2047 | |
| 2048 void Isolate::KillAllIsolates() { | |
| 2049 IsolateKillerVisitor visitor; | |
| 2050 VisitIsolates(&visitor); | |
| 2051 } | |
| 2052 | |
| 2053 | |
| 1959 static RawInstance* DeserializeObject(Isolate* isolate, | 2054 static RawInstance* DeserializeObject(Isolate* isolate, |
| 1960 Zone* zone, | 2055 Zone* zone, |
| 1961 uint8_t* obj_data, | 2056 uint8_t* obj_data, |
| 1962 intptr_t obj_len) { | 2057 intptr_t obj_len) { |
| 1963 if (obj_data == NULL) { | 2058 if (obj_data == NULL) { |
| 1964 return Instance::null(); | 2059 return Instance::null(); |
| 1965 } | 2060 } |
| 1966 MessageSnapshotReader reader(obj_data, | 2061 MessageSnapshotReader reader(obj_data, |
| 1967 obj_len, | 2062 obj_len, |
| 1968 isolate, | 2063 isolate, |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2155 serialized_message_, serialized_message_len_); | 2250 serialized_message_, serialized_message_len_); |
| 2156 } | 2251 } |
| 2157 | 2252 |
| 2158 | 2253 |
| 2159 void IsolateSpawnState::Cleanup() { | 2254 void IsolateSpawnState::Cleanup() { |
| 2160 SwitchIsolateScope switch_scope(I); | 2255 SwitchIsolateScope switch_scope(I); |
| 2161 Dart::ShutdownIsolate(); | 2256 Dart::ShutdownIsolate(); |
| 2162 } | 2257 } |
| 2163 | 2258 |
| 2164 } // namespace dart | 2259 } // namespace dart |
| OLD | NEW |