| 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_H_ | 5 #ifndef VM_MESSAGE_HANDLER_H_ |
| 6 #define VM_MESSAGE_H_ | 6 #define VM_MESSAGE_HANDLER_H_ |
| 7 | 7 |
| 8 #include "vm/message.h" |
| 8 #include "vm/thread.h" | 9 #include "vm/thread.h" |
| 9 | 10 #include "vm/thread_pool.h" |
| 10 // Duplicated from dart_api.h to avoid including the whole header. | |
| 11 typedef int64_t Dart_Port; | |
| 12 | 11 |
| 13 namespace dart { | 12 namespace dart { |
| 14 | 13 |
| 15 class Message { | |
| 16 public: | |
| 17 typedef enum { | |
| 18 kNormalPriority = 0, // Deliver message when idle. | |
| 19 kOOBPriority = 1, // Deliver message asap. | |
| 20 | |
| 21 // Iteration. | |
| 22 kFirstPriority = 0, | |
| 23 kNumPriorities = 2, | |
| 24 } Priority; | |
| 25 | |
| 26 // A port number which is never used. | |
| 27 static const Dart_Port kIllegalPort = 0; | |
| 28 | |
| 29 // A new message to be sent between two isolates. The data handed to this | |
| 30 // message will be disposed by calling free() once the message object is | |
| 31 // being destructed (after delivery or when the receiving port is closed). | |
| 32 // | |
| 33 // If reply_port is kIllegalPort, then there is no reply port. | |
| 34 Message(Dart_Port dest_port, Dart_Port reply_port, | |
| 35 uint8_t* data, Priority priority) | |
| 36 : next_(NULL), | |
| 37 dest_port_(dest_port), | |
| 38 reply_port_(reply_port), | |
| 39 data_(data), | |
| 40 priority_(priority) {} | |
| 41 ~Message() { | |
| 42 free(data_); | |
| 43 } | |
| 44 | |
| 45 Dart_Port dest_port() const { return dest_port_; } | |
| 46 Dart_Port reply_port() const { return reply_port_; } | |
| 47 uint8_t* data() const { return data_; } | |
| 48 Priority priority() const { return priority_; } | |
| 49 | |
| 50 private: | |
| 51 friend class MessageQueue; | |
| 52 | |
| 53 Message* next_; | |
| 54 Dart_Port dest_port_; | |
| 55 Dart_Port reply_port_; | |
| 56 uint8_t* data_; | |
| 57 Priority priority_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(Message); | |
| 60 }; | |
| 61 | |
| 62 // There is a message queue per isolate. | |
| 63 class MessageQueue { | |
| 64 public: | |
| 65 MessageQueue(); | |
| 66 ~MessageQueue(); | |
| 67 | |
| 68 void Enqueue(Message* msg); | |
| 69 | |
| 70 // Gets the next message from the message queue, possibly blocking | |
| 71 // if no message is available. 'millis' is a timeout in | |
| 72 // milliseconds. If 'millis' is 0, then this means to block | |
| 73 // indefinitely. May block if no message is available. May return | |
| 74 // NULL even if 'millis' is 0 due to spurious wakeups. | |
| 75 Message* Dequeue(int64_t millis); | |
| 76 | |
| 77 // Gets the next message from the message queue if available. Will | |
| 78 // not block. | |
| 79 Message* DequeueNoWait(); | |
| 80 | |
| 81 // Gets the next message of the specified priority or greater from | |
| 82 // the message queue if available. Will not block. | |
| 83 Message* DequeueNoWaitWithPriority(Message::Priority min_priority); | |
| 84 | |
| 85 void Flush(Dart_Port port); | |
| 86 void FlushAll(); | |
| 87 | |
| 88 private: | |
| 89 friend class MessageQueueTestPeer; | |
| 90 | |
| 91 Message* DequeueNoWaitHoldsLock(Message::Priority min_priority); | |
| 92 | |
| 93 Monitor monitor_; | |
| 94 Message* head_[Message::kNumPriorities]; | |
| 95 Message* tail_[Message::kNumPriorities]; | |
| 96 | |
| 97 DISALLOW_COPY_AND_ASSIGN(MessageQueue); | |
| 98 }; | |
| 99 | |
| 100 // A MessageHandler is an entity capable of accepting messages. | 14 // A MessageHandler is an entity capable of accepting messages. |
| 101 class MessageHandler { | 15 class MessageHandler { |
| 102 protected: | 16 protected: |
| 103 MessageHandler(); | 17 MessageHandler(); |
| 104 | 18 |
| 105 // Allows subclasses to provide custom message notification. | |
| 106 virtual void MessageNotify(Message::Priority priority); | |
| 107 | |
| 108 public: | 19 public: |
| 109 virtual ~MessageHandler(); | 20 virtual ~MessageHandler(); |
| 110 | 21 |
| 111 // Allow subclasses to provide a handler name. | 22 // Allow subclasses to provide a handler name. |
| 112 virtual const char* name() const; | 23 virtual const char* name() const; |
| 113 | 24 |
| 25 typedef uword CallbackData; |
| 26 typedef bool (*StartCallback)(CallbackData data); |
| 27 typedef void (*EndCallback)(CallbackData data); |
| 28 |
| 29 // Runs this message handler on the thread pool. |
| 30 // |
| 31 // Before processing messages, the optional StartFunction is run. |
| 32 // |
| 33 // A message handler will run until it terminates either normally or |
| 34 // abnormally. Normal termination occurs when the message handler |
| 35 // no longer has any live ports. Abnormal termination occurs when |
| 36 // HandleMessage() indicates that an error has occurred during |
| 37 // message processing. |
| 38 void Run(ThreadPool* pool, |
| 39 StartCallback start_callback, |
| 40 EndCallback end_callback, |
| 41 CallbackData data); |
| 42 |
| 43 // Handles the next message for this message handler. Should only |
| 44 // be used when not running the handler on the thread pool (via Run |
| 45 // or RunBlocking). |
| 46 // |
| 47 // Returns true on success. |
| 48 bool HandleNextMessage(); |
| 49 |
| 50 // Handles any OOB messages for this message handler. Can be used |
| 51 // even if the message handler is running on the thread pool. |
| 52 // |
| 53 // Returns true on success. |
| 54 bool HandleOOBMessages(); |
| 55 |
| 56 // A message handler tracks how many live ports it has. |
| 57 bool HasLivePorts() const { return live_ports_ > 0; } |
| 58 |
| 114 #if defined(DEBUG) | 59 #if defined(DEBUG) |
| 115 // Check that it is safe to access this message handler. | 60 // Check that it is safe to access this message handler. |
| 116 // | 61 // |
| 117 // For example, if this MessageHandler is an isolate, then it is | 62 // For example, if this MessageHandler is an isolate, then it is |
| 118 // only safe to access it when the MessageHandler is the current | 63 // only safe to access it when the MessageHandler is the current |
| 119 // isolate. | 64 // isolate. |
| 120 virtual void CheckAccess(); | 65 virtual void CheckAccess(); |
| 121 #endif | 66 #endif |
| 122 | 67 |
| 68 protected: |
| 69 // ------------ START PortMap API ------------ |
| 70 // These functions should only be called from the PortMap. |
| 71 |
| 72 // Posts a message on this handler's message queue. |
| 123 void PostMessage(Message* message); | 73 void PostMessage(Message* message); |
| 74 |
| 75 // Notifies this handler that a port is being closed. |
| 124 void ClosePort(Dart_Port port); | 76 void ClosePort(Dart_Port port); |
| 77 |
| 78 // Notifies this handler that all ports are being closed. |
| 125 void CloseAllPorts(); | 79 void CloseAllPorts(); |
| 126 | 80 |
| 127 // A message handler tracks how many live ports it has. | |
| 128 bool HasLivePorts() const { return live_ports_ > 0; } | |
| 129 void increment_live_ports() { | |
| 130 #if defined(DEBUG) | |
| 131 CheckAccess(); | |
| 132 #endif | |
| 133 live_ports_++; | |
| 134 } | |
| 135 void decrement_live_ports() { | |
| 136 #if defined(DEBUG) | |
| 137 CheckAccess(); | |
| 138 #endif | |
| 139 live_ports_--; | |
| 140 } | |
| 141 | |
| 142 // Returns true if the handler is owned by the PortMap. | 81 // Returns true if the handler is owned by the PortMap. |
| 143 // | 82 // |
| 144 // This is used to delete handlers when their last live port is closed. | 83 // This is used to delete handlers when their last live port is closed. |
| 145 virtual bool OwnedByPortMap() const { return false; } | 84 virtual bool OwnedByPortMap() const { return false; } |
| 146 | 85 |
| 147 MessageQueue* queue() const { return queue_; } | 86 void increment_live_ports(); |
| 87 void decrement_live_ports(); |
| 88 // ------------ END PortMap API ------------ |
| 89 |
| 90 // Custom message notification. Optionally provided by subclass. |
| 91 virtual void MessageNotify(Message::Priority priority); |
| 92 |
| 93 // Handles a single message. Provided by subclass. |
| 94 // |
| 95 // Returns true on success. |
| 96 virtual bool HandleMessage(Message* message) = 0; |
| 148 | 97 |
| 149 private: | 98 private: |
| 99 friend class PortMap; |
| 100 friend class MessageHandlerTestPeer; |
| 101 friend class MessageHandlerTask; |
| 102 |
| 103 // Called by MessageHandlerTask to process our task queue. |
| 104 void TaskCallback(); |
| 105 |
| 106 // Dequeue the next message. Prefer messages from the oob_queue_ to |
| 107 // messages from the queue_. |
| 108 Message* DequeueMessage(Message::Priority min_priority); |
| 109 |
| 110 // Handles any pending messages. |
| 111 bool HandleMessages(bool allow_normal_messages, |
| 112 bool allow_multiple_normal_messages); |
| 113 |
| 114 Monitor monitor_; // Protects all fields in MessageHandler. |
| 115 MessageQueue* queue_; |
| 116 MessageQueue* oob_queue_; |
| 150 intptr_t live_ports_; | 117 intptr_t live_ports_; |
| 151 MessageQueue* queue_; | 118 ThreadPool* pool_; |
| 119 ThreadPool::Task* task_; |
| 120 StartCallback start_callback_; |
| 121 EndCallback end_callback_; |
| 122 CallbackData callback_data_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(MessageHandler); |
| 152 }; | 125 }; |
| 153 | 126 |
| 154 } // namespace dart | 127 } // namespace dart |
| 155 | 128 |
| 156 #endif // VM_MESSAGE_H_ | 129 #endif // VM_MESSAGE_HANDLER_H_ |
| OLD | NEW |