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 ASSERT(priority < Message::kOOBPriority); | |
Søren Gjesse
2012/01/27 13:33:14
With this ASSERT the condition of the if statement
turnidge
2012/01/31 20:04:31
Yes. OOB messages are only partway done. I've ad
| |
69 if (priority >= Message::kOOBPriority) { | |
70 // Handle out of band messages even if the isolate is busy. | |
71 isolate_->ScheduleInterrupts(Isolate::kMessageInterrupt); | |
72 } | |
73 Dart_MessageNotifyCallback callback = isolate_->message_notify_callback(); | |
74 if (callback) { | |
75 // Allow the embedder to handle message notification. | |
76 (*callback)(Api::CastIsolate(isolate_)); | |
77 } | |
78 } | |
79 | |
80 | |
81 #if defined(DEBUG) | |
82 void IsolateMessageHandler::CheckAccess() { | |
83 ASSERT(isolate_ == Isolate::Current()); | |
84 } | |
85 #endif | |
86 | |
87 | |
37 Isolate::Isolate() | 88 Isolate::Isolate() |
38 : store_buffer_(), | 89 : store_buffer_(), |
39 message_queue_(NULL), | |
40 message_notify_callback_(NULL), | 90 message_notify_callback_(NULL), |
41 name_(NULL), | 91 name_(NULL), |
42 num_ports_(0), | |
43 live_ports_(0), | |
44 main_port_(0), | 92 main_port_(0), |
45 heap_(NULL), | 93 heap_(NULL), |
46 object_store_(NULL), | 94 object_store_(NULL), |
47 top_resource_(NULL), | 95 top_resource_(NULL), |
48 top_context_(Context::null()), | 96 top_context_(Context::null()), |
49 current_zone_(NULL), | 97 current_zone_(NULL), |
50 #if defined(DEBUG) | 98 #if defined(DEBUG) |
51 no_gc_scope_depth_(0), | 99 no_gc_scope_depth_(0), |
52 no_handle_scope_depth_(0), | 100 no_handle_scope_depth_(0), |
53 top_handle_scope_(NULL), | 101 top_handle_scope_(NULL), |
(...skipping 11 matching lines...) Expand all Loading... | |
65 timer_list_(), | 113 timer_list_(), |
66 ast_node_id_(AstNode::kNoId), | 114 ast_node_id_(AstNode::kNoId), |
67 mutex_(new Mutex()), | 115 mutex_(new Mutex()), |
68 stack_limit_(0), | 116 stack_limit_(0), |
69 saved_stack_limit_(0) { | 117 saved_stack_limit_(0) { |
70 } | 118 } |
71 | 119 |
72 | 120 |
73 Isolate::~Isolate() { | 121 Isolate::~Isolate() { |
74 delete [] name_; | 122 delete [] name_; |
75 delete message_queue_; | |
76 delete heap_; | 123 delete heap_; |
77 delete object_store_; | 124 delete object_store_; |
78 // Do not delete stack resources: top_resource_ and current_zone_. | 125 // Do not delete stack resources: top_resource_ and current_zone_. |
79 delete bigint_store_; | 126 delete bigint_store_; |
80 delete api_state_; | 127 delete api_state_; |
81 delete stub_code_; | 128 delete stub_code_; |
82 delete code_index_table_; | 129 delete code_index_table_; |
83 delete debugger_; | 130 delete debugger_; |
84 delete mutex_; | 131 delete mutex_; |
85 mutex_ = NULL; // Fail fast if interrupts are scheduled on a dead isolate. | 132 mutex_ = NULL; // Fail fast if interrupts are scheduled on a dead isolate. |
86 } | 133 delete message_handler_; |
siva
2012/01/28 00:21:05
similar to mutex_ = NULL set message_handler_ = NU
turnidge
2012/01/31 20:04:31
Done. Maybe we should set all of these to NULL...
| |
87 | |
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 } | 134 } |
129 | 135 |
130 | 136 |
131 Isolate* Isolate::Init(const char* name_prefix) { | 137 Isolate* Isolate::Init(const char* name_prefix) { |
132 Isolate* result = new Isolate(); | 138 Isolate* result = new Isolate(); |
133 ASSERT(result != NULL); | 139 ASSERT(result != NULL); |
134 | 140 |
135 // TODO(5411455): For now just set the recently created isolate as | 141 // TODO(5411455): For now just set the recently created isolate as |
136 // the current isolate. | 142 // the current isolate. |
137 SetCurrent(result); | 143 SetCurrent(result); |
138 | 144 |
139 // Set up the isolate message queue. | 145 // Setup the isolate message handler. |
140 MessageQueue* queue = new MessageQueue(); | 146 MessageHandler* handler = new IsolateMessageHandler(result); |
141 ASSERT(queue != NULL); | 147 ASSERT(handler != NULL); |
142 result->set_message_queue(queue); | 148 result->set_message_handler(handler); |
143 | 149 |
144 // Setup the Dart API state. | 150 // Setup the Dart API state. |
145 ApiState* state = new ApiState(); | 151 ApiState* state = new ApiState(); |
146 ASSERT(state != NULL); | 152 ASSERT(state != NULL); |
147 result->set_api_state(state); | 153 result->set_api_state(state); |
148 | 154 |
149 // Initialize stack top and limit in case we are running the isolate in the | 155 // Initialize stack top and limit in case we are running the isolate in the |
150 // main thread. | 156 // main thread. |
151 // TODO(5411455): Need to figure out how to set the stack limit for the | 157 // TODO(5411455): Need to figure out how to set the stack limit for the |
152 // main thread. | 158 // main thread. |
153 result->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&result)); | 159 result->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&result)); |
154 result->set_main_port(PortMap::CreatePort()); | 160 result->set_main_port(PortMap::CreatePort(result->message_handler())); |
155 result->BuildName(name_prefix); | 161 result->BuildName(name_prefix); |
156 | 162 |
157 result->debugger_ = new Debugger(); | 163 result->debugger_ = new Debugger(); |
158 result->debugger_->Initialize(result); | 164 result->debugger_->Initialize(result); |
159 if (FLAG_trace_isolates) { | 165 if (FLAG_trace_isolates) { |
160 if (strcmp(name_prefix, "vm-isolate") != 0) { | 166 if (name_prefix == NULL || strcmp(name_prefix, "vm-isolate") != 0) { |
161 OS::Print("[+] Starting isolate:\n" | 167 OS::Print("[+] Starting isolate:\n" |
162 "\tisolate: %s\n", result->name()); | 168 "\tisolate: %s\n", result->name()); |
163 } | 169 } |
164 } | 170 } |
165 return result; | 171 return result; |
166 } | 172 } |
167 | 173 |
168 | 174 |
169 void Isolate::BuildName(const char* name_prefix) { | 175 void Isolate::BuildName(const char* name_prefix) { |
170 ASSERT(name_ == NULL); | 176 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 | 287 // Clean up debugger resources. Shutting down the debugger |
282 // requires a handle zone. We must set up a temporary zone because | 288 // requires a handle zone. We must set up a temporary zone because |
283 // Isolate::Shutdown is called without a zone. | 289 // Isolate::Shutdown is called without a zone. |
284 { | 290 { |
285 Zone zone(this); | 291 Zone zone(this); |
286 HandleScope handle_scope(this); | 292 HandleScope handle_scope(this); |
287 debugger_->Shutdown(); | 293 debugger_->Shutdown(); |
288 } | 294 } |
289 | 295 |
290 // Close all the ports owned by this isolate. | 296 // Close all the ports owned by this isolate. |
291 PortMap::ClosePorts(); | 297 PortMap::ClosePorts(message_handler()); |
292 | 298 |
293 delete message_queue(); | 299 // Fail fast if anybody tries to post any more messsages to this isolate. |
294 set_message_queue(NULL); | 300 delete message_handler(); |
301 set_message_handler(NULL); | |
295 | 302 |
296 // Dump all accumalated timer data for the isolate. | 303 // Dump all accumalated timer data for the isolate. |
297 timer_list_.ReportTimers(); | 304 timer_list_.ReportTimers(); |
298 if (FLAG_report_invocation_count) { | 305 if (FLAG_report_invocation_count) { |
299 PrintInvokedFunctions(); | 306 PrintInvokedFunctions(); |
300 } | 307 } |
301 CompilerStats::Print(); | 308 CompilerStats::Print(); |
302 if (FLAG_generate_gdb_symbols) { | 309 if (FLAG_generate_gdb_symbols) { |
303 DebugInfo::UnregisterAllSections(); | 310 DebugInfo::UnregisterAllSections(); |
304 } | 311 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 SnapshotReader reader(snapshot, Isolate::Current()); | 352 SnapshotReader reader(snapshot, Isolate::Current()); |
346 Instance& instance = Instance::Handle(); | 353 Instance& instance = Instance::Handle(); |
347 instance ^= reader.ReadObject(); | 354 instance ^= reader.ReadObject(); |
348 return instance.raw(); | 355 return instance.raw(); |
349 } | 356 } |
350 | 357 |
351 | 358 |
352 | 359 |
353 RawObject* Isolate::StandardRunLoop() { | 360 RawObject* Isolate::StandardRunLoop() { |
354 ASSERT(long_jump_base() != NULL); | 361 ASSERT(long_jump_base() != NULL); |
355 ASSERT(message_notify_callback() == NULL); | 362 ASSERT(message_notify_callback() == NULL); |
siva
2012/01/28 00:21:05
ASSERT(message_handler() != NULL);
turnidge
2012/01/31 20:04:31
Done.
| |
356 | 363 |
357 while (live_ports() > 0) { | 364 while (message_handler()->HasLivePorts()) { |
358 ASSERT(this == Isolate::Current()); | 365 ASSERT(this == Isolate::Current()); |
359 Zone zone(this); | 366 Zone zone(this); |
360 HandleScope handle_scope(this); | 367 HandleScope handle_scope(this); |
361 | 368 |
362 Message* message = message_queue()->Dequeue(0); | 369 Message* message = message_handler()->queue()->Dequeue(0); |
363 if (message != NULL) { | 370 if (message != NULL) { |
364 if (message->priority() >= Message::kOOBPriority) { | 371 if (message->priority() >= Message::kOOBPriority) { |
365 // TODO(turnidge): Out of band messages will not go through the | 372 // TODO(turnidge): Out of band messages will not go through the |
366 // regular message handler. Instead they will be dispatched to | 373 // regular message handler. Instead they will be dispatched to |
367 // special vm code. Implement. | 374 // special vm code. Implement. |
368 UNIMPLEMENTED(); | 375 UNIMPLEMENTED(); |
369 } | 376 } |
370 const Instance& msg = | 377 const Instance& msg = |
371 Instance::Handle(DeserializeMessage(message->data())); | 378 Instance::Handle(DeserializeMessage(message->data())); |
372 const Object& result = Object::Handle( | 379 const Object& result = Object::Handle( |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 } | 431 } |
425 | 432 |
426 | 433 |
427 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor) { | 434 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor) { |
428 if (api_state() != NULL) { | 435 if (api_state() != NULL) { |
429 api_state()->VisitWeakHandles(visitor); | 436 api_state()->VisitWeakHandles(visitor); |
430 } | 437 } |
431 } | 438 } |
432 | 439 |
433 } // namespace dart | 440 } // namespace dart |
OLD | NEW |