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