Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends | List of all members
dart::MessageHandler Class Referenceabstract

#include <message_handler.h>

Inheritance diagram for dart::MessageHandler:
dart::FakeMessageHandler dart::IsolateMessageHandler dart::NativeMessageHandler dart::PortTestMessageHandler dart::SendAndExitMessagesHandler dart::ServiceTestMessageHandler dart::TestMessageHandler

Classes

class  AcquiredQueues
 

Public Types

enum  MessageStatus { kOK , kError , kShutdown }
 
typedef uword CallbackData
 
typedef MessageStatus(* StartCallback) (CallbackData data)
 
typedef void(* EndCallback) (CallbackData data)
 

Public Member Functions

virtual ~MessageHandler ()
 
virtual const char * name () const
 
bool Run (ThreadPool *pool, StartCallback start_callback, EndCallback end_callback, CallbackData data)
 
MessageStatus HandleNextMessage ()
 
MessageStatus HandleOOBMessages ()
 
MessageStatus PauseAndHandleAllMessages (int64_t timeout_millis)
 
bool HasOOBMessages ()
 
bool HasMessages ()
 
virtual bool KeepAliveLocked ()
 
void RequestDeletion ()
 
bool paused () const
 
void increment_paused ()
 
void decrement_paused ()
 
void DebugDump ()
 
bool should_pause_on_start () const
 
void set_should_pause_on_start (bool should_pause_on_start)
 
bool is_paused_on_start () const
 
bool should_pause_on_exit () const
 
void set_should_pause_on_exit (bool should_pause_on_exit)
 
bool is_paused_on_exit () const
 
int64_t paused_timestamp () const
 
bool ShouldPauseOnStart (MessageStatus status) const
 
bool ShouldPauseOnExit (MessageStatus status) const
 
void PausedOnStart (bool paused)
 
void PausedOnExit (bool paused)
 

Static Public Member Functions

static const char * MessageStatusString (MessageStatus status)
 

Protected Member Functions

 MessageHandler ()
 
virtual bool IsCurrentIsolate () const
 
virtual Isolateisolate () const
 
void PostMessage (std::unique_ptr< Message > message, bool before_events=false)
 
void ClosePort (Dart_Port port)
 
void CloseAllPorts ()
 
virtual void MessageNotify (Message::Priority priority)
 
virtual MessageStatus HandleMessage (std::unique_ptr< Message > message)=0
 
virtual void NotifyPauseOnStart ()
 
virtual void NotifyPauseOnExit ()
 
Threadthread () const
 

Friends

class PortMap
 
class MessageHandlerTestPeer
 
class MessageHandlerTask
 
template<typename GCVisitorType >
void MournFinalizerEntry (GCVisitorType *, FinalizerEntryPtr)
 

Detailed Description

Definition at line 20 of file message_handler.h.

Member Typedef Documentation

◆ CallbackData

Definition at line 37 of file message_handler.h.

◆ EndCallback

typedef void(* dart::MessageHandler::EndCallback) (CallbackData data)

Definition at line 39 of file message_handler.h.

◆ StartCallback

typedef MessageStatus(* dart::MessageHandler::StartCallback) (CallbackData data)

Definition at line 38 of file message_handler.h.

Member Enumeration Documentation

◆ MessageStatus

Enumerator
kOK 
kError 
kShutdown 

Definition at line 25 of file message_handler.h.

25 {
26 kOK, // We successfully handled a message.
27 kError, // We encountered an error handling a message.
28 kShutdown, // The VM is shutting down.
29 };

Constructor & Destructor Documentation

◆ MessageHandler()

dart::MessageHandler::MessageHandler ( )
protected

Definition at line 55 of file message_handler.cc.

56 : queue_(new MessageQueue()),
57 oob_queue_(new MessageQueue()),
58 oob_message_handling_allowed_(true),
59 paused_for_messages_(false),
60 paused_(0),
61#if !defined(PRODUCT)
62 should_pause_on_start_(false),
63 should_pause_on_exit_(false),
64 is_paused_on_start_(false),
65 is_paused_on_exit_(false),
66 remembered_paused_on_exit_status_(kOK),
67 paused_timestamp_(-1),
68#endif
69 task_running_(false),
70 delete_me_(false),
71 pool_(nullptr),
72 start_callback_(nullptr),
73 end_callback_(nullptr),
74 callback_data_(0) {
75 ASSERT(queue_ != nullptr);
76 ASSERT(oob_queue_ != nullptr);
77}
#define ASSERT(E)

◆ ~MessageHandler()

dart::MessageHandler::~MessageHandler ( )
virtual

Definition at line 79 of file message_handler.cc.

79 {
80 delete queue_;
81 delete oob_queue_;
82 queue_ = nullptr;
83 oob_queue_ = nullptr;
84 pool_ = nullptr;
85}

Member Function Documentation

◆ CloseAllPorts()

void dart::MessageHandler::CloseAllPorts ( )
protected

Definition at line 543 of file message_handler.cc.

543 {
544 MonitorLocker ml(&monitor_);
545 if (FLAG_trace_isolates) {
547 "[-] Closing all ports:\n"
548 "\thandler: %s\n",
549 name());
550 }
551 queue_->Clear();
552 oob_queue_->Clear();
553}
virtual const char * name() const
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1

◆ ClosePort()

void dart::MessageHandler::ClosePort ( Dart_Port  port)
protected

Definition at line 532 of file message_handler.cc.

532 {
533 if (FLAG_trace_isolates) {
534 MonitorLocker ml(&monitor_);
536 "[-] Closing port:\n"
537 "\thandler: %s\n"
538 "\tport: %" Pd64 "\n",
539 name(), port);
540 }
541}
#define Pd64
Definition globals.h:416

◆ DebugDump()

void dart::MessageHandler::DebugDump ( )

Definition at line 570 of file message_handler.cc.

570 {
572}
static void DebugDumpForMessageHandler(MessageHandler *handler)
Definition port.cc:305

◆ decrement_paused()

void dart::MessageHandler::decrement_paused ( )
inline

Definition at line 101 of file message_handler.h.

101 {
102 ASSERT(paused_ > 0);
103 paused_--;
104 }

◆ HandleMessage()

virtual MessageStatus dart::MessageHandler::HandleMessage ( std::unique_ptr< Message message)
protectedpure virtual

◆ HandleNextMessage()

MessageHandler::MessageStatus dart::MessageHandler::HandleNextMessage ( )

Definition at line 290 of file message_handler.cc.

290 {
291 // We can only call HandleNextMessage when this handler is not
292 // assigned to a thread pool.
293 MonitorLocker ml(&monitor_);
294 ASSERT(pool_ == nullptr);
295 ASSERT(!delete_me_);
296#if defined(DEBUG)
297 CheckAccess();
298#endif
299 return HandleMessages(&ml, true, false);
300}

◆ HandleOOBMessages()

MessageHandler::MessageStatus dart::MessageHandler::HandleOOBMessages ( )

Definition at line 338 of file message_handler.cc.

338 {
339 if (!oob_message_handling_allowed_) {
340 return kOK;
341 }
342 MonitorLocker ml(&monitor_);
343 ASSERT(!delete_me_);
344#if defined(DEBUG)
345 CheckAccess();
346#endif
347 return HandleMessages(&ml, false, false);
348}

◆ HasMessages()

bool dart::MessageHandler::HasMessages ( )

Definition at line 385 of file message_handler.cc.

385 {
386 MonitorLocker ml(&monitor_);
387 return !queue_->IsEmpty();
388}

◆ HasOOBMessages()

bool dart::MessageHandler::HasOOBMessages ( )

Definition at line 372 of file message_handler.cc.

372 {
373 MonitorLocker ml(&monitor_);
374 return !oob_queue_->IsEmpty();
375}

◆ increment_paused()

void dart::MessageHandler::increment_paused ( )
inline

Definition at line 100 of file message_handler.h.

100{ paused_++; }

◆ is_paused_on_exit()

bool dart::MessageHandler::is_paused_on_exit ( ) const
inline

Definition at line 123 of file message_handler.h.

123{ return is_paused_on_exit_; }

◆ is_paused_on_start()

bool dart::MessageHandler::is_paused_on_start ( ) const
inline

Definition at line 115 of file message_handler.h.

115{ return is_paused_on_start_; }

◆ IsCurrentIsolate()

virtual bool dart::MessageHandler::IsCurrentIsolate ( ) const
inlineprotectedvirtual

Reimplemented in dart::IsolateMessageHandler.

Definition at line 178 of file message_handler.h.

178{ return false; }

◆ isolate()

virtual Isolate * dart::MessageHandler::isolate ( ) const
inlineprotectedvirtual

Reimplemented in dart::SendAndExitMessagesHandler, dart::IsolateMessageHandler, and dart::ServiceTestMessageHandler.

Definition at line 181 of file message_handler.h.

181{ return nullptr; }

◆ KeepAliveLocked()

virtual bool dart::MessageHandler::KeepAliveLocked ( )
inlinevirtual

Reimplemented in dart::IsolateMessageHandler.

Definition at line 88 of file message_handler.h.

88 {
89 // By default we keep alive until the message handler was asked to shutdown
90 // via [RequestDeletion].
91 return !delete_me_;
92 }

◆ MessageNotify()

void dart::MessageHandler::MessageNotify ( Message::Priority  priority)
protectedvirtual

Reimplemented in dart::IsolateMessageHandler, dart::TestMessageHandler, and dart::PortTestMessageHandler.

Definition at line 97 of file message_handler.cc.

97 {
98 // By default, there is no custom message notification.
99}

◆ MessageStatusString()

const char * dart::MessageHandler::MessageStatusString ( MessageStatus  status)
static

Definition at line 41 of file message_handler.cc.

41 {
42 switch (status) {
43 case kOK:
44 return "OK";
45 case kError:
46 return "Error";
47 case kShutdown:
48 return "Shutdown";
49 default:
51 return "Illegal";
52 }
53}
#define UNREACHABLE()
Definition assert.h:248

◆ name()

const char * dart::MessageHandler::name ( ) const
virtual

Reimplemented in dart::SendAndExitMessagesHandler, dart::IsolateMessageHandler, and dart::NativeMessageHandler.

Definition at line 87 of file message_handler.cc.

87 {
88 return "<unnamed>";
89}

◆ NotifyPauseOnExit()

virtual void dart::MessageHandler::NotifyPauseOnExit ( )
inlineprotectedvirtual

Reimplemented in dart::IsolateMessageHandler.

Definition at line 206 of file message_handler.h.

206{}

◆ NotifyPauseOnStart()

virtual void dart::MessageHandler::NotifyPauseOnStart ( )
inlineprotectedvirtual

Reimplemented in dart::IsolateMessageHandler.

Definition at line 205 of file message_handler.h.

205{}

◆ PauseAndHandleAllMessages()

MessageHandler::MessageStatus dart::MessageHandler::PauseAndHandleAllMessages ( int64_t  timeout_millis)

Definition at line 302 of file message_handler.cc.

303 {
304 MonitorLocker ml(&monitor_, /*no_safepoint_scope=*/false);
305 ASSERT(task_running_);
306 ASSERT(!delete_me_);
307#if defined(DEBUG)
308 CheckAccess();
309#endif
310 paused_for_messages_ = true;
311 while (queue_->IsEmpty() && oob_queue_->IsEmpty()) {
313 {
314 // Ensure this thread is at a safepoint while we wait for new messages to
315 // arrive.
316 TransitionVMToNative transition(Thread::Current());
317 wr = ml.Wait(timeout_millis);
318 }
319 ASSERT(task_running_);
320 ASSERT(!delete_me_);
321 if (wr == Monitor::kTimedOut) {
322 break;
323 }
324 if (queue_->IsEmpty()) {
325 // There are only OOB messages. Handle them and then continue waiting for
326 // normal messages unless there is an error.
327 MessageStatus status = HandleMessages(&ml, false, false);
328 if (status != kOK) {
329 paused_for_messages_ = false;
330 return status;
331 }
332 }
333 }
334 paused_for_messages_ = false;
335 return HandleMessages(&ml, true, true);
336}
static Thread * Current()
Definition thread.h:361

◆ paused()

bool dart::MessageHandler::paused ( ) const
inline

Definition at line 98 of file message_handler.h.

98{ return paused_ > 0; }

◆ paused_timestamp()

int64_t dart::MessageHandler::paused_timestamp ( ) const
inline

Definition at line 126 of file message_handler.h.

126{ return paused_timestamp_; }

◆ PausedOnExit()

void dart::MessageHandler::PausedOnExit ( bool  paused)

Definition at line 605 of file message_handler.cc.

605 {
606 MonitorLocker ml(&monitor_);
607 PausedOnExitLocked(&ml, paused);
608}

◆ PausedOnStart()

void dart::MessageHandler::PausedOnStart ( bool  paused)

Definition at line 574 of file message_handler.cc.

574 {
575 MonitorLocker ml(&monitor_);
576 PausedOnStartLocked(&ml, paused);
577}

◆ PostMessage()

void dart::MessageHandler::PostMessage ( std::unique_ptr< Message message,
bool  before_events = false 
)
protected

Definition at line 130 of file message_handler.cc.

131 {
132 Message::Priority saved_priority;
133
134 {
135 MonitorLocker ml(&monitor_);
136 if (FLAG_trace_isolates) {
137 Isolate* source_isolate = Isolate::Current();
138 if (source_isolate != nullptr) {
140 "[>] Posting message:\n"
141 "\tlen: %" Pd "\n\tsource: (%" Pd64
142 ") %s\n\tdest: %s\n"
143 "\tdest_port: %" Pd64 "\n",
144 message->Size(), static_cast<int64_t>(source_isolate->main_port()),
145 source_isolate->name(), name(), message->dest_port());
146 } else {
148 "[>] Posting message:\n"
149 "\tlen: %" Pd
150 "\n\tsource: <native code>\n"
151 "\tdest: %s\n"
152 "\tdest_port: %" Pd64 "\n",
153 message->Size(), name(), message->dest_port());
154 }
155 }
156
157 saved_priority = message->priority();
158 if (message->IsOOB()) {
159 oob_queue_->Enqueue(std::move(message), before_events);
160 } else {
161 queue_->Enqueue(std::move(message), before_events);
162 }
163 if (paused_for_messages_) {
164 ml.Notify();
165 }
166
167 if (pool_ != nullptr && !task_running_) {
168 ASSERT(!delete_me_);
169 task_running_ = true;
170 const bool launched_successfully = pool_->Run<MessageHandlerTask>(this);
171 ASSERT(launched_successfully);
172 }
173 }
174
175 // Invoke any custom message notification.
176 MessageNotify(saved_priority);
177}
static Isolate * Current()
Definition isolate.h:939
friend class MessageHandlerTask
virtual void MessageNotify(Message::Priority priority)
void Enqueue(std::unique_ptr< Message > msg, bool before_events)
Definition message.cc:98
bool Run(Args &&... args)
Definition thread_pool.h:45
Win32Message message
#define Pd
Definition globals.h:408

◆ RequestDeletion()

void dart::MessageHandler::RequestDeletion ( )

Definition at line 555 of file message_handler.cc.

555 {
556 {
557 MonitorLocker ml(&monitor_);
558 if (task_running_) {
559 // This message handler currently has a task running on the thread pool.
560 delete_me_ = true;
561 return;
562 }
563 }
564
565 // This message handler has no current task. Delete it.
566 delete this;
567}

◆ Run()

bool dart::MessageHandler::Run ( ThreadPool pool,
StartCallback  start_callback,
EndCallback  end_callback,
CallbackData  data 
)

Definition at line 101 of file message_handler.cc.

104 {
105 MonitorLocker ml(&monitor_);
106 if (FLAG_trace_isolates) {
108 "[+] Starting message handler:\n"
109 "\thandler: %s\n",
110 name());
111 }
112 ASSERT(pool_ == nullptr);
113 ASSERT(!delete_me_);
114 pool_ = pool;
115 start_callback_ = start_callback;
116 end_callback_ = end_callback;
117 callback_data_ = data;
118 task_running_ = true;
119 bool result = pool_->Run<MessageHandlerTask>(this);
120 if (!result) {
121 pool_ = nullptr;
122 start_callback_ = nullptr;
123 end_callback_ = nullptr;
124 callback_data_ = 0;
125 task_running_ = false;
126 }
127 return result;
128}
AutoreleasePool pool
GAsyncResult * result
static int8_t data[kExtLength]

◆ set_should_pause_on_exit()

void dart::MessageHandler::set_should_pause_on_exit ( bool  should_pause_on_exit)
inline

Definition at line 119 of file message_handler.h.

119 {
120 should_pause_on_exit_ = should_pause_on_exit;
121 }
bool should_pause_on_exit() const

◆ set_should_pause_on_start()

void dart::MessageHandler::set_should_pause_on_start ( bool  should_pause_on_start)
inline

Definition at line 111 of file message_handler.h.

111 {
112 should_pause_on_start_ = should_pause_on_start;
113 }
bool should_pause_on_start() const

◆ should_pause_on_exit()

bool dart::MessageHandler::should_pause_on_exit ( ) const
inline

Definition at line 117 of file message_handler.h.

117{ return should_pause_on_exit_; }

◆ should_pause_on_start()

bool dart::MessageHandler::should_pause_on_start ( ) const
inline

Definition at line 109 of file message_handler.h.

109{ return should_pause_on_start_; }

◆ ShouldPauseOnExit()

bool dart::MessageHandler::ShouldPauseOnExit ( MessageStatus  status) const

Definition at line 362 of file message_handler.cc.

362 {
363 Isolate* owning_isolate = isolate();
364 if (owning_isolate == nullptr) {
365 return false;
366 }
367 return (status != MessageHandler::kShutdown) && should_pause_on_exit() &&
368 owning_isolate->is_runnable();
369}
virtual Isolate * isolate() const

◆ ShouldPauseOnStart()

bool dart::MessageHandler::ShouldPauseOnStart ( MessageStatus  status) const

Definition at line 351 of file message_handler.cc.

351 {
352 Isolate* owning_isolate = isolate();
353 if (owning_isolate == nullptr) {
354 return false;
355 }
356 // If we are restarting or shutting down, we do not want to honor
357 // should_pause_on_start or should_pause_on_exit.
358 return (status != MessageHandler::kShutdown) && should_pause_on_start() &&
359 owning_isolate->is_runnable();
360}

◆ thread()

Thread * dart::MessageHandler::thread ( ) const
inlineprotected

Definition at line 209 of file message_handler.h.

209{ return Thread::Current(); }

Friends And Related Symbol Documentation

◆ MessageHandlerTask

friend class MessageHandlerTask
friend

Definition at line 216 of file message_handler.h.

◆ MessageHandlerTestPeer

friend class MessageHandlerTestPeer
friend

Definition at line 215 of file message_handler.h.

◆ MournFinalizerEntry

template<typename GCVisitorType >
void MournFinalizerEntry ( GCVisitorType *  visitor,
FinalizerEntryPtr  current_entry 
)
friend

Definition at line 162 of file gc_shared.h.

163 {
164 TRACE_FINALIZER("Processing Entry %p", current_entry->untag());
165
166 const Heap::Space before_gc_space = SpaceForExternal(current_entry);
167 const bool value_collected_this_gc =
168 GCVisitorType::ForwardOrSetNullIfCollected(
169 current_entry, &current_entry->untag()->value_);
170 if (!value_collected_this_gc && before_gc_space == Heap::kNew) {
171 const Heap::Space after_gc_space = SpaceForExternal(current_entry);
172 if (after_gc_space == Heap::kOld) {
173 const intptr_t external_size = current_entry->untag()->external_size_;
174 TRACE_FINALIZER("Promoting external size %" Pd
175 " bytes from new to old space",
176 external_size);
177 visitor->isolate_group()->heap()->PromotedExternal(external_size);
178 }
179 }
180 GCVisitorType::ForwardOrSetNullIfCollected(current_entry,
181 &current_entry->untag()->detach_);
182 GCVisitorType::ForwardOrSetNullIfCollected(
183 current_entry, &current_entry->untag()->finalizer_);
184
185 ObjectPtr token_object = current_entry->untag()->token();
186 // See sdk/lib/_internal/vm/lib/internal_patch.dart FinalizerBase.detach.
187 const bool is_detached = token_object == current_entry;
188
189 if (!value_collected_this_gc) return;
190 if (is_detached) return;
191
192 FinalizerBasePtr finalizer = current_entry->untag()->finalizer();
193
194 if (finalizer.IsRawNull()) {
195 TRACE_FINALIZER("Value collected entry %p finalizer null",
196 current_entry->untag());
197
198 // Do nothing, the finalizer has been GCed.
199 return;
200 }
201
202 TRACE_FINALIZER("Value collected entry %p finalizer %p",
203 current_entry->untag(), finalizer->untag());
204
205 FinalizerPtr finalizer_dart = static_cast<FinalizerPtr>(finalizer);
206 // Move entry to entries collected and current head of that list as
207 // the next element. Using a atomic exchange satisfies concurrency
208 // between the parallel GC tasks.
209 // We rely on the fact that the mutator thread is not running to avoid
210 // races between GC and mutator modifying Finalizer.entries_collected.
211 //
212 // We only run in serial marker or in the finalize step in the marker,
213 // both are in safepoint.
214 // The main scavenger worker is at safepoint, the other scavenger
215 // workers are not, but they bypass safepoint because the main
216 // worker is at a safepoint already.
217 ASSERT(Thread::Current()->OwnsGCSafepoint() ||
218 Thread::Current()->BypassSafepoints());
219
220 if (finalizer.IsNativeFinalizer()) {
221 NativeFinalizerPtr native_finalizer =
222 static_cast<NativeFinalizerPtr>(finalizer);
223
224 // Immediately call native callback.
225 RunNativeFinalizerCallback(native_finalizer, current_entry, before_gc_space,
226 visitor);
227
228 // Fall-through sending a message to clear the entries and remove
229 // from detachments.
230 }
231
232 FinalizerEntryPtr previous_head =
233 finalizer_dart->untag()->exchange_entries_collected(current_entry);
234 current_entry->untag()->set_next(previous_head);
235 const bool first_entry = previous_head.IsRawNull();
236
237 // If we're in the marker, we need to ensure that we release the store
238 // buffer afterwards.
239 // If we're in the scavenger and have the finalizer in old space and
240 // a new space entry, we don't need to release the store buffer.
241 if (!first_entry && previous_head->IsNewObject() &&
242 current_entry->IsOldObject()) {
243 TRACE_FINALIZER("Entry %p (old) next is %p (new)", current_entry->untag(),
244 previous_head->untag());
245 // We must release the thread's store buffer block.
246 }
247
248 // Schedule calling Dart finalizer.
249 if (first_entry) {
250 Isolate* isolate = finalizer->untag()->isolate_;
251 if (isolate == nullptr) {
252 TRACE_FINALIZER("Not scheduling finalizer %p callback on isolate null",
253 finalizer->untag());
254 } else {
255 TRACE_FINALIZER("Scheduling finalizer %p callback on isolate %p",
256 finalizer->untag(), isolate);
257
258 PersistentHandle* handle =
260 handle->set_ptr(finalizer);
261 MessageHandler* message_handler = isolate->message_handler();
262 message_handler->PostMessage(
264 /*before_events*/ false);
265 }
266 }
267}
PersistentHandle * AllocatePersistentHandle()
@ kNew
Definition heap.h:38
@ kOld
Definition heap.h:39
ApiState * api_state() const
Definition isolate.h:693
MessageHandler * message_handler() const
Definition isolate.cc:2380
IsolateGroup * group() const
Definition isolate.h:990
void PostMessage(std::unique_ptr< Message > message, bool before_events=false)
static std::unique_ptr< Message > New(Args &&... args)
Definition message.h:72
@ kNormalPriority
Definition message.h:28
void set_ptr(ObjectPtr ref)
#define TRACE_FINALIZER(format,...)
Definition gc_shared.h:96
Heap::Space SpaceForExternal(FinalizerEntryPtr raw_entry)
Definition gc_shared.cc:42
void RunNativeFinalizerCallback(NativeFinalizerPtr raw_finalizer, FinalizerEntryPtr raw_entry, Heap::Space before_gc_space, GCVisitorType *visitor)
Definition gc_shared.h:107

◆ PortMap

friend class PortMap
friend

Definition at line 214 of file message_handler.h.


The documentation for this class was generated from the following files: