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 class ReceivePortFactory { | 5 class ReceivePortFactory { |
6 factory ReceivePort() { | 6 factory ReceivePort() { |
7 return new ReceivePortImpl(); | 7 return new ReceivePortImpl(); |
8 } | 8 } |
9 | 9 |
10 factory ReceivePort.singleShot() { | 10 factory ReceivePort.singleShot() { |
11 return new ReceivePortSingleShotImpl(); | 11 return new ReceivePortSingleShotImpl(); |
12 } | 12 } |
13 } | 13 } |
14 | 14 |
15 | 15 |
16 class ReceivePortImpl implements ReceivePort { | 16 class ReceivePortImpl implements ReceivePort { |
17 /*--- public interface ---*/ | 17 /*--- public interface ---*/ |
18 factory ReceivePortImpl() native "ReceivePortImpl_factory"; | 18 factory ReceivePortImpl() native "ReceivePortImpl_factory"; |
19 | 19 |
20 receive(void onMessage(var message, SendPort replyTo)) { | 20 receive(void onMessage(var message, SendPort replyTo)) { |
21 _numLivePorts++; | |
siva
2011/11/24 00:52:31
If I call receive multiple times on the same recei
turnidge
2011/11/29 01:01:31
I redid the change so that isolate.dart doesn't ne
| |
21 _onMessage = onMessage; | 22 _onMessage = onMessage; |
22 } | 23 } |
23 | 24 |
24 close() { | 25 close() { |
25 _portMap.remove(_id); | 26 _portMap.remove(_id); |
26 _closeInternal(_id); | 27 _closeInternal(_id); |
28 _numLivePorts--; | |
27 } | 29 } |
28 | 30 |
29 SendPort toSendPort() { | 31 SendPort toSendPort() { |
30 return new SendPortImpl(_id); | 32 return new SendPortImpl(_id); |
31 } | 33 } |
32 | 34 |
33 /**** Internal implementation details ****/ | 35 /**** Internal implementation details ****/ |
34 // Called from the VM to create a new ReceivePort instance. | 36 // Called from the VM to create a new ReceivePort instance. |
35 static ReceivePortImpl create_(int id) { | 37 static ReceivePortImpl _create(int id) { |
36 return new ReceivePortImpl._internal(id); | 38 return new ReceivePortImpl._internal(id); |
37 } | 39 } |
38 ReceivePortImpl._internal(int id) : _id = id { | 40 ReceivePortImpl._internal(int id) : _id = id { |
39 if (_portMap === null) { | 41 if (_portMap === null) { |
40 _portMap = new Map(); | 42 _portMap = new Map(); |
43 _numLivePorts = 0; | |
41 } | 44 } |
42 _portMap[id] = this; | 45 _portMap[id] = this; |
43 } | 46 } |
44 | 47 |
48 static int _getNumLivePorts() => _numLivePorts === null ? 0 : _numLivePorts; | |
49 | |
45 // Called from the VM to dispatch to the handler. | 50 // Called from the VM to dispatch to the handler. |
46 static void handleMessage_(int id, int replyId, var message) { | 51 static void _handleMessage(int id, int replyId, var message) { |
47 assert(_portMap !== null); | 52 assert(_portMap !== null); |
48 ReceivePort port = _portMap[id]; | 53 ReceivePort port = _portMap[id]; |
49 SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); | 54 SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); |
50 (port._onMessage)(message, replyTo); | 55 (port._onMessage)(message, replyTo); |
51 } | 56 } |
52 | 57 |
53 // Call into the VM to close the VM maintained mappings. | 58 // Call into the VM to close the VM maintained mappings. |
54 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 59 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; |
55 | 60 |
56 final int _id; | 61 final int _id; |
57 var _onMessage; | 62 var _onMessage; |
58 | 63 |
59 // id to ReceivePort mapping. | 64 // id to ReceivePort mapping. |
60 static Map _portMap; | 65 static Map _portMap; |
66 | |
67 // The number of ReceivePorts which have called receive. | |
68 static int _numLivePorts; | |
61 } | 69 } |
62 | 70 |
63 | 71 |
64 class ReceivePortSingleShotImpl implements ReceivePort { | 72 class ReceivePortSingleShotImpl implements ReceivePort { |
65 | 73 |
66 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } | 74 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } |
67 | 75 |
68 void receive(void callback(var message, SendPort replyTo)) { | 76 void receive(void callback(var message, SendPort replyTo)) { |
69 _port.receive((var message, SendPort replyTo) { | 77 _port.receive((var message, SendPort replyTo) { |
70 _port.close(); | 78 _port.close(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 return (other is SendPortImpl) && _id == other._id; | 120 return (other is SendPortImpl) && _id == other._id; |
113 } | 121 } |
114 | 122 |
115 int hashCode() { | 123 int hashCode() { |
116 return _id; | 124 return _id; |
117 } | 125 } |
118 | 126 |
119 /*--- private implementation ---*/ | 127 /*--- private implementation ---*/ |
120 const SendPortImpl(int id) : _id = id; | 128 const SendPortImpl(int id) : _id = id; |
121 | 129 |
122 // SendPortImpl.create_ is called from the VM when a new SendPort instance is | 130 // SendPortImpl._create is called from the VM when a new SendPort instance is |
123 // needed by the VM code. | 131 // needed by the VM code. |
124 static SendPort create_(int id) { | 132 static SendPort _create(int id) { |
125 return new SendPortImpl(id); | 133 return new SendPortImpl(id); |
126 } | 134 } |
127 | 135 |
128 // Forward the implementation of sending messages to the VM. Only port ids | 136 // Forward the implementation of sending messages to the VM. Only port ids |
129 // are being handed to the VM. | 137 // are being handed to the VM. |
130 static _sendInternal(int sendId, int replyId, var message) | 138 static _sendInternal(int sendId, int replyId, var message) |
131 native "SendPortImpl_sendInternal_"; | 139 native "SendPortImpl_sendInternal_"; |
132 | 140 |
133 final int _id; | 141 final int _id; |
134 } | 142 } |
135 | 143 |
136 | 144 |
137 class IsolateNatives { | 145 class IsolateNatives { |
138 static Future<SendPort> spawn(Isolate isolate, bool isLight) { | 146 static Future<SendPort> spawn(Isolate isolate, bool isLight) { |
139 Completer<SendPort> completer = new Completer<SendPort>(); | 147 Completer<SendPort> completer = new Completer<SendPort>(); |
140 SendPort port = _start(isolate, isLight); | 148 SendPort port = _start(isolate, isLight); |
141 completer.complete(port); | 149 completer.complete(port); |
142 return completer.future; | 150 return completer.future; |
143 } | 151 } |
144 | 152 |
145 // Starts a new isolate calling the run method on a new instance of the | 153 // Starts a new isolate calling the run method on a new instance of the |
146 // remote class's type. | 154 // remote class's type. |
147 // Returns the send port which is passed to the newly created isolate. | 155 // Returns the send port which is passed to the newly created isolate. |
148 // This method is being dispatched to from the public core library code. | 156 // This method is being dispatched to from the public core library code. |
149 static SendPort _start(Isolate isolate, bool light) | 157 static SendPort _start(Isolate isolate, bool light) |
150 native "IsolateNatives_start"; | 158 native "IsolateNatives_start"; |
151 } | 159 } |
OLD | NEW |