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