Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
message_handler.h
Go to the documentation of this file.
1// Copyright (c) 2011, 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#ifndef RUNTIME_VM_MESSAGE_HANDLER_H_
6#define RUNTIME_VM_MESSAGE_HANDLER_H_
7
8#include <memory>
9
10#include "vm/isolate.h"
11#include "vm/lockers.h"
12#include "vm/message.h"
13#include "vm/os_thread.h"
14#include "vm/port_set.h"
15#include "vm/thread_pool.h"
16
17namespace dart {
18
19// A MessageHandler is an entity capable of accepting messages.
21 protected:
23
24 public:
26 kOK, // We successfully handled a message.
27 kError, // We encountered an error handling a message.
28 kShutdown, // The VM is shutting down.
29 };
30 static const char* MessageStatusString(MessageStatus status);
31
32 virtual ~MessageHandler();
33
34 // Allow subclasses to provide a handler name.
35 virtual const char* name() const;
36
39 typedef void (*EndCallback)(CallbackData data);
40
41 // Runs this message handler on the thread pool.
42 //
43 // Before processing messages, the optional StartFunction is run.
44 //
45 // A message handler will run until it terminates either normally or
46 // abnormally. Normal termination occurs when the message handler
47 // no longer has any live ports. Abnormal termination occurs when
48 // HandleMessage() indicates that an error has occurred during
49 // message processing.
50
51 // Returns false if the handler terminated abnormally, otherwise it
52 // returns true.
53 bool Run(ThreadPool* pool,
54 StartCallback start_callback,
55 EndCallback end_callback,
57
58 // Handles the next message for this message handler. Should only
59 // be used when not running the handler on the thread pool (via Run
60 // or RunBlocking).
61 //
62 // Returns true on success.
64
65 // Handles any OOB messages for this message handler. Can be used
66 // even if the message handler is running on the thread pool.
67 //
68 // Returns true on success.
70
71 // Blocks the thread on a condition variable until a message arrives, and then
72 // handles all messages.
73 MessageStatus PauseAndHandleAllMessages(int64_t timeout_millis);
74
75 // Returns true if there are pending OOB messages for this message
76 // handler.
77 bool HasOOBMessages();
78
79#if defined(TESTING)
80 std::unique_ptr<Message> StealOOBMessage();
81#endif
82
83 // Returns true if there are pending normal messages for this message
84 // handler.
85 bool HasMessages();
86
87 // Whether to keep this message handler alive or whether it should shutdown.
88 virtual bool KeepAliveLocked() {
89 // By default we keep alive until the message handler was asked to shutdown
90 // via [RequestDeletion].
91 return !delete_me_;
92 }
93
94 // Requests deletion of this message handler when the next task
95 // completes.
96 void RequestDeletion();
97
98 bool paused() const { return paused_ > 0; }
99
100 void increment_paused() { paused_++; }
102 ASSERT(paused_ > 0);
103 paused_--;
104 }
105
106#if !defined(PRODUCT)
107 void DebugDump();
108
109 bool should_pause_on_start() const { return should_pause_on_start_; }
110
112 should_pause_on_start_ = should_pause_on_start;
113 }
114
115 bool is_paused_on_start() const { return is_paused_on_start_; }
116
117 bool should_pause_on_exit() const { return should_pause_on_exit_; }
118
120 should_pause_on_exit_ = should_pause_on_exit;
121 }
122
123 bool is_paused_on_exit() const { return is_paused_on_exit_; }
124
125 // Timestamp of the paused on start or paused on exit.
126 int64_t paused_timestamp() const { return paused_timestamp_; }
127
128 bool ShouldPauseOnStart(MessageStatus status) const;
129 bool ShouldPauseOnExit(MessageStatus status) const;
130 void PausedOnStart(bool paused);
131 void PausedOnExit(bool paused);
132#endif
133
134 // Gives temporary ownership of |queue| and |oob_queue|. Using this object
135 // has the side effect that no OOB messages will be handled if a stack
136 // overflow interrupt is delivered.
138 public:
139 explicit AcquiredQueues(MessageHandler* handler);
140
142
144 if (handler_ == nullptr) {
145 return nullptr;
146 }
147 return handler_->queue_;
148 }
149
151 if (handler_ == nullptr) {
152 return nullptr;
153 }
154 return handler_->oob_queue_;
155 }
156
157 private:
158 MessageHandler* handler_;
160
161 friend class MessageHandler;
162 };
163
164#if defined(DEBUG)
165 // Check that it is safe to access this message handler.
166 //
167 // For example, if this MessageHandler is an isolate, then it is
168 // only safe to access it when the MessageHandler is the current
169 // isolate.
170 virtual void CheckAccess() const;
171#endif
172
173 protected:
174 // ------------ START PortMap API ------------
175 // These functions should only be called from the PortMap.
176
177 // Does this message handler correspond to the current isolate?
178 virtual bool IsCurrentIsolate() const { return false; }
179
180 // Return Isolate to which this message handler corresponds to.
181 virtual Isolate* isolate() const { return nullptr; }
182
183 // Posts a message on this handler's message queue.
184 // If before_events is true, then the message is enqueued before any pending
185 // events, but after any pending isolate library events.
186 void PostMessage(std::unique_ptr<Message> message,
187 bool before_events = false);
188
189 // Notifies this handler that a port is being closed.
190 void ClosePort(Dart_Port port);
191
192 // Notifies this handler that all ports are being closed.
193 void CloseAllPorts();
194
195 // ------------ END PortMap API ------------
196
197 // Custom message notification. Optionally provided by subclass.
198 virtual void MessageNotify(Message::Priority priority);
199
200 // Handles a single message. Provided by subclass.
201 //
202 // Returns true on success.
203 virtual MessageStatus HandleMessage(std::unique_ptr<Message> message) = 0;
204
205 virtual void NotifyPauseOnStart() {}
206 virtual void NotifyPauseOnExit() {}
207
208 // TODO(iposva): Set a local field before entering MessageHandler methods.
209 Thread* thread() const { return Thread::Current(); }
210
211 private:
212 template <typename GCVisitorType>
213 friend void MournFinalizerEntry(GCVisitorType*, FinalizerEntryPtr);
214 friend class PortMap;
216 friend class MessageHandlerTask;
217
218 struct PortSetEntry : public PortSet<PortSetEntry>::Entry {};
219
220 // Called by MessageHandlerTask to process our task queue.
221 void TaskCallback();
222
223 // Checks if we have a slot for idle task execution, if we have a slot
224 // for idle task execution it is scheduled immediately or we wait for
225 // idle expiration and then attempt to schedule the idle task.
226 // Returns true if their is scope for idle task execution so that we
227 // can loop back to handle more messages or false if idle tasks are not
228 // scheduled.
229 bool CheckIfIdleLocked(MonitorLocker* ml);
230
231 // Triggers a run of the idle task.
232 void RunIdleTaskLocked(MonitorLocker* ml);
233
234 // NOTE: These two functions release and reacquire the monitor, you may
235 // need to call HandleMessages to ensure all pending messages are handled.
236 void PausedOnStartLocked(MonitorLocker* ml, bool paused);
237 void PausedOnExitLocked(MonitorLocker* ml, bool paused);
238
239 // Dequeue the next message. Prefer messages from the oob_queue_ to
240 // messages from the queue_.
241 std::unique_ptr<Message> DequeueMessage(Message::Priority min_priority);
242
243 void ClearOOBQueue();
244
245 // Handles any pending messages.
246 MessageStatus HandleMessages(MonitorLocker* ml,
247 bool allow_normal_messages,
248 bool allow_multiple_normal_messages);
249
250 Monitor monitor_; // Protects all fields in MessageHandler.
251 MessageQueue* queue_;
252 MessageQueue* oob_queue_;
253 // This flag is not thread safe and can only reliably be accessed on a single
254 // thread.
255 bool oob_message_handling_allowed_;
256 bool paused_for_messages_;
258 ports_; // Only accessed by [PortMap], protected by [PortMap]s lock.
259 intptr_t paused_; // The number of pause messages received.
260#if !defined(PRODUCT)
261 bool should_pause_on_start_;
262 bool should_pause_on_exit_;
263 bool is_paused_on_start_;
264 bool is_paused_on_exit_;
265 // When isolate gets paused, remember the status of the message being
266 // processed so that we can resume correctly(into potentially not-OK status).
267 MessageStatus remembered_paused_on_exit_status_;
268 int64_t paused_timestamp_;
269#endif
270 bool task_running_;
271 bool delete_me_;
272 ThreadPool* pool_;
273 StartCallback start_callback_;
274 EndCallback end_callback_;
275 CallbackData callback_data_;
276
278};
279
280} // namespace dart
281
282#endif // RUNTIME_VM_MESSAGE_HANDLER_H_
AutoreleasePool pool
void PausedOnStart(bool paused)
virtual bool KeepAliveLocked()
void(* EndCallback)(CallbackData data)
void PausedOnExit(bool paused)
virtual void NotifyPauseOnExit()
virtual const char * name() const
bool ShouldPauseOnStart(MessageStatus status) const
int64_t paused_timestamp() const
Thread * thread() const
static const char * MessageStatusString(MessageStatus status)
bool Run(ThreadPool *pool, StartCallback start_callback, EndCallback end_callback, CallbackData data)
bool is_paused_on_start() const
virtual Isolate * isolate() const
virtual void NotifyPauseOnStart()
void ClosePort(Dart_Port port)
MessageStatus HandleNextMessage()
MessageStatus PauseAndHandleAllMessages(int64_t timeout_millis)
void set_should_pause_on_start(bool should_pause_on_start)
bool ShouldPauseOnExit(MessageStatus status) const
void set_should_pause_on_exit(bool should_pause_on_exit)
bool is_paused_on_exit() const
virtual bool IsCurrentIsolate() const
MessageStatus(* StartCallback)(CallbackData data)
MessageStatus HandleOOBMessages()
bool should_pause_on_start() const
virtual MessageStatus HandleMessage(std::unique_ptr< Message > message)=0
friend void MournFinalizerEntry(GCVisitorType *, FinalizerEntryPtr)
Definition gc_shared.h:162
bool should_pause_on_exit() const
virtual void MessageNotify(Message::Priority priority)
static Thread * Current()
Definition thread.h:361
int64_t Dart_Port
Definition dart_api.h:1524
#define ASSERT(E)
Win32Message message
uintptr_t uword
Definition globals.h:501
static int8_t data[kExtLength]
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581
#define PostMessage