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