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() { |
(...skipping 14 matching lines...) Expand all Loading... |
25 _portMap.remove(_id); | 25 _portMap.remove(_id); |
26 _closeInternal(_id); | 26 _closeInternal(_id); |
27 } | 27 } |
28 | 28 |
29 SendPort toSendPort() { | 29 SendPort toSendPort() { |
30 return new SendPortImpl(_id); | 30 return new SendPortImpl(_id); |
31 } | 31 } |
32 | 32 |
33 /**** Internal implementation details ****/ | 33 /**** Internal implementation details ****/ |
34 // Called from the VM to create a new ReceivePort instance. | 34 // Called from the VM to create a new ReceivePort instance. |
35 static ReceivePortImpl create_(int id) { | 35 static ReceivePortImpl _get_or_create(int id) { |
| 36 if (_portMap !== null) { |
| 37 ReceivePortImpl port = _portMap[id]; |
| 38 if (port !== null) { |
| 39 return port; |
| 40 } |
| 41 } |
36 return new ReceivePortImpl._internal(id); | 42 return new ReceivePortImpl._internal(id); |
37 } | 43 } |
38 ReceivePortImpl._internal(int id) : _id = id { | 44 ReceivePortImpl._internal(int id) : _id = id { |
39 if (_portMap === null) { | 45 if (_portMap === null) { |
40 _portMap = new Map(); | 46 _portMap = new Map(); |
41 } | 47 } |
42 _portMap[id] = this; | 48 _portMap[id] = this; |
43 } | 49 } |
44 | 50 |
45 // Called from the VM to dispatch to the handler. | 51 // Called from the VM to dispatch to the handler. |
46 static void handleMessage_(int id, int replyId, var message) { | 52 static void _handleMessage(int id, int replyId, var message) { |
47 assert(_portMap !== null); | 53 assert(_portMap !== null); |
48 ReceivePort port = _portMap[id]; | 54 ReceivePort port = _portMap[id]; |
49 SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); | 55 SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); |
50 (port._onMessage)(message, replyTo); | 56 (port._onMessage)(message, replyTo); |
51 } | 57 } |
52 | 58 |
53 // Call into the VM to close the VM maintained mappings. | 59 // Call into the VM to close the VM maintained mappings. |
54 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 60 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; |
55 | 61 |
56 final int _id; | 62 final int _id; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 return (other is SendPortImpl) && _id == other._id; | 118 return (other is SendPortImpl) && _id == other._id; |
113 } | 119 } |
114 | 120 |
115 int hashCode() { | 121 int hashCode() { |
116 return _id; | 122 return _id; |
117 } | 123 } |
118 | 124 |
119 /*--- private implementation ---*/ | 125 /*--- private implementation ---*/ |
120 const SendPortImpl(int id) : _id = id; | 126 const SendPortImpl(int id) : _id = id; |
121 | 127 |
122 // SendPortImpl.create_ is called from the VM when a new SendPort instance is | 128 // SendPortImpl._create is called from the VM when a new SendPort instance is |
123 // needed by the VM code. | 129 // needed by the VM code. |
124 static SendPort create_(int id) { | 130 static SendPort _create(int id) { |
125 return new SendPortImpl(id); | 131 return new SendPortImpl(id); |
126 } | 132 } |
127 | 133 |
128 // Forward the implementation of sending messages to the VM. Only port ids | 134 // Forward the implementation of sending messages to the VM. Only port ids |
129 // are being handed to the VM. | 135 // are being handed to the VM. |
130 static _sendInternal(int sendId, int replyId, var message) | 136 static _sendInternal(int sendId, int replyId, var message) |
131 native "SendPortImpl_sendInternal_"; | 137 native "SendPortImpl_sendInternal_"; |
132 | 138 |
133 final int _id; | 139 final int _id; |
134 } | 140 } |
135 | 141 |
136 | 142 |
137 class IsolateNatives { | 143 class IsolateNatives { |
138 static Future<SendPort> spawn(Isolate isolate, bool isLight) { | 144 static Future<SendPort> spawn(Isolate isolate, bool isLight) { |
139 Completer<SendPort> completer = new Completer<SendPort>(); | 145 Completer<SendPort> completer = new Completer<SendPort>(); |
140 SendPort port = _start(isolate, isLight); | 146 SendPort port = _start(isolate, isLight); |
141 completer.complete(port); | 147 completer.complete(port); |
142 return completer.future; | 148 return completer.future; |
143 } | 149 } |
144 | 150 |
145 // Starts a new isolate calling the run method on a new instance of the | 151 // Starts a new isolate calling the run method on a new instance of the |
146 // remote class's type. | 152 // remote class's type. |
147 // Returns the send port which is passed to the newly created isolate. | 153 // 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. | 154 // This method is being dispatched to from the public core library code. |
149 static SendPort _start(Isolate isolate, bool light) | 155 static SendPort _start(Isolate isolate, bool light) |
150 native "IsolateNatives_start"; | 156 native "IsolateNatives_start"; |
151 } | 157 } |
OLD | NEW |