| 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_PORT_H_ | 5 #ifndef VM_MESSAGE_QUEUE_H_ |
| 6 #define VM_PORT_H_ | 6 #define VM_MESSAGE_QUEUE_H_ |
| 7 | 7 |
| 8 #include "vm/allocation.h" | 8 #include "include/dart_api.h" |
| 9 #include "vm/globals.h" | |
| 10 #include "vm/growable_array.h" | |
| 11 #include "vm/isolate.h" | |
| 12 #include "vm/thread.h" | 9 #include "vm/thread.h" |
| 13 | 10 |
| 14 namespace dart { | 11 namespace dart { |
| 15 | 12 |
| 16 class PortMessage { | 13 class PortMessage { |
| 17 public: | 14 public: |
| 18 // A new message to be sent between two isolates. The data handed to this | 15 // A new message to be sent between two isolates. The data handed to this |
| 19 // message will be disposed by calling free() once the message object is | 16 // message will be disposed by calling free() once the message object is |
| 20 // being destructed (after delivery or when the receiving port is closed). | 17 // being destructed (after delivery or when the receiving port is closed). |
| 21 PortMessage(intptr_t dest_id, intptr_t reply_id, void* data) | 18 PortMessage(Dart_Port dest_port, Dart_Port reply_port, Dart_Message data) |
| 22 : next_(NULL), | 19 : next_(NULL), |
| 23 dest_id_(dest_id), | 20 dest_port_(dest_port), |
| 24 reply_id_(reply_id), | 21 reply_port_(reply_port), |
| 25 data_(data) {} | 22 data_(data) {} |
| 26 ~PortMessage() { | 23 ~PortMessage() { |
| 27 free(data_); | 24 free(data_); |
| 28 } | 25 } |
| 29 | 26 |
| 30 intptr_t dest_id() const { return dest_id_; } | 27 Dart_Port dest_port() const { return dest_port_; } |
| 31 intptr_t reply_id() const { return reply_id_; } | 28 Dart_Port reply_port() const { return reply_port_; } |
| 32 void* data() const { return data_; } | 29 Dart_Message data() const { return data_; } |
| 33 | |
| 34 void Handle(); | |
| 35 | 30 |
| 36 private: | 31 private: |
| 32 friend class MessageQueue; |
| 33 |
| 37 PortMessage* next_; | 34 PortMessage* next_; |
| 38 intptr_t dest_id_; | 35 Dart_Port dest_port_; |
| 39 intptr_t reply_id_; | 36 Dart_Port reply_port_; |
| 40 void* data_; | 37 Dart_Message data_; |
| 41 | |
| 42 friend class MessageQueue; | |
| 43 | 38 |
| 44 DISALLOW_COPY_AND_ASSIGN(PortMessage); | 39 DISALLOW_COPY_AND_ASSIGN(PortMessage); |
| 45 }; | 40 }; |
| 46 | 41 |
| 47 | 42 |
| 48 // There is a message queue per isolate. Access to the message queue should be | 43 // There is a message queue per isolate. |
| 49 // protected by the isolate monitor. | |
| 50 class MessageQueue { | 44 class MessageQueue { |
| 51 public: | 45 public: |
| 52 MessageQueue() : head_(NULL), tail_(NULL) {} | 46 MessageQueue() : head_(NULL), tail_(NULL) {} |
| 53 ~MessageQueue(); | 47 ~MessageQueue(); |
| 54 | 48 |
| 55 void Enqueue(PortMessage* msg); | 49 void Enqueue(PortMessage* msg); |
| 56 PortMessage* Dequeue(); | 50 PortMessage* Dequeue(); |
| 57 | 51 |
| 58 void Flush(intptr_t id); | 52 void Wait(int64_t millis); |
| 53 |
| 54 void Flush(Dart_Port port); |
| 59 void FlushAll(); | 55 void FlushAll(); |
| 60 | 56 |
| 61 private: | 57 private: |
| 58 friend class MessageQueueTestPeer; |
| 59 |
| 60 Monitor monitor_; |
| 62 PortMessage* head_; | 61 PortMessage* head_; |
| 63 PortMessage* tail_; | 62 PortMessage* tail_; |
| 64 | 63 |
| 65 DISALLOW_COPY_AND_ASSIGN(MessageQueue); | 64 DISALLOW_COPY_AND_ASSIGN(MessageQueue); |
| 66 }; | 65 }; |
| 67 | 66 |
| 68 | |
| 69 class PortMap: public AllStatic { | |
| 70 public: | |
| 71 // Allocate a port in the current isolate and return its VM-global id. | |
| 72 static intptr_t CreatePort(); | |
| 73 | |
| 74 // Close the port with id. All pending messages will be dropped. | |
| 75 static void ClosePort(intptr_t id); | |
| 76 | |
| 77 // Close all the ports of the current isolate. | |
| 78 static void ClosePorts(); | |
| 79 | |
| 80 static bool IsActivePort(intptr_t id); | |
| 81 | |
| 82 // Enqueues the message in the port with id. Returns false if the port is not | |
| 83 // active any longer. | |
| 84 static bool PostMessage(PortMessage* msg); | |
| 85 | |
| 86 // Dequeue the next message pending for this isolate. Returns null if timeout | |
| 87 // was reached before a message was posted. | |
| 88 static PortMessage* ReceiveMessage(int64_t millis); | |
| 89 | |
| 90 static void InitOnce(); | |
| 91 | |
| 92 private: | |
| 93 // Mapping between port numbers and isolates. | |
| 94 // Free entries have id == 0 and isolate == NULL. Deleted entries have id == 0 | |
| 95 // and isolate == deleted_entry_. | |
| 96 typedef struct { | |
| 97 intptr_t id; | |
| 98 Isolate* isolate; | |
| 99 } Entry; | |
| 100 | |
| 101 // Allocate a new unique port id. | |
| 102 static intptr_t AllocateId(); | |
| 103 | |
| 104 static intptr_t FindId(intptr_t id); | |
| 105 static void Rehash(intptr_t new_capacity); | |
| 106 | |
| 107 static void MaintainInvariants(); | |
| 108 | |
| 109 // Lock protecting access to the port map. | |
| 110 static Mutex* mutex_; | |
| 111 | |
| 112 // Hashmap of ports. | |
| 113 static Entry* map_; | |
| 114 static Isolate* deleted_entry_; | |
| 115 static intptr_t capacity_; | |
| 116 static intptr_t used_; | |
| 117 static intptr_t deleted_; | |
| 118 | |
| 119 static intptr_t next_id_; | |
| 120 }; | |
| 121 | |
| 122 } // namespace dart | 67 } // namespace dart |
| 123 | 68 |
| 124 #endif // VM_PORT_H_ | 69 #endif // VM_MESSAGE_QUEUE_H_ |
| OLD | NEW |