| 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 "include/dart_api.h" |
| 9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
| 10 #include "vm/globals.h" | 10 #include "vm/globals.h" |
| 11 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 | 13 |
| 14 class Isolate; | 14 class Isolate; |
| 15 class Message; | 15 class Message; |
| 16 class MessageHandler; |
| 16 class Mutex; | 17 class Mutex; |
| 17 class PortMapTestPeer; | 18 class PortMapTestPeer; |
| 18 | 19 |
| 19 class PortMap: public AllStatic { | 20 class PortMap: public AllStatic { |
| 20 public: | 21 public: |
| 21 // Allocate a port in the current isolate and return its VM-global id. | 22 // Allocate a port for the provided handler and return its VM-global id. |
| 22 static Dart_Port CreatePort(); | 23 static Dart_Port CreatePort(MessageHandler* handler); |
| 23 | 24 |
| 24 // Indicates that a port has had a ReceivePort created for it at the | 25 // Indicates that a port has had a ReceivePort created for it at the |
| 25 // dart language level. The port remains live until it is closed. | 26 // dart language level. The port remains live until it is closed. |
| 26 static void SetLive(Dart_Port id); | 27 static void SetLive(Dart_Port id); |
| 27 | 28 |
| 28 // Close the port with id. All pending messages will be dropped. | 29 // Close the port with id. All pending messages will be dropped. |
| 29 static void ClosePort(Dart_Port id); | 30 // |
| 31 // Returns true if the port is successfully closed. |
| 32 static bool ClosePort(Dart_Port id); |
| 30 | 33 |
| 31 // Close all the ports of the current isolate. | 34 // Close all the ports for the provided handler. |
| 32 static void ClosePorts(); | 35 static void ClosePorts(MessageHandler* handler); |
| 33 | 36 |
| 34 // Enqueues the message in the port with id. Returns false if the port is not | 37 // Enqueues the message in the port with id. Returns false if the port is not |
| 35 // active any longer. | 38 // active any longer. |
| 36 // | 39 // |
| 37 // Claims ownership of 'message'. | 40 // Claims ownership of 'message'. |
| 38 static bool PostMessage(Message* message); | 41 static bool PostMessage(Message* message); |
| 39 | 42 |
| 40 static void InitOnce(); | 43 static void InitOnce(); |
| 41 | 44 |
| 42 private: | 45 private: |
| 43 friend class dart::PortMapTestPeer; | 46 friend class dart::PortMapTestPeer; |
| 44 | 47 |
| 45 // Mapping between port numbers and isolates. | 48 // Mapping between port numbers and handlers. |
| 46 // Free entries have id == 0 and isolate == NULL. Deleted entries have id == 0 | 49 // |
| 47 // and isolate == deleted_entry_. | 50 // Free entries have id == 0 and handler == NULL. Deleted entries |
| 51 // have id == 0 and handler == deleted_entry_. |
| 48 typedef struct { | 52 typedef struct { |
| 49 Dart_Port port; | 53 Dart_Port port; |
| 50 Isolate* isolate; | 54 MessageHandler* handler; |
| 51 bool live; | 55 bool live; |
| 52 } Entry; | 56 } Entry; |
| 53 | 57 |
| 54 // Allocate a new unique port. | 58 // Allocate a new unique port. |
| 55 static Dart_Port AllocatePort(); | 59 static Dart_Port AllocatePort(); |
| 56 | 60 |
| 57 static bool IsActivePort(Dart_Port id); | 61 static bool IsActivePort(Dart_Port id); |
| 58 static bool IsLivePort(Dart_Port id); | 62 static bool IsLivePort(Dart_Port id); |
| 59 | 63 |
| 60 static intptr_t FindPort(Dart_Port port); | 64 static intptr_t FindPort(Dart_Port port); |
| 61 static void Rehash(intptr_t new_capacity); | 65 static void Rehash(intptr_t new_capacity); |
| 62 | 66 |
| 63 static void MaintainInvariants(); | 67 static void MaintainInvariants(); |
| 64 | 68 |
| 65 // Lock protecting access to the port map. | 69 // Lock protecting access to the port map. |
| 66 static Mutex* mutex_; | 70 static Mutex* mutex_; |
| 67 | 71 |
| 68 // Hashmap of ports. | 72 // Hashmap of ports. |
| 69 static Entry* map_; | 73 static Entry* map_; |
| 70 static Isolate* deleted_entry_; | 74 static MessageHandler* deleted_entry_; |
| 71 static intptr_t capacity_; | 75 static intptr_t capacity_; |
| 72 static intptr_t used_; | 76 static intptr_t used_; |
| 73 static intptr_t deleted_; | 77 static intptr_t deleted_; |
| 74 | 78 |
| 75 static Dart_Port next_port_; | 79 static Dart_Port next_port_; |
| 76 }; | 80 }; |
| 77 | 81 |
| 78 } // namespace dart | 82 } // namespace dart |
| 79 | 83 |
| 80 #endif // VM_PORT_H_ | 84 #endif // VM_PORT_H_ |
| OLD | NEW |