Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 _CloseToken { | 5 patch class ReceivePort { |
| 6 /// This token is sent from [IsolateSink]s to [IsolateStream]s to ask them to | 6 /* patch */ factory ReceivePort() = _ReceivePortImpl; |
| 7 /// close themselves. | 7 |
| 8 const _CloseToken(); | 8 /* patch */ factory ReceivePort.fromRawReceivePort(RawReceivePort rawPort) = |
| 9 _ReceivePortImpl.fromRawReceivePort; | |
| 9 } | 10 } |
| 10 | 11 |
| 11 patch bool _isCloseToken(var object) { | 12 patch class RawReceivePort { |
| 12 // TODO(floitsch): can we compare against const _CloseToken()? | 13 /** |
| 13 return object is _CloseToken; | 14 * Opens a long-lived port for receiving messages. |
| 14 } | 15 * |
| 15 | 16 * A [RawReceivePort] is low level and does not work with [Zone]s. It |
| 16 patch class MessageBox { | 17 * can not be paused. The data-handler must be set before the first |
| 17 /* patch */ MessageBox.oneShot() : this._oneShot(new ReceivePort()); | 18 * event is received. |
|
Lasse Reichstein Nielsen
2013/10/25 09:42:49
How about not making the handler optional?
You ca
floitsch
2013/10/25 13:11:01
The port doesn't care. It just tries to call it. I
| |
| 18 MessageBox._oneShot(ReceivePort receivePort) | 19 */ |
| 19 : stream = new IsolateStream._fromOriginalReceivePortOneShot(receivePort), | 20 /* patch */ factory RawReceivePort([void handler(event)]) { |
| 20 sink = new _IsolateSink._fromPort(receivePort.toSendPort()); | 21 _RawReceivePortImpl result = new _RawReceivePortImpl(); |
| 21 | 22 result.handler = handler; |
| 22 /* patch */ MessageBox() : this._(new ReceivePort()); | 23 return result; |
| 23 MessageBox._(ReceivePort receivePort) | |
| 24 : stream = new IsolateStream._fromOriginalReceivePort(receivePort), | |
| 25 sink = new _IsolateSink._fromPort(receivePort.toSendPort()); | |
| 26 } | |
| 27 | |
| 28 class _IsolateSink implements IsolateSink { | |
| 29 bool _isClosed = false; | |
| 30 final SendPort _port; | |
| 31 _IsolateSink._fromPort(this._port); | |
| 32 | |
| 33 void add(dynamic message) { | |
| 34 _port.send(message); | |
| 35 } | |
| 36 | |
| 37 void addError(Object errorEvent) { | |
| 38 throw new UnimplementedError("addError on isolate streams"); | |
| 39 } | |
| 40 | |
| 41 void close() { | |
| 42 if (_isClosed) return; | |
| 43 add(const _CloseToken()); | |
| 44 _isClosed = true; | |
| 45 } | |
| 46 | |
| 47 bool operator==(var other) { | |
| 48 return other is IsolateSink && _port == other._port; | |
| 49 } | |
| 50 | |
| 51 int get hashCode => _port.hashCode + 499; | |
| 52 } | |
| 53 | |
| 54 patch IsolateSink streamSpawnFunction( | |
| 55 void topLevelFunction(), | |
| 56 [bool unhandledExceptionCallback(IsolateUnhandledException e)]) { | |
| 57 SendPort sendPort = spawnFunction(topLevelFunction, | |
| 58 unhandledExceptionCallback); | |
| 59 return new _IsolateSink._fromPort(sendPort); | |
| 60 } | |
| 61 | |
| 62 patch class ReceivePort { | |
| 63 /* patch */ factory ReceivePort() { | |
| 64 return new _ReceivePortImpl(); | |
| 65 } | 24 } |
| 66 } | 25 } |
| 67 | 26 |
| 68 class _ReceivePortImpl implements ReceivePort { | 27 class _ReceivePortImpl extends Stream implements ReceivePort { |
| 69 factory _ReceivePortImpl() native "ReceivePortImpl_factory"; | 28 _ReceivePortImpl() : this.fromRawReceivePort(new RawReceivePort()); |
| 70 | 29 |
| 71 receive(void onMessage(var message, SendPort replyTo)) { | 30 _ReceivePortImpl.fromRawReceivePort(this._rawPort) { |
| 72 _onMessage = onMessage; | 31 _controller = new StreamController(onCancel: close, sync: true); |
| 32 _rawPort.handler = _controller.add; | |
| 73 } | 33 } |
| 74 | 34 |
| 35 SendPort get sendPort { | |
| 36 return _rawPort.sendPort; | |
| 37 } | |
| 38 | |
| 39 StreamSubscription listen(void onData(var message), | |
| 40 { Function onError, | |
| 41 void onDone(), | |
| 42 bool cancelOnError }) { | |
| 43 return _controller.stream.listen(onData); | |
| 44 } | |
| 45 | |
| 46 close() { | |
| 47 _rawPort.close(); | |
| 48 _controller.close(); | |
| 49 } | |
| 50 | |
| 51 final RawReceivePort _rawPort; | |
| 52 StreamController _controller; | |
| 53 } | |
| 54 | |
| 55 class _RawReceivePortImpl implements RawReceivePort { | |
| 56 factory _RawReceivePortImpl() native "RawReceivePortImpl_factory"; | |
| 57 | |
| 75 close() { | 58 close() { |
| 76 _portMap.remove(_id); | 59 _portMap.remove(_id); |
| 77 _closeInternal(_id); | 60 _closeInternal(_id); |
| 78 } | 61 } |
| 79 | 62 |
| 80 SendPort toSendPort() { | 63 SendPort get sendPort { |
| 81 return new _SendPortImpl(_id); | 64 return new _SendPortImpl(_id); |
| 82 } | 65 } |
| 83 | 66 |
| 84 /**** Internal implementation details ****/ | 67 /**** Internal implementation details ****/ |
| 85 // Called from the VM to create a new ReceivePort instance. | 68 // Called from the VM to create a new RawReceivePort instance. |
| 86 static _ReceivePortImpl _get_or_create(int id) { | 69 static _RawReceivePortImpl _get_or_create(int id) { |
| 87 if (_portMap != null) { | 70 if (_portMap != null) { |
| 88 _ReceivePortImpl port = _portMap[id]; | 71 _RawReceivePortImpl port = _portMap[id]; |
| 89 if (port != null) { | 72 if (port != null) { |
| 90 return port; | 73 return port; |
| 91 } | 74 } |
| 92 } | 75 } |
| 93 return new _ReceivePortImpl._internal(id); | 76 return new _RawReceivePortImpl._internal(id); |
| 94 } | 77 } |
| 95 | 78 |
| 96 _ReceivePortImpl._internal(int id) : _id = id { | 79 _RawReceivePortImpl._internal(int id) : _id = id { |
| 97 if (_portMap == null) { | 80 if (_portMap == null) { |
| 98 _portMap = new Map(); | 81 _portMap = new Map(); |
|
Ivan Posva
2013/10/25 07:01:46
This does not need to be a DoublyLinkedHashMap, pl
floitsch
2013/10/25 13:11:01
Done.
| |
| 99 } | 82 } |
| 100 _portMap[id] = this; | 83 _portMap[id] = this; |
| 101 } | 84 } |
| 102 | 85 |
| 103 // Called from the VM to retrieve the ReceivePort for a message. | 86 // Called from the VM to retrieve the RawReceivePort for a message. |
| 104 static _ReceivePortImpl _lookupReceivePort(int id) { | 87 static _RawReceivePortImpl _lookupReceivePort(int id) { |
| 105 assert(_portMap != null); | 88 assert(_portMap != null); |
|
Ivan Posva
2013/10/25 07:01:46
These tests would not be needed if it was a static
floitsch
2013/10/25 13:11:01
Done.
| |
| 106 return _portMap[id]; | 89 return _portMap[id]; |
| 107 } | 90 } |
| 108 | 91 |
| 109 // Called from the VM to dispatch to the handler. | 92 // Called from the VM to dispatch to the handler. |
| 110 static void _handleMessage(_ReceivePortImpl port, int replyId, var message) { | 93 static void _handleMessage(_RawReceivePortImpl port, int replyId, var message) { |
|
Lasse Reichstein Nielsen
2013/10/25 09:42:49
long line.
floitsch
2013/10/25 13:11:01
Done.
| |
| 111 assert(port != null); | 94 assert(port != null); |
| 112 SendPort replyTo = (replyId == 0) ? null : new _SendPortImpl(replyId); | 95 port._handler(message); |
| 113 (port._onMessage)(message, replyTo); | |
| 114 } | 96 } |
| 115 | 97 |
| 116 // Call into the VM to close the VM maintained mappings. | 98 // Call into the VM to close the VM maintained mappings. |
| 117 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 99 static _closeInternal(int id) native "RawReceivePortImpl_closeInternal"; |
| 100 | |
| 101 void set handler(Function newHandler) { | |
| 102 this._handler = newHandler; | |
| 103 } | |
| 118 | 104 |
| 119 final int _id; | 105 final int _id; |
| 120 var _onMessage; | 106 Function _handler; |
| 121 | 107 |
| 122 // id to ReceivePort mapping. | 108 // id to RawReceivePort mapping. |
| 123 static Map _portMap; | 109 static Map _portMap; |
| 124 } | 110 } |
| 125 | 111 |
| 126 | 112 |
| 127 class _SendPortImpl implements SendPort { | 113 class _SendPortImpl implements SendPort { |
| 128 /*--- public interface ---*/ | 114 /*--- public interface ---*/ |
| 129 void send(var message, [SendPort replyTo = null]) { | 115 void send(var message, [SendPort replyTo = null]) { |
|
Ivan Posva
2013/10/25 07:01:46
As far as I understand the SendPort.send does not
floitsch
2013/10/25 13:11:01
As discussed. We still need it for Native C commun
| |
| 130 this._sendNow(message, replyTo); | 116 this._sendNow(message, replyTo); |
| 131 } | 117 } |
| 132 | 118 |
| 133 void _sendNow(var message, SendPort replyTo) { | 119 void _sendNow(var message, SendPort replyTo) { |
|
Ivan Posva
2013/10/25 07:01:46
This function makes no sense now. How about callin
floitsch
2013/10/25 13:11:01
ditto.
| |
| 134 int replyId = (replyTo == null) ? 0 : replyTo._id; | 120 int replyId = (replyTo == null) ? 0 : replyTo._id; |
| 135 _sendInternal(_id, replyId, message); | 121 _sendInternal(_id, replyId, message); |
| 136 } | 122 } |
| 137 | 123 |
| 138 Future call(var message) { | |
| 139 final completer = new Completer.sync(); | |
| 140 final port = new _ReceivePortImpl(); | |
| 141 send(message, port.toSendPort()); | |
| 142 port.receive((value, ignoreReplyTo) { | |
| 143 port.close(); | |
| 144 if (value is Exception) { | |
| 145 completer.completeError(value); | |
| 146 } else { | |
| 147 completer.complete(value); | |
| 148 } | |
| 149 }); | |
| 150 return completer.future; | |
| 151 } | |
| 152 | |
| 153 bool operator==(var other) { | 124 bool operator==(var other) { |
| 154 return (other is _SendPortImpl) && _id == other._id; | 125 return (other is _SendPortImpl) && _id == other._id; |
| 155 } | 126 } |
| 156 | 127 |
| 157 int get hashCode { | 128 int get hashCode { |
| 158 return _id; | 129 return _id; |
|
Ivan Posva
2013/10/25 07:01:46
We should not leak the id out ever.
Lasse Reichstein Nielsen
2013/10/25 09:42:49
The value needs to deterministically depend on _id
floitsch
2013/10/25 13:11:01
Done.
floitsch
2013/10/25 13:11:01
Implemented the single Jenkins hash step as sugges
Ivan Posva
2013/10/25 18:41:39
As long as we cannot go from the hash back to the
| |
| 159 } | 130 } |
| 160 | 131 |
| 161 /*--- private implementation ---*/ | 132 /*--- private implementation ---*/ |
| 162 const _SendPortImpl(int id) : _id = id; | 133 const _SendPortImpl(int id) : _id = id; |
| 163 | 134 |
| 164 // _SendPortImpl._create is called from the VM when a new SendPort instance is | 135 // _SendPortImpl._create is called from the VM when a new SendPort instance is |
| 165 // needed by the VM code. | 136 // needed by the VM code. |
| 166 static SendPort _create(int id) { | 137 static SendPort _create(int id) { |
| 167 return new _SendPortImpl(id); | 138 return new _SendPortImpl(id); |
| 168 } | 139 } |
| 169 | 140 |
| 170 // Forward the implementation of sending messages to the VM. Only port ids | 141 // Forward the implementation of sending messages to the VM. Only port ids |
| 171 // are being handed to the VM. | 142 // are being handed to the VM. |
| 172 static _sendInternal(int sendId, int replyId, var message) | 143 static _sendInternal(int sendId, int replyId, var message) |
| 173 native "SendPortImpl_sendInternal_"; | 144 native "SendPortImpl_sendInternal_"; |
| 174 | 145 |
| 175 final int _id; | 146 final int _id; |
| 176 } | 147 } |
| 177 | 148 |
| 178 _getPortInternal() native "isolate_getPortInternal"; | 149 _getPortInternal() native "isolate_getPortInternal"; |
| 179 | 150 |
| 180 ReceivePort _portInternal; | 151 typedef _MainFunction(); |
| 152 typedef _MainFunctionArgs(args); | |
| 153 typedef _MainFunctionArgsMessage(args, message); | |
| 181 | 154 |
| 182 patch class _Isolate { | 155 /** |
| 183 /* patch */ static ReceivePort get port { | 156 * Takes the real entry point as argument and invokes it with the initial |
| 184 if (_portInternal == null) { | 157 * message. |
| 185 _portInternal = _getPortInternal(); | 158 * |
| 159 * The initial message is (currently) received through the global port variable. | |
| 160 */ | |
| 161 void _startIsolate(Function entryPoint, bool isFunctionSpawn) { | |
| 162 Isolate._port.first.then((message) { | |
|
Ivan Posva
2013/10/25 07:01:46
You cannot use first here. As far as I understand
floitsch
2013/10/25 13:11:01
As discussed: closing the port for now.
| |
| 163 var reply = message[0]; | |
|
Ivan Posva
2013/10/25 07:01:46
Please assign the message[0] and message[1] values
floitsch
2013/10/25 13:11:01
Done.
| |
| 164 // TODO(floitsch): don't send ok-message if we can't find the entry point. | |
| 165 reply.send("started"); | |
| 166 if (isFunctionSpawn) { | |
| 167 entryPoint(message[1]); | |
| 168 } else if (entryPoint is _MainFunctionArgsMessage) { | |
|
Ivan Posva
2013/10/25 07:01:46
This will not mix the if-statement for spawnFuncti
floitsch
2013/10/25 13:11:01
Done.
| |
| 169 entryPoint(message[1], message[2]); | |
|
Ivan Posva
2013/10/25 07:01:46
You are not checking whether the message even has
floitsch
2013/10/25 13:11:01
It would throw otherwise.
I have added the assert
| |
| 170 } else if (entryPoint is _MainFunctionArgs) { | |
| 171 entryPoint(message[1]); | |
| 172 } else { | |
| 173 entryPoint(); | |
| 186 } | 174 } |
| 187 return _portInternal; | 175 }); |
| 176 } | |
| 177 | |
| 178 patch class Isolate { | |
| 179 /* patch */ static Future<Isolate> spawn( | |
| 180 void entryPoint(message), var message) { | |
| 181 Completer completer = new Completer<Isolate>.sync(); | |
| 182 try { | |
| 183 // The VM will invoke [_startIsolate] with entryPoint as argument. | |
| 184 SendPort controlPort = _spawnFunction(entryPoint); | |
| 185 RawReceivePort readyPort = new RawReceivePort(); | |
| 186 controlPort.send([readyPort.sendPort, message]); | |
| 187 readyPort.handler = (_) { | |
|
Ivan Posva
2013/10/25 07:01:46
... = (readyMsg) {
assert(readyMsg == "started")
floitsch
2013/10/25 13:11:01
Done.
| |
| 188 readyPort.close(); | |
| 189 completer.complete(new Isolate._fromControlPort(controlPort)); | |
|
Ivan Posva
2013/10/25 07:01:46
See note above. By the time you get here the contr
floitsch
2013/10/25 13:11:01
Agreed.
As discussed. Leaving as is. Currently nob
| |
| 190 }; | |
| 191 } catch(e, st) { | |
| 192 // TODO(floitsch): we want errors to go into the returned future. | |
| 193 rethrow; | |
| 194 }; | |
| 195 return completer.future; | |
| 188 } | 196 } |
| 189 | 197 |
| 190 /* patch */ static SendPort spawnFunction(void topLevelFunction(), | 198 /* patch */ static Future<Isolate> spawnUri( |
| 191 [bool unhandledExceptionCallback(IsolateUnhandledException e)]) | 199 Uri uri, List<String> args, var message) { |
| 200 Completer completer = new Completer<Isolate>.sync(); | |
| 201 try { | |
| 202 // The VM will invoke [_startIsolate] and not `main`. | |
| 203 SendPort controlPort = _spawnUri(uri.path); | |
| 204 RawReceivePort readyPort = new RawReceivePort(); | |
| 205 controlPort.send([readyPort.sendPort, args, message]); | |
| 206 readyPort.handler = (_) { | |
|
Ivan Posva
2013/10/25 07:01:46
ditto
floitsch
2013/10/25 13:11:01
ditto.
| |
| 207 readyPort.close(); | |
| 208 completer.complete(new Isolate._fromControlPort(controlPort)); | |
| 209 }; | |
| 210 } catch(e, st) { | |
| 211 // TODO(floitsch): we want errors to go into the returned future. | |
| 212 rethrow; | |
| 213 }; | |
| 214 return completer.future; | |
| 215 } | |
| 216 | |
| 217 static final ReceivePort _port = | |
| 218 new ReceivePort.fromRawReceivePort(_getPortInternal()); | |
| 219 | |
| 220 static SendPort _spawnFunction(Function topLevelFunction) | |
| 192 native "isolate_spawnFunction"; | 221 native "isolate_spawnFunction"; |
| 193 | 222 |
| 194 /* patch */ static SendPort spawnUri(String uri) native "isolate_spawnUri"; | 223 static SendPort _spawnUri(String uri) native "isolate_spawnUri"; |
| 195 } | 224 } |
| OLD | NEW |