| 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_H_ |
| 6 #define VM_MESSAGE_H_ | 6 #define VM_MESSAGE_H_ |
| 7 | 7 |
| 8 #include "vm/thread.h" | 8 #include "vm/thread.h" |
| 9 | 9 |
| 10 // Duplicated from dart_api.h to avoid including the whole header. | 10 // Duplicated from dart_api.h to avoid including the whole header. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 // There is a message queue per isolate. | 62 // There is a message queue per isolate. |
| 63 class MessageQueue { | 63 class MessageQueue { |
| 64 public: | 64 public: |
| 65 MessageQueue(); | 65 MessageQueue(); |
| 66 ~MessageQueue(); | 66 ~MessageQueue(); |
| 67 | 67 |
| 68 void Enqueue(Message* msg); | 68 void Enqueue(Message* msg); |
| 69 | 69 |
| 70 // Gets the next message from the message queue if available. | 70 // Gets the next message from the message queue, possibly blocking |
| 71 // Returns NULL if no message is available. | 71 // if no message is available. 'millis' is a timeout in |
| 72 Message* Dequeue(); | 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(); |
| 73 | 80 |
| 74 void Flush(Dart_Port port); | 81 void Flush(Dart_Port port); |
| 75 void FlushAll(); | 82 void FlushAll(); |
| 76 | 83 |
| 77 private: | 84 private: |
| 78 friend class MessageQueueTestPeer; | 85 friend class MessageQueueTestPeer; |
| 79 | 86 |
| 87 Message* DequeueNoWaitHoldsLock(); |
| 88 |
| 89 Monitor monitor_; |
| 80 Message* head_[Message::kNumPriorities]; | 90 Message* head_[Message::kNumPriorities]; |
| 81 Message* tail_[Message::kNumPriorities]; | 91 Message* tail_[Message::kNumPriorities]; |
| 82 | 92 |
| 83 DISALLOW_COPY_AND_ASSIGN(MessageQueue); | 93 DISALLOW_COPY_AND_ASSIGN(MessageQueue); |
| 84 }; | 94 }; |
| 85 | 95 |
| 86 // A MessageHandler is an entity capable of accepting messages. | 96 // A MessageHandler is an entity capable of accepting messages. |
| 87 class MessageHandler { | 97 class MessageHandler { |
| 88 protected: | 98 protected: |
| 99 MessageHandler(); |
| 100 |
| 89 // Allows subclasses to provide custom message notification. | 101 // Allows subclasses to provide custom message notification. |
| 90 virtual void MessageNotify(Message::Priority priority); | 102 virtual void MessageNotify(Message::Priority priority); |
| 91 | 103 |
| 92 public: | 104 public: |
| 93 MessageHandler(); | |
| 94 virtual ~MessageHandler(); | 105 virtual ~MessageHandler(); |
| 95 | 106 |
| 96 // Allow subclasses to provide a handler name. | 107 // Allow subclasses to provide a handler name. |
| 97 virtual const char* name() const; | 108 virtual const char* name() const; |
| 98 | 109 |
| 99 #if defined(DEBUG) | 110 #if defined(DEBUG) |
| 100 // Check that it is safe to access this message handler. | 111 // Check that it is safe to access this message handler. |
| 101 // | 112 // |
| 102 // For example, if this MessageHandler is an isolate, then it is | 113 // For example, if this MessageHandler is an isolate, then it is |
| 103 // only safe to access it when the MessageHandler is the current | 114 // only safe to access it when the MessageHandler is the current |
| 104 // isolate. | 115 // isolate. |
| 105 virtual void CheckAccess(); | 116 virtual void CheckAccess(); |
| 106 #endif | 117 #endif |
| 107 | 118 |
| 108 void PostMessage(Message* message); | 119 void PostMessage(Message* message); |
| 109 void ClosePort(Dart_Port port); | 120 void ClosePort(Dart_Port port); |
| 110 void CloseAllPorts(); | 121 void CloseAllPorts(); |
| 111 | 122 |
| 112 // Gets the next message from the message queue, possibly blocking | |
| 113 // if no message is available. 'millis' is a timeout in | |
| 114 // milliseconds. If 'millis' is 0, then this means to block | |
| 115 // indefinitely. May block if no message is available. May return | |
| 116 // NULL even if 'millis' is 0 due to spurious wakeups. | |
| 117 Message* Dequeue(int64_t millis); | |
| 118 | |
| 119 // Gets the next message from the message queue if available. Will | |
| 120 // not block. | |
| 121 Message* DequeueNoWait(); | |
| 122 | |
| 123 // A message handler tracks how many live ports it has. | 123 // A message handler tracks how many live ports it has. |
| 124 bool HasLivePorts() const { return live_ports_ > 0; } | 124 bool HasLivePorts() const { return live_ports_ > 0; } |
| 125 void increment_live_ports() { | 125 void increment_live_ports() { |
| 126 #if defined(DEBUG) | 126 #if defined(DEBUG) |
| 127 CheckAccess(); | 127 CheckAccess(); |
| 128 #endif | 128 #endif |
| 129 live_ports_++; | 129 live_ports_++; |
| 130 } | 130 } |
| 131 void decrement_live_ports() { | 131 void decrement_live_ports() { |
| 132 #if defined(DEBUG) | 132 #if defined(DEBUG) |
| 133 CheckAccess(); | 133 CheckAccess(); |
| 134 #endif | 134 #endif |
| 135 live_ports_--; | 135 live_ports_--; |
| 136 } | 136 } |
| 137 | 137 |
| 138 // Returns true if the handler is owned by the PortMap. | 138 // Returns true if the handler is owned by the PortMap. |
| 139 // | 139 // |
| 140 // This is used to delete handlers when their last live port is closed. | 140 // This is used to delete handlers when their last live port is closed. |
| 141 virtual bool OwnedByPortMap() const { return false; } | 141 virtual bool OwnedByPortMap() const { return false; } |
| 142 | 142 |
| 143 MessageQueue* queue() const { return queue_; } |
| 144 |
| 143 private: | 145 private: |
| 144 friend class MessageHandlerTestPeer; | |
| 145 | |
| 146 Monitor monitor_; | |
| 147 intptr_t live_ports_; | 146 intptr_t live_ports_; |
| 148 MessageQueue* queue_; | 147 MessageQueue* queue_; |
| 149 }; | 148 }; |
| 150 | 149 |
| 151 } // namespace dart | 150 } // namespace dart |
| 152 | 151 |
| 153 #endif // VM_MESSAGE_H_ | 152 #endif // VM_MESSAGE_H_ |
| OLD | NEW |