OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS 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 | 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 #ifndef VM_MESSAGE_HANDLER_H_ | 5 #ifndef VM_MESSAGE_HANDLER_H_ |
6 #define VM_MESSAGE_HANDLER_H_ | 6 #define VM_MESSAGE_HANDLER_H_ |
7 | 7 |
8 #include "vm/isolate.h" | 8 #include "vm/isolate.h" |
9 #include "vm/message.h" | 9 #include "vm/message.h" |
10 #include "vm/os_thread.h" | 10 #include "vm/os_thread.h" |
11 #include "vm/thread_pool.h" | 11 #include "vm/thread_pool.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 // A MessageHandler is an entity capable of accepting messages. | 15 // A MessageHandler is an entity capable of accepting messages. |
16 class MessageHandler { | 16 class MessageHandler { |
17 protected: | 17 protected: |
18 MessageHandler(); | 18 MessageHandler(); |
19 | 19 |
20 public: | 20 public: |
| 21 enum MessageStatus { |
| 22 kOK, // We successfully handled a message. |
| 23 kError, // We encountered an error handling a message. |
| 24 kRestart, // The VM is restarting. |
| 25 kShutdown, // The VM is shutting down. |
| 26 }; |
| 27 static const char* MessageStatusString(MessageStatus status); |
| 28 |
21 virtual ~MessageHandler(); | 29 virtual ~MessageHandler(); |
22 | 30 |
23 // Allow subclasses to provide a handler name. | 31 // Allow subclasses to provide a handler name. |
24 virtual const char* name() const; | 32 virtual const char* name() const; |
25 | 33 |
26 typedef uword CallbackData; | 34 typedef uword CallbackData; |
27 typedef bool (*StartCallback)(CallbackData data); | 35 typedef MessageStatus (*StartCallback)(CallbackData data); |
28 typedef void (*EndCallback)(CallbackData data); | 36 typedef void (*EndCallback)(CallbackData data); |
29 | 37 |
30 // Runs this message handler on the thread pool. | 38 // Runs this message handler on the thread pool. |
31 // | 39 // |
32 // Before processing messages, the optional StartFunction is run. | 40 // Before processing messages, the optional StartFunction is run. |
33 // | 41 // |
34 // A message handler will run until it terminates either normally or | 42 // A message handler will run until it terminates either normally or |
35 // abnormally. Normal termination occurs when the message handler | 43 // abnormally. Normal termination occurs when the message handler |
36 // no longer has any live ports. Abnormal termination occurs when | 44 // no longer has any live ports. Abnormal termination occurs when |
37 // HandleMessage() indicates that an error has occurred during | 45 // HandleMessage() indicates that an error has occurred during |
38 // message processing. | 46 // message processing. |
39 void Run(ThreadPool* pool, | 47 void Run(ThreadPool* pool, |
40 StartCallback start_callback, | 48 StartCallback start_callback, |
41 EndCallback end_callback, | 49 EndCallback end_callback, |
42 CallbackData data); | 50 CallbackData data); |
43 | 51 |
44 // Handles the next message for this message handler. Should only | 52 // Handles the next message for this message handler. Should only |
45 // be used when not running the handler on the thread pool (via Run | 53 // be used when not running the handler on the thread pool (via Run |
46 // or RunBlocking). | 54 // or RunBlocking). |
47 // | 55 // |
48 // Returns true on success. | 56 // Returns true on success. |
49 bool HandleNextMessage(); | 57 MessageStatus HandleNextMessage(); |
50 | 58 |
51 // Handles any OOB messages for this message handler. Can be used | 59 // Handles any OOB messages for this message handler. Can be used |
52 // even if the message handler is running on the thread pool. | 60 // even if the message handler is running on the thread pool. |
53 // | 61 // |
54 // Returns true on success. | 62 // Returns true on success. |
55 bool HandleOOBMessages(); | 63 MessageStatus HandleOOBMessages(); |
56 | 64 |
57 // Returns true if there are pending OOB messages for this message | 65 // Returns true if there are pending OOB messages for this message |
58 // handler. | 66 // handler. |
59 bool HasOOBMessages(); | 67 bool HasOOBMessages(); |
60 | 68 |
61 // A message handler tracks how many live ports it has. | 69 // A message handler tracks how many live ports it has. |
62 bool HasLivePorts() const { return live_ports_ > 0; } | 70 bool HasLivePorts() const { return live_ports_ > 0; } |
63 | 71 |
64 intptr_t live_ports() const { | 72 intptr_t live_ports() const { |
65 return live_ports_; | 73 return live_ports_; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 void increment_live_ports(); | 180 void increment_live_ports(); |
173 void decrement_live_ports(); | 181 void decrement_live_ports(); |
174 // ------------ END PortMap API ------------ | 182 // ------------ END PortMap API ------------ |
175 | 183 |
176 // Custom message notification. Optionally provided by subclass. | 184 // Custom message notification. Optionally provided by subclass. |
177 virtual void MessageNotify(Message::Priority priority); | 185 virtual void MessageNotify(Message::Priority priority); |
178 | 186 |
179 // Handles a single message. Provided by subclass. | 187 // Handles a single message. Provided by subclass. |
180 // | 188 // |
181 // Returns true on success. | 189 // Returns true on success. |
182 virtual bool HandleMessage(Message* message) = 0; | 190 virtual MessageStatus HandleMessage(Message* message) = 0; |
183 | 191 |
184 virtual void NotifyPauseOnStart() {} | 192 virtual void NotifyPauseOnStart() {} |
185 virtual void NotifyPauseOnExit() {} | 193 virtual void NotifyPauseOnExit() {} |
186 | 194 |
187 // TODO(iposva): Set a local field before entering MessageHandler methods. | 195 // TODO(iposva): Set a local field before entering MessageHandler methods. |
188 Thread* thread() const { return Thread::Current(); } | 196 Thread* thread() const { return Thread::Current(); } |
189 | 197 |
190 private: | 198 private: |
191 friend class PortMap; | 199 friend class PortMap; |
192 friend class MessageHandlerTestPeer; | 200 friend class MessageHandlerTestPeer; |
193 friend class MessageHandlerTask; | 201 friend class MessageHandlerTask; |
194 | 202 |
195 // Called by MessageHandlerTask to process our task queue. | 203 // Called by MessageHandlerTask to process our task queue. |
196 void TaskCallback(); | 204 void TaskCallback(); |
197 | 205 |
198 // Dequeue the next message. Prefer messages from the oob_queue_ to | 206 // Dequeue the next message. Prefer messages from the oob_queue_ to |
199 // messages from the queue_. | 207 // messages from the queue_. |
200 Message* DequeueMessage(Message::Priority min_priority); | 208 Message* DequeueMessage(Message::Priority min_priority); |
201 | 209 |
| 210 void ClearOOBQueue(); |
| 211 |
202 // Handles any pending messages. | 212 // Handles any pending messages. |
203 bool HandleMessages(bool allow_normal_messages, | 213 MessageStatus HandleMessages(bool allow_normal_messages, |
204 bool allow_multiple_normal_messages); | 214 bool allow_multiple_normal_messages); |
205 | 215 |
206 Monitor monitor_; // Protects all fields in MessageHandler. | 216 Monitor monitor_; // Protects all fields in MessageHandler. |
207 MessageQueue* queue_; | 217 MessageQueue* queue_; |
208 MessageQueue* oob_queue_; | 218 MessageQueue* oob_queue_; |
209 // This flag is not thread safe and can only reliably be accessed on a single | 219 // This flag is not thread safe and can only reliably be accessed on a single |
210 // thread. | 220 // thread. |
211 bool oob_message_handling_allowed_; | 221 bool oob_message_handling_allowed_; |
212 intptr_t live_ports_; // The number of open ports, including control ports. | 222 intptr_t live_ports_; // The number of open ports, including control ports. |
213 intptr_t paused_; // The number of pause messages received. | 223 intptr_t paused_; // The number of pause messages received. |
214 bool pause_on_start_; | 224 bool pause_on_start_; |
215 bool pause_on_exit_; | 225 bool pause_on_exit_; |
216 bool paused_on_start_; | 226 bool paused_on_start_; |
217 bool paused_on_exit_; | 227 bool paused_on_exit_; |
218 int64_t paused_timestamp_; | 228 int64_t paused_timestamp_; |
219 ThreadPool* pool_; | 229 ThreadPool* pool_; |
220 ThreadPool::Task* task_; | 230 ThreadPool::Task* task_; |
221 StartCallback start_callback_; | 231 StartCallback start_callback_; |
222 EndCallback end_callback_; | 232 EndCallback end_callback_; |
223 CallbackData callback_data_; | 233 CallbackData callback_data_; |
224 | 234 |
225 DISALLOW_COPY_AND_ASSIGN(MessageHandler); | 235 DISALLOW_COPY_AND_ASSIGN(MessageHandler); |
226 }; | 236 }; |
227 | 237 |
228 } // namespace dart | 238 } // namespace dart |
229 | 239 |
230 #endif // VM_MESSAGE_HANDLER_H_ | 240 #endif // VM_MESSAGE_HANDLER_H_ |
OLD | NEW |