Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: runtime/vm/isolate.cc

Issue 9169063: Add support for native ports in the vm. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/isolate.h ('k') | runtime/vm/message.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 if (priority >= Message::kOOBPriority) {
69 // Handle out of band messages even if the isolate is busy.
70 // isolate_->ScheduleInterrupts(Isolate::kMessageInterrupt);
71 UNIMPLEMENTED();
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
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_;
87 134 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 } 135 }
129 136
130 137
131 Isolate* Isolate::Init(const char* name_prefix) { 138 Isolate* Isolate::Init(const char* name_prefix) {
132 Isolate* result = new Isolate(); 139 Isolate* result = new Isolate();
133 ASSERT(result != NULL); 140 ASSERT(result != NULL);
134 141
135 // TODO(5411455): For now just set the recently created isolate as 142 // TODO(5411455): For now just set the recently created isolate as
136 // the current isolate. 143 // the current isolate.
137 SetCurrent(result); 144 SetCurrent(result);
138 145
139 // Set up the isolate message queue. 146 // Setup the isolate message handler.
140 MessageQueue* queue = new MessageQueue(); 147 MessageHandler* handler = new IsolateMessageHandler(result);
141 ASSERT(queue != NULL); 148 ASSERT(handler != NULL);
142 result->set_message_queue(queue); 149 result->set_message_handler(handler);
143 150
144 // Setup the Dart API state. 151 // Setup the Dart API state.
145 ApiState* state = new ApiState(); 152 ApiState* state = new ApiState();
146 ASSERT(state != NULL); 153 ASSERT(state != NULL);
147 result->set_api_state(state); 154 result->set_api_state(state);
148 155
149 // Initialize stack top and limit in case we are running the isolate in the 156 // Initialize stack top and limit in case we are running the isolate in the
150 // main thread. 157 // main thread.
151 // TODO(5411455): Need to figure out how to set the stack limit for the 158 // TODO(5411455): Need to figure out how to set the stack limit for the
152 // main thread. 159 // main thread.
153 result->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&result)); 160 result->SetStackLimitFromCurrentTOS(reinterpret_cast<uword>(&result));
154 result->set_main_port(PortMap::CreatePort()); 161 result->set_main_port(PortMap::CreatePort(result->message_handler()));
155 result->BuildName(name_prefix); 162 result->BuildName(name_prefix);
156 163
157 result->debugger_ = new Debugger(); 164 result->debugger_ = new Debugger();
158 result->debugger_->Initialize(result); 165 result->debugger_->Initialize(result);
159 if (FLAG_trace_isolates) { 166 if (FLAG_trace_isolates) {
160 if (strcmp(name_prefix, "vm-isolate") != 0) { 167 if (name_prefix == NULL || strcmp(name_prefix, "vm-isolate") != 0) {
161 OS::Print("[+] Starting isolate:\n" 168 OS::Print("[+] Starting isolate:\n"
162 "\tisolate: %s\n", result->name()); 169 "\tisolate: %s\n", result->name());
163 } 170 }
164 } 171 }
165 return result; 172 return result;
166 } 173 }
167 174
168 175
169 void Isolate::BuildName(const char* name_prefix) { 176 void Isolate::BuildName(const char* name_prefix) {
170 ASSERT(name_ == NULL); 177 ASSERT(name_ == NULL);
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 // Clean up debugger resources. Shutting down the debugger 288 // Clean up debugger resources. Shutting down the debugger
282 // requires a handle zone. We must set up a temporary zone because 289 // requires a handle zone. We must set up a temporary zone because
283 // Isolate::Shutdown is called without a zone. 290 // Isolate::Shutdown is called without a zone.
284 { 291 {
285 Zone zone(this); 292 Zone zone(this);
286 HandleScope handle_scope(this); 293 HandleScope handle_scope(this);
287 debugger_->Shutdown(); 294 debugger_->Shutdown();
288 } 295 }
289 296
290 // Close all the ports owned by this isolate. 297 // Close all the ports owned by this isolate.
291 PortMap::ClosePorts(); 298 PortMap::ClosePorts(message_handler());
292 299
293 delete message_queue(); 300 // Fail fast if anybody tries to post any more messsages to this isolate.
294 set_message_queue(NULL); 301 delete message_handler();
302 set_message_handler(NULL);
295 303
296 // Dump all accumalated timer data for the isolate. 304 // Dump all accumalated timer data for the isolate.
297 timer_list_.ReportTimers(); 305 timer_list_.ReportTimers();
298 if (FLAG_report_invocation_count) { 306 if (FLAG_report_invocation_count) {
299 PrintInvokedFunctions(); 307 PrintInvokedFunctions();
300 } 308 }
301 CompilerStats::Print(); 309 CompilerStats::Print();
302 if (FLAG_generate_gdb_symbols) { 310 if (FLAG_generate_gdb_symbols) {
303 DebugInfo::UnregisterAllSections(); 311 DebugInfo::UnregisterAllSections();
304 } 312 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 Instance& instance = Instance::Handle(); 354 Instance& instance = Instance::Handle();
347 instance ^= reader.ReadObject(); 355 instance ^= reader.ReadObject();
348 return instance.raw(); 356 return instance.raw();
349 } 357 }
350 358
351 359
352 360
353 RawObject* Isolate::StandardRunLoop() { 361 RawObject* Isolate::StandardRunLoop() {
354 ASSERT(long_jump_base() != NULL); 362 ASSERT(long_jump_base() != NULL);
355 ASSERT(message_notify_callback() == NULL); 363 ASSERT(message_notify_callback() == NULL);
364 ASSERT(message_handler() != NULL);
356 365
357 while (live_ports() > 0) { 366 while (message_handler()->HasLivePorts()) {
358 ASSERT(this == Isolate::Current()); 367 ASSERT(this == Isolate::Current());
359 Zone zone(this); 368 Zone zone(this);
360 HandleScope handle_scope(this); 369 HandleScope handle_scope(this);
361 370
362 Message* message = message_queue()->Dequeue(0); 371 Message* message = message_handler()->queue()->Dequeue(0);
363 if (message != NULL) { 372 if (message != NULL) {
364 if (message->priority() >= Message::kOOBPriority) { 373 if (message->priority() >= Message::kOOBPriority) {
365 // TODO(turnidge): Out of band messages will not go through the 374 // TODO(turnidge): Out of band messages will not go through the
366 // regular message handler. Instead they will be dispatched to 375 // regular message handler. Instead they will be dispatched to
367 // special vm code. Implement. 376 // special vm code. Implement.
368 UNIMPLEMENTED(); 377 UNIMPLEMENTED();
369 } 378 }
370 const Instance& msg = 379 const Instance& msg =
371 Instance::Handle(DeserializeMessage(message->data())); 380 Instance::Handle(DeserializeMessage(message->data()));
372 const Object& result = Object::Handle( 381 const Object& result = Object::Handle(
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 } 433 }
425 434
426 435
427 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor) { 436 void Isolate::VisitWeakPersistentHandles(HandleVisitor* visitor) {
428 if (api_state() != NULL) { 437 if (api_state() != NULL) {
429 api_state()->VisitWeakHandles(visitor); 438 api_state()->VisitWeakHandles(visitor);
430 } 439 }
431 } 440 }
432 441
433 } // namespace dart 442 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/isolate.h ('k') | runtime/vm/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698