| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 // Copyright (c) 2016, the Dart project authors.  Please see the AUTHORS file |  | 
| 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. |  | 
| 4 |  | 
| 5 #include "vm/kernel_isolate.h" |  | 
| 6 |  | 
| 7 #include "vm/compiler.h" |  | 
| 8 #include "vm/dart_api_impl.h" |  | 
| 9 #include "vm/dart_entry.h" |  | 
| 10 #include "vm/isolate.h" |  | 
| 11 #include "vm/lockers.h" |  | 
| 12 #include "vm/message.h" |  | 
| 13 #include "vm/message_handler.h" |  | 
| 14 #include "vm/native_entry.h" |  | 
| 15 #include "vm/native_arguments.h" |  | 
| 16 #include "vm/object.h" |  | 
| 17 #include "vm/object_store.h" |  | 
| 18 #include "vm/port.h" |  | 
| 19 #include "vm/service.h" |  | 
| 20 #include "vm/symbols.h" |  | 
| 21 #include "vm/thread_pool.h" |  | 
| 22 #include "vm/timeline.h" |  | 
| 23 |  | 
| 24 namespace dart { |  | 
| 25 |  | 
| 26 #if !defined(DART_PRECOMPILED_RUNTIME) |  | 
| 27 |  | 
| 28 #define Z (T->zone()) |  | 
| 29 |  | 
| 30 DEFINE_FLAG(bool, trace_kernel, false, "Trace Kernel service requests."); |  | 
| 31 |  | 
| 32 |  | 
| 33 const char* KernelIsolate::kName = "kernel-service"; |  | 
| 34 Dart_IsolateCreateCallback KernelIsolate::create_callback_ = NULL; |  | 
| 35 Monitor* KernelIsolate::monitor_ = NULL; |  | 
| 36 Isolate* KernelIsolate::isolate_ = NULL; |  | 
| 37 bool KernelIsolate::initializing_ = true; |  | 
| 38 Dart_Port KernelIsolate::kernel_port_ = ILLEGAL_PORT; |  | 
| 39 |  | 
| 40 |  | 
| 41 class RunKernelTask : public ThreadPool::Task { |  | 
| 42  public: |  | 
| 43   virtual void Run() { |  | 
| 44     ASSERT(Isolate::Current() == NULL); |  | 
| 45 |  | 
| 46 #ifndef PRODUCT |  | 
| 47     TimelineDurationScope tds(Timeline::GetVMStream(), "KernelIsolateStartup"); |  | 
| 48 #endif  // !PRODUCT |  | 
| 49     char* error = NULL; |  | 
| 50     Isolate* isolate = NULL; |  | 
| 51 |  | 
| 52     Dart_IsolateCreateCallback create_callback = |  | 
| 53         KernelIsolate::create_callback(); |  | 
| 54 |  | 
| 55     if (create_callback == NULL) { |  | 
| 56       KernelIsolate::FinishedInitializing(); |  | 
| 57       return; |  | 
| 58     } |  | 
| 59 |  | 
| 60     Dart_IsolateFlags api_flags; |  | 
| 61     Isolate::FlagsInitialize(&api_flags); |  | 
| 62 |  | 
| 63     isolate = reinterpret_cast<Isolate*>(create_callback( |  | 
| 64         KernelIsolate::kName, NULL, NULL, NULL, &api_flags, NULL, &error)); |  | 
| 65     if (isolate == NULL) { |  | 
| 66       if (FLAG_trace_kernel) { |  | 
| 67         OS::PrintErr("kernel-service: Isolate creation error: %s\n", error); |  | 
| 68       } |  | 
| 69       KernelIsolate::SetKernelIsolate(NULL); |  | 
| 70       KernelIsolate::FinishedInitializing(); |  | 
| 71       return; |  | 
| 72     } |  | 
| 73 |  | 
| 74     bool init_success = false; |  | 
| 75     { |  | 
| 76       ASSERT(Isolate::Current() == NULL); |  | 
| 77       StartIsolateScope start_scope(isolate); |  | 
| 78       init_success = RunMain(isolate); |  | 
| 79     } |  | 
| 80     KernelIsolate::FinishedInitializing(); |  | 
| 81 |  | 
| 82     if (!init_success) { |  | 
| 83       ShutdownIsolate(reinterpret_cast<uword>(isolate)); |  | 
| 84       return; |  | 
| 85     } |  | 
| 86 |  | 
| 87     // isolate_ was set as side effect of create callback. |  | 
| 88     ASSERT(KernelIsolate::IsKernelIsolate(isolate)); |  | 
| 89 |  | 
| 90     isolate->message_handler()->Run(Dart::thread_pool(), NULL, ShutdownIsolate, |  | 
| 91                                     reinterpret_cast<uword>(isolate)); |  | 
| 92   } |  | 
| 93 |  | 
| 94  protected: |  | 
| 95   static void ShutdownIsolate(uword parameter) { |  | 
| 96     if (FLAG_trace_kernel) { |  | 
| 97       OS::Print("kernel-service: ShutdownIsolate\n"); |  | 
| 98     } |  | 
| 99     Isolate* I = reinterpret_cast<Isolate*>(parameter); |  | 
| 100     ASSERT(KernelIsolate::IsKernelIsolate(I)); |  | 
| 101     KernelIsolate::SetKernelIsolate(NULL); |  | 
| 102     KernelIsolate::SetLoadPort(ILLEGAL_PORT); |  | 
| 103     I->WaitForOutstandingSpawns(); |  | 
| 104     { |  | 
| 105       // Print the error if there is one.  This may execute dart code to |  | 
| 106       // print the exception object, so we need to use a StartIsolateScope. |  | 
| 107       ASSERT(Isolate::Current() == NULL); |  | 
| 108       StartIsolateScope start_scope(I); |  | 
| 109       Thread* T = Thread::Current(); |  | 
| 110       ASSERT(I == T->isolate()); |  | 
| 111       StackZone zone(T); |  | 
| 112       HandleScope handle_scope(T); |  | 
| 113       Error& error = Error::Handle(Z); |  | 
| 114       error = T->sticky_error(); |  | 
| 115       if (!error.IsNull() && !error.IsUnwindError()) { |  | 
| 116         OS::PrintErr("kernel-service: Error: %s\n", error.ToErrorCString()); |  | 
| 117       } |  | 
| 118       error = I->sticky_error(); |  | 
| 119       if (!error.IsNull() && !error.IsUnwindError()) { |  | 
| 120         OS::PrintErr("kernel-service: Error: %s\n", error.ToErrorCString()); |  | 
| 121       } |  | 
| 122       Dart::RunShutdownCallback(); |  | 
| 123     } |  | 
| 124     // Shut the isolate down. |  | 
| 125     Dart::ShutdownIsolate(I); |  | 
| 126     if (FLAG_trace_kernel) { |  | 
| 127       OS::Print("kernel-service: Shutdown.\n"); |  | 
| 128     } |  | 
| 129   } |  | 
| 130 |  | 
| 131   bool RunMain(Isolate* I) { |  | 
| 132     Thread* T = Thread::Current(); |  | 
| 133     ASSERT(I == T->isolate()); |  | 
| 134     StackZone zone(T); |  | 
| 135     HANDLESCOPE(T); |  | 
| 136     // Invoke main which will return the port to which load requests are sent. |  | 
| 137     const Library& root_library = |  | 
| 138         Library::Handle(Z, I->object_store()->root_library()); |  | 
| 139     if (root_library.IsNull()) { |  | 
| 140       if (FLAG_trace_kernel) { |  | 
| 141         OS::Print("kernel-service: Embedder did not install a script."); |  | 
| 142       } |  | 
| 143       // Kernel isolate is not supported by embedder. |  | 
| 144       return false; |  | 
| 145     } |  | 
| 146     ASSERT(!root_library.IsNull()); |  | 
| 147     const String& entry_name = String::Handle(Z, String::New("main")); |  | 
| 148     ASSERT(!entry_name.IsNull()); |  | 
| 149     const Function& entry = Function::Handle( |  | 
| 150         Z, root_library.LookupFunctionAllowPrivate(entry_name)); |  | 
| 151     if (entry.IsNull()) { |  | 
| 152       // Kernel isolate is not supported by embedder. |  | 
| 153       if (FLAG_trace_kernel) { |  | 
| 154         OS::Print("kernel-service: Embedder did not provide a main function."); |  | 
| 155       } |  | 
| 156       return false; |  | 
| 157     } |  | 
| 158     ASSERT(!entry.IsNull()); |  | 
| 159     const Object& result = Object::Handle( |  | 
| 160         Z, DartEntry::InvokeFunction(entry, Object::empty_array())); |  | 
| 161     ASSERT(!result.IsNull()); |  | 
| 162     if (result.IsError()) { |  | 
| 163       // Kernel isolate did not initialize properly. |  | 
| 164       if (FLAG_trace_kernel) { |  | 
| 165         const Error& error = Error::Cast(result); |  | 
| 166         OS::Print("kernel-service: Calling main resulted in an error: %s", |  | 
| 167                   error.ToErrorCString()); |  | 
| 168       } |  | 
| 169       return false; |  | 
| 170     } |  | 
| 171     ASSERT(result.IsReceivePort()); |  | 
| 172     const ReceivePort& rp = ReceivePort::Cast(result); |  | 
| 173     KernelIsolate::SetLoadPort(rp.Id()); |  | 
| 174     return true; |  | 
| 175   } |  | 
| 176 }; |  | 
| 177 |  | 
| 178 |  | 
| 179 void KernelIsolate::Run() { |  | 
| 180   ASSERT(monitor_ == NULL); |  | 
| 181   monitor_ = new Monitor(); |  | 
| 182   ASSERT(monitor_ != NULL); |  | 
| 183   // Grab the isolate create callback here to avoid race conditions with tests |  | 
| 184   // that change this after Dart_Initialize returns. |  | 
| 185   create_callback_ = Isolate::CreateCallback(); |  | 
| 186   Dart::thread_pool()->Run(new RunKernelTask()); |  | 
| 187 } |  | 
| 188 |  | 
| 189 |  | 
| 190 void KernelIsolate::InitCallback(Isolate* I) { |  | 
| 191   Thread* T = Thread::Current(); |  | 
| 192   ASSERT(I == T->isolate()); |  | 
| 193   ASSERT(I != NULL); |  | 
| 194   ASSERT(I->name() != NULL); |  | 
| 195   if (strstr(I->name(), "kernel-service") == NULL) { |  | 
| 196     // Not service isolate. |  | 
| 197     return; |  | 
| 198   } |  | 
| 199   ASSERT(!Exists()); |  | 
| 200   if (FLAG_trace_kernel) { |  | 
| 201     OS::Print("kernel-service: InitCallback for %s.\n", I->name()); |  | 
| 202   } |  | 
| 203   SetKernelIsolate(I); |  | 
| 204 } |  | 
| 205 |  | 
| 206 |  | 
| 207 bool KernelIsolate::IsKernelIsolate(const Isolate* isolate) { |  | 
| 208   MonitorLocker ml(monitor_); |  | 
| 209   return isolate == isolate_; |  | 
| 210 } |  | 
| 211 |  | 
| 212 |  | 
| 213 bool KernelIsolate::IsRunning() { |  | 
| 214   MonitorLocker ml(monitor_); |  | 
| 215   return (kernel_port_ != ILLEGAL_PORT) && (isolate_ != NULL); |  | 
| 216 } |  | 
| 217 |  | 
| 218 |  | 
| 219 bool KernelIsolate::Exists() { |  | 
| 220   MonitorLocker ml(monitor_); |  | 
| 221   return isolate_ != NULL; |  | 
| 222 } |  | 
| 223 |  | 
| 224 |  | 
| 225 void KernelIsolate::SetKernelIsolate(Isolate* isolate) { |  | 
| 226   MonitorLocker ml(monitor_); |  | 
| 227   isolate_ = isolate; |  | 
| 228 } |  | 
| 229 |  | 
| 230 void KernelIsolate::SetLoadPort(Dart_Port port) { |  | 
| 231   MonitorLocker ml(monitor_); |  | 
| 232   kernel_port_ = port; |  | 
| 233 } |  | 
| 234 |  | 
| 235 void KernelIsolate::FinishedInitializing() { |  | 
| 236   MonitorLocker ml(monitor_); |  | 
| 237   initializing_ = false; |  | 
| 238   ml.NotifyAll(); |  | 
| 239 } |  | 
| 240 |  | 
| 241 |  | 
| 242 Dart_Port KernelIsolate::WaitForKernelPort() { |  | 
| 243   MonitorLocker ml(monitor_); |  | 
| 244   while (initializing_ && (kernel_port_ == ILLEGAL_PORT)) { |  | 
| 245     ml.Wait(); |  | 
| 246   } |  | 
| 247   return kernel_port_; |  | 
| 248 } |  | 
| 249 |  | 
| 250 #endif  // DART_PRECOMPILED_RUNTIME |  | 
| 251 |  | 
| 252 }  // namespace dart |  | 
| OLD | NEW | 
|---|