OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "vm/bigint_store.h" | 9 #include "vm/bigint_store.h" |
10 #include "vm/code_index_table.h" | 10 #include "vm/code_index_table.h" |
11 #include "vm/compiler_stats.h" | 11 #include "vm/compiler_stats.h" |
12 #include "vm/dart_api_state.h" | 12 #include "vm/dart_api_state.h" |
13 #include "vm/dart_entry.h" | 13 #include "vm/dart_entry.h" |
14 #include "vm/debugger.h" | 14 #include "vm/debugger.h" |
15 #include "vm/debuginfo.h" | 15 #include "vm/debuginfo.h" |
16 #include "vm/heap.h" | 16 #include "vm/heap.h" |
17 #include "vm/message_queue.h" | 17 #include "vm/message.h" |
18 #include "vm/object_store.h" | 18 #include "vm/object_store.h" |
19 #include "vm/parser.h" | 19 #include "vm/parser.h" |
20 #include "vm/port.h" | 20 #include "vm/port.h" |
21 #include "vm/random.h" | 21 #include "vm/random.h" |
22 #include "vm/stack_frame.h" | 22 #include "vm/stack_frame.h" |
23 #include "vm/stub_code.h" | 23 #include "vm/stub_code.h" |
24 #include "vm/thread.h" | 24 #include "vm/thread.h" |
25 #include "vm/timer.h" | 25 #include "vm/timer.h" |
26 #include "vm/visitor.h" | 26 #include "vm/visitor.h" |
27 | 27 |
28 namespace dart { | 28 namespace dart { |
29 | 29 |
30 DEFINE_FLAG(bool, report_invocation_count, false, | 30 DEFINE_FLAG(bool, report_invocation_count, false, |
31 "Count function invocations and report."); | 31 "Count function invocations and report."); |
32 DEFINE_FLAG(bool, trace_isolates, false, | 32 DEFINE_FLAG(bool, trace_isolates, false, |
33 "Trace isolate creation and shut down."); | 33 "Trace isolate creation and shut down."); |
34 DECLARE_FLAG(bool, generate_gdb_symbols); | 34 DECLARE_FLAG(bool, generate_gdb_symbols); |
35 | 35 |
36 | 36 |
37 class IsolateMessageHandler : public MessageHandler { | |
38 public: | |
39 explicit IsolateMessageHandler(Isolate* isolate); | |
40 ~IsolateMessageHandler(); | |
41 | |
42 const char* name() const; | |
43 void MessageNotify(Message::Priority priority); | |
44 | |
45 #if defined(DEBUG) | |
46 // Check that it is safe to access this handler. | |
47 void CheckAccess(); | |
48 #endif | |
49 private: | |
50 Isolate* isolate_; | |
51 }; | |
52 | |
53 | |
54 IsolateMessageHandler::IsolateMessageHandler(Isolate* isolate) | |
55 : isolate_(isolate) { | |
56 } | |
57 | |
58 | |
59 IsolateMessageHandler::~IsolateMessageHandler() { | |
60 } | |
61 | |
62 const char* IsolateMessageHandler::name() const { | |
63 return isolate_->name(); | |
64 } | |
65 | |
66 | |
67 void IsolateMessageHandler::MessageNotify(Message::Priority priority) { | |
68 // TODO(turnidge): Remove this assert once we start using OOB messages. | |
69 ASSERT(priority < Message::kOOBPriority); | |
siva
2012/02/01 00:38:56
Instead of this assert which seems confusing to pe
turnidge
2012/02/01 18:51:27
Done.
| |
70 if (priority >= Message::kOOBPriority) { | |
71 // Handle out of band messages even if the isolate is busy. | |
72 isolate_->ScheduleInterrupts(Isolate::kMessageInterrupt); | |
73 } | |
74 Dart_MessageNotifyCallback callback = isolate_->message_notify_callback(); | |
75 if (callback) { | |
76 // Allow the embedder to handle message notification. | |
77 (*callback)(Api::CastIsolate(isolate_)); | |
78 } | |
79 } | |
80 | |
81 | |
82 #if defined(DEBUG) | |
83 void IsolateMessageHandler::CheckAccess() { | |
84 ASSERT(isolate_ == Isolate::Current()); | |
85 } | |
86 #endif | |
87 | |
88 | |
37 Isolate::Isolate() | 89 Isolate::Isolate() |
38 : store_buffer_(), | 90 : store_buffer_(), |
39 message_queue_(NULL), | |
40 message_notify_callback_(NULL), | 91 message_notify_callback_(NULL), |
41 name_(NULL), | 92 name_(NULL), |
42 num_ports_(0), | |
43 live_ports_(0), | |
44 main_port_(0), | 93 main_port_(0), |
45 heap_(NULL), | 94 heap_(NULL), |
46 object_store_(NULL), | 95 object_store_(NULL), |
47 top_resource_(NULL), | 96 top_resource_(NULL), |
48 top_context_(Context::null()), | 97 top_context_(Context::null()), |
49 current_zone_(NULL), | 98 current_zone_(NULL), |
50 #if defined(DEBUG) | 99 #if defined(DEBUG) |
51 no_gc_scope_depth_(0), | 100 no_gc_scope_depth_(0), |
52 no_handle_scope_depth_(0), | 101 no_handle_scope_depth_(0), |
53 top_handle_scope_(NULL), | 102 top_handle_scope_(NULL), |
(...skipping 11 matching lines...) Expand all Loading... | |
65 timer_list_(), | 114 timer_list_(), |
66 ast_node_id_(AstNode::kNoId), | 115 ast_node_id_(AstNode::kNoId), |
67 mutex_(new Mutex()), | 116 mutex_(new Mutex()), |
68 stack_limit_(0), | 117 stack_limit_(0), |
69 saved_stack_limit_(0) { | 118 saved_stack_limit_(0) { |
70 } | 119 } |
71 | 120 |
72 | 121 |
73 Isolate::~Isolate() { | 122 Isolate::~Isolate() { |
74 delete [] name_; | 123 delete [] name_; |
75 delete message_queue_; | |
76 delete heap_; | 124 delete heap_; |
77 delete object_store_; | 125 delete object_store_; |
78 // Do not delete stack resources: top_resource_ and current_zone_. | 126 // Do not delete stack resources: top_resource_ and current_zone_. |
79 delete bigint_store_; | 127 delete bigint_store_; |
80 delete api_state_; | 128 delete api_state_; |
81 delete stub_code_; | 129 delete stub_code_; |
82 delete code_index_table_; | 130 delete code_index_table_; |
83 delete debugger_; | 131 delete debugger_; |
84 delete mutex_; | 132 delete mutex_; |
85 mutex_ = NULL; // Fail fast if interrupts are scheduled on a dead isolate. | 133 mutex_ = NULL; // Fail fast if interrupts are scheduled on a dead isolate. |
86 } | 134 delete message_handler_; |
87 | 135 message_handler_ = NULL; // Fail fast if we send messages to a dead isolate. |
88 | |
89 void Isolate::PostMessage(Message* message) { | |
90 if (FLAG_trace_isolates) { | |
91 const char* source_name = "<native code>"; | |
92 Isolate* source_isolate = Isolate::Current(); | |
93 if (source_isolate) { | |
94 source_name = source_isolate->name(); | |
95 } | |
96 OS::Print("[>] Posting message:\n" | |
97 "\tsource: %s\n" | |
98 "\treply_port: %lld\n" | |
99 "\tdest: %s\n" | |
100 "\tdest_port: %lld\n", | |
101 source_name, message->reply_port(), name(), message->dest_port()); | |
102 } | |
103 | |
104 Message::Priority priority = message->priority(); | |
105 message_queue()->Enqueue(message); | |
106 message = NULL; // Do not access message. May have been deleted. | |
107 | |
108 ASSERT(priority < Message::kOOBPriority); | |
109 if (priority >= Message::kOOBPriority) { | |
110 // Handle out of band messages even if the isolate is busy. | |
111 ScheduleInterrupts(Isolate::kMessageInterrupt); | |
112 } | |
113 Dart_MessageNotifyCallback callback = message_notify_callback(); | |
114 if (callback) { | |
115 // Allow the embedder to handle message notification. | |
116 (*callback)(Api::CastIsolate(this)); | |
117 } | |
118 } | |
119 | |
120 | |
121 void Isolate::ClosePort(Dart_Port port) { | |
122 message_queue()->Flush(port); | |
123 } | |
124 | |
125 | |
126 void Isolate::CloseAllPorts() { | |
127 message_queue()->FlushAll(); | |
128 } | 136 } |
129 | 137 |
130 | 138 |
131 Isolate* Isolate::Init(const char* name_prefix) { | 139 Isolate* Isolate::Init(const char* name_prefix) { |
132 Isolate* result = new Isolate(); | 140 Isolate* result = new Isolate(); |
133 ASSERT(result != NULL); | 141 ASSERT(result != NULL); |
134 | 142 |
135 // TODO(5411455): For now just set the recently created isolate as | 143 // TODO(5411455): For now just set the recently created isolate as |
136 // the current isolate. | 144 // the current isolate. |
137 SetCurrent(result); | 145 SetCurrent(result); |
138 | 146 |
139 // Set up the isolate message queue. | 147 // Setup the isolate message handler. |
140 MessageQueue* queue = new MessageQueue(); | 148 MessageHandler* handler = new IsolateMessageHandler(result); |
141 ASSERT(queue != NULL); | 149 ASSERT(handler != NULL); |
142 result->set_message_queue(queue); | 150 result->set_message_handler(handler); |
143 | 151 |
144 // Setup the Dart API state. | 152 // Setup the Dart API state. |
145 ApiState* state = new ApiState(); | 153 ApiState* state = new ApiState(); |
146 ASSERT(state != NULL); | 154 ASSERT(state != NULL); |
147 result->set_api_state(state); | 155 result->set_api_state(state); |
148 | 156 |
149 // Initialize stack top and limit in case we are running the isolate in the | 157 // Initialize stack top and limit in case we are running the isolate in the |
150 // main thread. | 158 // main thread. |
151 // TODO(5411455): Need to figure out how to set the stack limit for the | 159 // TODO(5411455): Need to figure out how to set the stack limit for the |
152 // main thread. | 160 // main thread. |
153 result->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&result)); | 161 result->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&result)); |
154 result->set_main_port(PortMap::CreatePort()); | 162 result->set_main_port(PortMap::CreatePort(result->message_handler())); |
155 result->BuildName(name_prefix); | 163 result->BuildName(name_prefix); |
156 | 164 |
157 result->debugger_ = new Debugger(); | 165 result->debugger_ = new Debugger(); |
158 result->debugger_->Initialize(result); | 166 result->debugger_->Initialize(result); |
159 if (FLAG_trace_isolates) { | 167 if (FLAG_trace_isolates) { |
160 if (strcmp(name_prefix, "vm-isolate") != 0) { | 168 if (name_prefix == NULL || strcmp(name_prefix, "vm-isolate") != 0) { |
161 OS::Print("[+] Starting isolate:\n" | 169 OS::Print("[+] Starting isolate:\n" |
162 "\tisolate: %s\n", result->name()); | 170 "\tisolate: %s\n", result->name()); |
163 } | 171 } |
164 } | 172 } |
165 return result; | 173 return result; |
166 } | 174 } |
167 | 175 |
168 | 176 |
169 void Isolate::BuildName(const char* name_prefix) { | 177 void Isolate::BuildName(const char* name_prefix) { |
170 ASSERT(name_ == NULL); | 178 ASSERT(name_ == NULL); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 // Clean up debugger resources. Shutting down the debugger | 289 // Clean up debugger resources. Shutting down the debugger |
282 // requires a handle zone. We must set up a temporary zone because | 290 // requires a handle zone. We must set up a temporary zone because |
283 // Isolate::Shutdown is called without a zone. | 291 // Isolate::Shutdown is called without a zone. |
284 { | 292 { |
285 Zone zone(this); | 293 Zone zone(this); |
286 HandleScope handle_scope(this); | 294 HandleScope handle_scope(this); |
287 debugger_->Shutdown(); | 295 debugger_->Shutdown(); |
288 } | 296 } |
289 | 297 |
290 // Close all the ports owned by this isolate. | 298 // Close all the ports owned by this isolate. |
291 PortMap::ClosePorts(); | 299 PortMap::ClosePorts(message_handler()); |
292 | 300 |
293 delete message_queue(); | 301 // Fail fast if anybody tries to post any more messsages to this isolate. |
294 set_message_queue(NULL); | 302 delete message_handler(); |
303 set_message_handler(NULL); | |
295 | 304 |
296 // Dump all accumalated timer data for the isolate. | 305 // Dump all accumalated timer data for the isolate. |
297 timer_list_.ReportTimers(); | 306 timer_list_.ReportTimers(); |
298 if (FLAG_report_invocation_count) { | 307 if (FLAG_report_invocation_count) { |
299 PrintInvokedFunctions(); | 308 PrintInvokedFunctions(); |
300 } | 309 } |
301 CompilerStats::Print(); | 310 CompilerStats::Print(); |
302 if (FLAG_generate_gdb_symbols) { | 311 if (FLAG_generate_gdb_symbols) { |
303 DebugInfo::UnregisterAllSections(); | 312 DebugInfo::UnregisterAllSections(); |
304 } | 313 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 Instance& instance = Instance::Handle(); | 355 Instance& instance = Instance::Handle(); |
347 instance ^= reader.ReadObject(); | 356 instance ^= reader.ReadObject(); |
348 return instance.raw(); | 357 return instance.raw(); |
349 } | 358 } |
350 | 359 |
351 | 360 |
352 | 361 |
353 RawObject* Isolate::StandardRunLoop() { | 362 RawObject* Isolate::StandardRunLoop() { |
354 ASSERT(long_jump_base() != NULL); | 363 ASSERT(long_jump_base() != NULL); |
355 ASSERT(message_notify_callback() == NULL); | 364 ASSERT(message_notify_callback() == NULL); |
365 ASSERT(message_handler() != NULL); | |
356 | 366 |
357 while (live_ports() > 0) { | 367 while (message_handler()->HasLivePorts()) { |
358 ASSERT(this == Isolate::Current()); | 368 ASSERT(this == Isolate::Current()); |
359 Zone zone(this); | 369 Zone zone(this); |
360 HandleScope handle_scope(this); | 370 HandleScope handle_scope(this); |
361 | 371 |
362 Message* message = message_queue()->Dequeue(0); | 372 Message* message = message_handler()->queue()->Dequeue(0); |
363 if (message != NULL) { | 373 if (message != NULL) { |
364 if (message->priority() >= Message::kOOBPriority) { | 374 if (message->priority() >= Message::kOOBPriority) { |
365 // TODO(turnidge): Out of band messages will not go through the | 375 // TODO(turnidge): Out of band messages will not go through the |
366 // regular message handler. Instead they will be dispatched to | 376 // regular message handler. Instead they will be dispatched to |
367 // special vm code. Implement. | 377 // special vm code. Implement. |
368 UNIMPLEMENTED(); | 378 UNIMPLEMENTED(); |
369 } | 379 } |
370 const Instance& msg = | 380 const Instance& msg = |
371 Instance::Handle(DeserializeMessage(message->data())); | 381 Instance::Handle(DeserializeMessage(message->data())); |
372 const Object& result = Object::Handle( | 382 const Object& result = Object::Handle( |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 } | 434 } |
425 | 435 |
426 | 436 |
427 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor) { | 437 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor) { |
428 if (api_state() != NULL) { | 438 if (api_state() != NULL) { |
429 api_state()->VisitWeakHandles(visitor); | 439 api_state()->VisitWeakHandles(visitor); |
430 } | 440 } |
431 } | 441 } |
432 | 442 |
433 } // namespace dart | 443 } // namespace dart |
OLD | NEW |