| 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" | |
| 9 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 10 #include "platform/json.h" | 9 #include "platform/json.h" |
| 11 #include "vm/code_observers.h" | 10 #include "vm/code_observers.h" |
| 12 #include "vm/compiler_stats.h" | 11 #include "vm/compiler_stats.h" |
| 13 #include "vm/coverage.h" | 12 #include "vm/coverage.h" |
| 14 #include "vm/dart_api_message.h" | |
| 15 #include "vm/dart_api_state.h" | 13 #include "vm/dart_api_state.h" |
| 16 #include "vm/dart_entry.h" | 14 #include "vm/dart_entry.h" |
| 17 #include "vm/debugger.h" | 15 #include "vm/debugger.h" |
| 18 #include "vm/deopt_instructions.h" | 16 #include "vm/deopt_instructions.h" |
| 19 #include "vm/heap.h" | 17 #include "vm/heap.h" |
| 20 #include "vm/lockers.h" | 18 #include "vm/lockers.h" |
| 21 #include "vm/log.h" | 19 #include "vm/log.h" |
| 22 #include "vm/message_handler.h" | 20 #include "vm/message_handler.h" |
| 23 #include "vm/object_id_ring.h" | 21 #include "vm/object_id_ring.h" |
| 24 #include "vm/object_store.h" | 22 #include "vm/object_store.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 void NotifyPauseOnStart(); | 158 void NotifyPauseOnStart(); |
| 161 void NotifyPauseOnExit(); | 159 void NotifyPauseOnExit(); |
| 162 | 160 |
| 163 #if defined(DEBUG) | 161 #if defined(DEBUG) |
| 164 // Check that it is safe to access this handler. | 162 // Check that it is safe to access this handler. |
| 165 void CheckAccess(); | 163 void CheckAccess(); |
| 166 #endif | 164 #endif |
| 167 bool IsCurrentIsolate() const; | 165 bool IsCurrentIsolate() const; |
| 168 virtual Isolate* isolate() const { return isolate_; } | 166 virtual Isolate* isolate() const { return isolate_; } |
| 169 | 167 |
| 168 private: |
| 170 // Keep both these enums in sync with isolate_patch.dart. | 169 // Keep both these enums in sync with isolate_patch.dart. |
| 171 // The different Isolate API message types. | 170 // The different Isolate API message types. |
| 172 enum { | 171 enum { |
| 173 kPauseMsg = 1, | 172 kPauseMsg = 1, |
| 174 kResumeMsg = 2, | 173 kResumeMsg = 2, |
| 175 kPingMsg = 3, | 174 kPingMsg = 3, |
| 176 kKillMsg = 4, | 175 kKillMsg = 4, |
| 177 kAddExitMsg = 5, | 176 kAddExitMsg = 5, |
| 178 kDelExitMsg = 6, | 177 kDelExitMsg = 6, |
| 179 kAddErrorMsg = 7, | 178 kAddErrorMsg = 7, |
| 180 kDelErrorMsg = 8, | 179 kDelErrorMsg = 8, |
| 181 kErrorFatalMsg = 9, | 180 kErrorFatalMsg = 9, |
| 182 }; | 181 }; |
| 183 // The different Isolate API message priorities for ping and kill messages. | 182 // The different Isolate API message priorities for ping and kill messages. |
| 184 enum { | 183 enum { |
| 185 kImmediateAction = 0, | 184 kImmediateAction = 0, |
| 186 kBeforeNextEventAction = 1, | 185 kBeforeNextEventAction = 1, |
| 187 kAsEventAction = 2 | 186 kAsEventAction = 2 |
| 188 }; | 187 }; |
| 189 | 188 |
| 190 private: | |
| 191 // A result of false indicates that the isolate should terminate the | 189 // A result of false indicates that the isolate should terminate the |
| 192 // processing of further events. | 190 // processing of further events. |
| 193 bool HandleLibMessage(const Array& message); | 191 bool HandleLibMessage(const Array& message); |
| 194 | 192 |
| 195 bool ProcessUnhandledException(const Error& result); | 193 bool ProcessUnhandledException(const Error& result); |
| 196 Isolate* isolate_; | 194 Isolate* isolate_; |
| 197 }; | 195 }; |
| 198 | 196 |
| 199 | 197 |
| 200 IsolateMessageHandler::IsolateMessageHandler(Isolate* isolate) | 198 IsolateMessageHandler::IsolateMessageHandler(Isolate* isolate) |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 bool Isolate::IsIsolateOf(Thread* thread) { | 743 bool Isolate::IsIsolateOf(Thread* thread) { |
| 746 return this == thread->isolate(); | 744 return this == thread->isolate(); |
| 747 } | 745 } |
| 748 #endif // DEBUG | 746 #endif // DEBUG |
| 749 | 747 |
| 750 | 748 |
| 751 void Isolate::InitOnce() { | 749 void Isolate::InitOnce() { |
| 752 create_callback_ = NULL; | 750 create_callback_ = NULL; |
| 753 isolates_list_monitor_ = new Monitor(); | 751 isolates_list_monitor_ = new Monitor(); |
| 754 ASSERT(isolates_list_monitor_ != NULL); | 752 ASSERT(isolates_list_monitor_ != NULL); |
| 755 EnableIsolateCreation(); | |
| 756 } | 753 } |
| 757 | 754 |
| 758 | 755 |
| 759 Isolate* Isolate::Init(const char* name_prefix, | 756 Isolate* Isolate::Init(const char* name_prefix, |
| 760 const Dart_IsolateFlags& api_flags, | 757 const Dart_IsolateFlags& api_flags, |
| 761 bool is_vm_isolate) { | 758 bool is_vm_isolate) { |
| 762 Isolate* result = new Isolate(api_flags); | 759 Isolate* result = new Isolate(api_flags); |
| 763 ASSERT(result != NULL); | 760 ASSERT(result != NULL); |
| 764 | 761 |
| 765 // Initialize metrics. | 762 // Initialize metrics. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 if (FLAG_trace_isolates) { | 821 if (FLAG_trace_isolates) { |
| 825 if (name_prefix == NULL || strcmp(name_prefix, "vm-isolate") != 0) { | 822 if (name_prefix == NULL || strcmp(name_prefix, "vm-isolate") != 0) { |
| 826 OS::Print("[+] Starting isolate:\n" | 823 OS::Print("[+] Starting isolate:\n" |
| 827 "\tisolate: %s\n", result->name()); | 824 "\tisolate: %s\n", result->name()); |
| 828 } | 825 } |
| 829 } | 826 } |
| 830 if (FLAG_compiler_stats) { | 827 if (FLAG_compiler_stats) { |
| 831 result->compiler_stats_ = new CompilerStats(result); | 828 result->compiler_stats_ = new CompilerStats(result); |
| 832 } | 829 } |
| 833 ObjectIdRing::Init(result); | 830 ObjectIdRing::Init(result); |
| 834 | 831 // Add to isolate list. |
| 835 // Add to isolate list. Shutdown and delete the isolate on failure. | 832 AddIsolateTolist(result); |
| 836 if (!AddIsolateToList(result)) { | |
| 837 result->Shutdown(); | |
| 838 delete result; | |
| 839 return NULL; | |
| 840 } | |
| 841 | 833 |
| 842 return result; | 834 return result; |
| 843 } | 835 } |
| 844 | 836 |
| 845 | 837 |
| 846 void Isolate::InitializeStackLimit() { | 838 void Isolate::InitializeStackLimit() { |
| 847 SetStackLimitFromStackBase(Isolate::GetCurrentStackPointer()); | 839 SetStackLimitFromStackBase(Isolate::GetCurrentStackPointer()); |
| 848 } | 840 } |
| 849 | 841 |
| 850 | 842 |
| (...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1473 } | 1465 } |
| 1474 #endif // DEBUG | 1466 #endif // DEBUG |
| 1475 | 1467 |
| 1476 // First, perform higher-level cleanup that may need to allocate. | 1468 // First, perform higher-level cleanup that may need to allocate. |
| 1477 { | 1469 { |
| 1478 // Ensure we have a zone and handle scope so that we can call VM functions. | 1470 // Ensure we have a zone and handle scope so that we can call VM functions. |
| 1479 StackZone stack_zone(this); | 1471 StackZone stack_zone(this); |
| 1480 HandleScope handle_scope(this); | 1472 HandleScope handle_scope(this); |
| 1481 | 1473 |
| 1482 // Write out the coverage data if collection has been enabled. | 1474 // Write out the coverage data if collection has been enabled. |
| 1483 if ((this != Dart::vm_isolate()) && | 1475 CodeCoverage::Write(this); |
| 1484 !ServiceIsolate::IsServiceIsolateDescendant(this)) { | |
| 1485 CodeCoverage::Write(this); | |
| 1486 } | |
| 1487 | 1476 |
| 1488 if ((timeline_event_recorder_ != NULL) && | 1477 if ((timeline_event_recorder_ != NULL) && |
| 1489 (FLAG_timeline_trace_dir != NULL)) { | 1478 (FLAG_timeline_trace_dir != NULL)) { |
| 1490 timeline_event_recorder_->WriteTo(FLAG_timeline_trace_dir); | 1479 timeline_event_recorder_->WriteTo(FLAG_timeline_trace_dir); |
| 1491 } | 1480 } |
| 1492 } | 1481 } |
| 1493 | 1482 |
| 1494 // Remove this isolate from the list *before* we start tearing it down, to | 1483 // Remove this isolate from the list *before* we start tearing it down, to |
| 1495 // avoid exposing it in a state of decay. | 1484 // avoid exposing it in a state of decay. |
| 1496 RemoveIsolateFromList(this); | 1485 RemoveIsolateFromList(this); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1573 Isolate::unhandled_exception_callback_ = NULL; | 1562 Isolate::unhandled_exception_callback_ = NULL; |
| 1574 Dart_IsolateShutdownCallback Isolate::shutdown_callback_ = NULL; | 1563 Dart_IsolateShutdownCallback Isolate::shutdown_callback_ = NULL; |
| 1575 Dart_FileOpenCallback Isolate::file_open_callback_ = NULL; | 1564 Dart_FileOpenCallback Isolate::file_open_callback_ = NULL; |
| 1576 Dart_FileReadCallback Isolate::file_read_callback_ = NULL; | 1565 Dart_FileReadCallback Isolate::file_read_callback_ = NULL; |
| 1577 Dart_FileWriteCallback Isolate::file_write_callback_ = NULL; | 1566 Dart_FileWriteCallback Isolate::file_write_callback_ = NULL; |
| 1578 Dart_FileCloseCallback Isolate::file_close_callback_ = NULL; | 1567 Dart_FileCloseCallback Isolate::file_close_callback_ = NULL; |
| 1579 Dart_EntropySource Isolate::entropy_source_callback_ = NULL; | 1568 Dart_EntropySource Isolate::entropy_source_callback_ = NULL; |
| 1580 | 1569 |
| 1581 Monitor* Isolate::isolates_list_monitor_ = NULL; | 1570 Monitor* Isolate::isolates_list_monitor_ = NULL; |
| 1582 Isolate* Isolate::isolates_list_head_ = NULL; | 1571 Isolate* Isolate::isolates_list_head_ = NULL; |
| 1583 bool Isolate::creation_enabled_ = false; | 1572 |
| 1584 | 1573 |
| 1585 void Isolate::IterateObjectPointers(ObjectPointerVisitor* visitor, | 1574 void Isolate::IterateObjectPointers(ObjectPointerVisitor* visitor, |
| 1586 bool visit_prologue_weak_handles, | 1575 bool visit_prologue_weak_handles, |
| 1587 bool validate_frames) { | 1576 bool validate_frames) { |
| 1588 HeapIterationScope heap_iteration_scope; | 1577 HeapIterationScope heap_iteration_scope; |
| 1589 VisitObjectPointers(visitor, visit_prologue_weak_handles, validate_frames); | 1578 VisitObjectPointers(visitor, visit_prologue_weak_handles, validate_frames); |
| 1590 } | 1579 } |
| 1591 | 1580 |
| 1592 | 1581 |
| 1593 void Isolate::VisitObjectPointers(ObjectPointerVisitor* visitor, | 1582 void Isolate::VisitObjectPointers(ObjectPointerVisitor* visitor, |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1922 intptr_t count = 0; | 1911 intptr_t count = 0; |
| 1923 Isolate* current = isolates_list_head_; | 1912 Isolate* current = isolates_list_head_; |
| 1924 while (current != NULL) { | 1913 while (current != NULL) { |
| 1925 count++; | 1914 count++; |
| 1926 current = current->next_; | 1915 current = current->next_; |
| 1927 } | 1916 } |
| 1928 return count; | 1917 return count; |
| 1929 } | 1918 } |
| 1930 | 1919 |
| 1931 | 1920 |
| 1932 bool Isolate::AddIsolateToList(Isolate* isolate) { | 1921 void Isolate::AddIsolateTolist(Isolate* isolate) { |
| 1933 MonitorLocker ml(isolates_list_monitor_); | 1922 MonitorLocker ml(isolates_list_monitor_); |
| 1934 if (!creation_enabled_) { | |
| 1935 return false; | |
| 1936 } | |
| 1937 ASSERT(isolate != NULL); | 1923 ASSERT(isolate != NULL); |
| 1938 ASSERT(isolate->next_ == NULL); | 1924 ASSERT(isolate->next_ == NULL); |
| 1939 isolate->next_ = isolates_list_head_; | 1925 isolate->next_ = isolates_list_head_; |
| 1940 isolates_list_head_ = isolate; | 1926 isolates_list_head_ = isolate; |
| 1941 return true; | |
| 1942 } | 1927 } |
| 1943 | 1928 |
| 1944 | 1929 |
| 1945 void Isolate::RemoveIsolateFromList(Isolate* isolate) { | 1930 void Isolate::RemoveIsolateFromList(Isolate* isolate) { |
| 1946 MonitorLocker ml(isolates_list_monitor_); | 1931 MonitorLocker ml(isolates_list_monitor_); |
| 1947 ASSERT(isolate != NULL); | 1932 ASSERT(isolate != NULL); |
| 1948 if (isolate == isolates_list_head_) { | 1933 if (isolate == isolates_list_head_) { |
| 1949 isolates_list_head_ = isolate->next_; | 1934 isolates_list_head_ = isolate->next_; |
| 1950 return; | 1935 return; |
| 1951 } | 1936 } |
| 1952 Isolate* previous = NULL; | 1937 Isolate* previous = NULL; |
| 1953 Isolate* current = isolates_list_head_; | 1938 Isolate* current = isolates_list_head_; |
| 1954 while (current) { | 1939 while (current) { |
| 1955 if (current == isolate) { | 1940 if (current == isolate) { |
| 1956 ASSERT(previous != NULL); | 1941 ASSERT(previous != NULL); |
| 1957 previous->next_ = current->next_; | 1942 previous->next_ = current->next_; |
| 1958 return; | 1943 return; |
| 1959 } | 1944 } |
| 1960 previous = current; | 1945 previous = current; |
| 1961 current = current->next_; | 1946 current = current->next_; |
| 1962 } | 1947 } |
| 1963 // If we are shutting down the VM, the isolate may not be in the list. | 1948 UNREACHABLE(); |
| 1964 ASSERT(!creation_enabled_); | |
| 1965 } | 1949 } |
| 1966 | 1950 |
| 1967 | 1951 |
| 1968 #if defined(DEBUG) | 1952 #if defined(DEBUG) |
| 1969 void Isolate::CheckForDuplicateThreadState(InterruptableThreadState* state) { | 1953 void Isolate::CheckForDuplicateThreadState(InterruptableThreadState* state) { |
| 1970 MonitorLocker ml(isolates_list_monitor_); | 1954 MonitorLocker ml(isolates_list_monitor_); |
| 1971 ASSERT(state != NULL); | 1955 ASSERT(state != NULL); |
| 1972 Isolate* current = isolates_list_head_; | 1956 Isolate* current = isolates_list_head_; |
| 1973 while (current) { | 1957 while (current) { |
| 1974 ASSERT(current->thread_state() != state); | 1958 ASSERT(current->thread_state() != state); |
| 1975 current = current->next_; | 1959 current = current->next_; |
| 1976 } | 1960 } |
| 1977 } | 1961 } |
| 1978 | |
| 1979 | |
| 1980 int Isolate::IsolateCount() { | |
| 1981 MonitorLocker ml(isolates_list_monitor_); | |
| 1982 Isolate* current = isolates_list_head_; | |
| 1983 int count = 0; | |
| 1984 while (current) { | |
| 1985 count++; | |
| 1986 current = current->next_; | |
| 1987 } | |
| 1988 return count; | |
| 1989 } | |
| 1990 #endif | 1962 #endif |
| 1991 | 1963 |
| 1992 | 1964 |
| 1993 void Isolate::DisableIsolateCreation() { | |
| 1994 MonitorLocker ml(isolates_list_monitor_); | |
| 1995 creation_enabled_ = false; | |
| 1996 } | |
| 1997 | |
| 1998 | |
| 1999 void Isolate::EnableIsolateCreation() { | |
| 2000 MonitorLocker ml(isolates_list_monitor_); | |
| 2001 creation_enabled_ = true; | |
| 2002 } | |
| 2003 | |
| 2004 | |
| 2005 template<class T> | 1965 template<class T> |
| 2006 T* Isolate::AllocateReusableHandle() { | 1966 T* Isolate::AllocateReusableHandle() { |
| 2007 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); | 1967 T* handle = reinterpret_cast<T*>(reusable_handles_.AllocateScopedHandle()); |
| 2008 T::initializeHandle(handle, T::null()); | 1968 T::initializeHandle(handle, T::null()); |
| 2009 return handle; | 1969 return handle; |
| 2010 } | 1970 } |
| 2011 | 1971 |
| 2012 | 1972 |
| 2013 void Isolate::KillIsolate(Isolate* isolate) { | |
| 2014 Dart_CObject kill_msg; | |
| 2015 Dart_CObject* list_values[4]; | |
| 2016 kill_msg.type = Dart_CObject_kArray; | |
| 2017 kill_msg.value.as_array.length = 4; | |
| 2018 kill_msg.value.as_array.values = list_values; | |
| 2019 | |
| 2020 Dart_CObject oob; | |
| 2021 oob.type = Dart_CObject_kInt32; | |
| 2022 oob.value.as_int32 = Message::kIsolateLibOOBMsg; | |
| 2023 list_values[0] = &oob; | |
| 2024 | |
| 2025 Dart_CObject kill; | |
| 2026 kill.type = Dart_CObject_kInt32; | |
| 2027 kill.value.as_int32 = IsolateMessageHandler::kKillMsg; | |
| 2028 list_values[1] = &kill; | |
| 2029 | |
| 2030 Dart_CObject cap; | |
| 2031 cap.type = Dart_CObject_kCapability; | |
| 2032 cap.value.as_capability.id = isolate->terminate_capability(); | |
| 2033 list_values[2] = ∩ | |
| 2034 | |
| 2035 Dart_CObject imm; | |
| 2036 imm.type = Dart_CObject_kInt32; | |
| 2037 imm.value.as_int32 = IsolateMessageHandler::kImmediateAction; | |
| 2038 list_values[3] = &imm; | |
| 2039 | |
| 2040 isolate->ScheduleInterrupts(Isolate::kMessageInterrupt); | |
| 2041 { | |
| 2042 uint8_t* buffer = NULL; | |
| 2043 ApiMessageWriter writer(&buffer, allocator); | |
| 2044 bool success = writer.WriteCMessage(&kill_msg); | |
| 2045 ASSERT(success); | |
| 2046 | |
| 2047 // Post the message at the given port. | |
| 2048 success = PortMap::PostMessage(new Message(isolate->main_port(), | |
| 2049 buffer, | |
| 2050 writer.BytesWritten(), | |
| 2051 Message::kOOBPriority)); | |
| 2052 ASSERT(success); | |
| 2053 } | |
| 2054 } | |
| 2055 | |
| 2056 | |
| 2057 class IsolateKillerVisitor : public IsolateVisitor { | |
| 2058 public: | |
| 2059 IsolateKillerVisitor() {} | |
| 2060 | |
| 2061 virtual ~IsolateKillerVisitor() {} | |
| 2062 | |
| 2063 void VisitIsolate(Isolate* isolate) { | |
| 2064 ASSERT(isolate != NULL); | |
| 2065 if (ServiceIsolate::IsServiceIsolateDescendant(isolate) || | |
| 2066 (isolate == Dart::vm_isolate())) { | |
| 2067 return; | |
| 2068 } | |
| 2069 Isolate::KillIsolate(isolate); | |
| 2070 } | |
| 2071 }; | |
| 2072 | |
| 2073 | |
| 2074 void Isolate::KillAllIsolates() { | |
| 2075 IsolateKillerVisitor visitor; | |
| 2076 VisitIsolates(&visitor); | |
| 2077 } | |
| 2078 | |
| 2079 | |
| 2080 static RawInstance* DeserializeObject(Isolate* isolate, | 1973 static RawInstance* DeserializeObject(Isolate* isolate, |
| 2081 Zone* zone, | 1974 Zone* zone, |
| 2082 uint8_t* obj_data, | 1975 uint8_t* obj_data, |
| 2083 intptr_t obj_len) { | 1976 intptr_t obj_len) { |
| 2084 if (obj_data == NULL) { | 1977 if (obj_data == NULL) { |
| 2085 return Instance::null(); | 1978 return Instance::null(); |
| 2086 } | 1979 } |
| 2087 MessageSnapshotReader reader(obj_data, | 1980 MessageSnapshotReader reader(obj_data, |
| 2088 obj_len, | 1981 obj_len, |
| 2089 isolate, | 1982 isolate, |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2276 serialized_message_, serialized_message_len_); | 2169 serialized_message_, serialized_message_len_); |
| 2277 } | 2170 } |
| 2278 | 2171 |
| 2279 | 2172 |
| 2280 void IsolateSpawnState::Cleanup() { | 2173 void IsolateSpawnState::Cleanup() { |
| 2281 SwitchIsolateScope switch_scope(I); | 2174 SwitchIsolateScope switch_scope(I); |
| 2282 Dart::ShutdownIsolate(); | 2175 Dart::ShutdownIsolate(); |
| 2283 } | 2176 } |
| 2284 | 2177 |
| 2285 } // namespace dart | 2178 } // namespace dart |
| OLD | NEW |