| 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 /** Common functionality to all send ports. */ | 5 /** Common functionality to all send ports. */ |
| 6 class _BaseSendPort implements SendPort { | 6 class _BaseSendPort implements SendPort { |
| 7 /** Id for the destination isolate. */ | 7 /** Id for the destination isolate. */ |
| 8 final int _isolateId; | 8 final int _isolateId; |
| 9 | 9 |
| 10 _BaseSendPort(this._isolateId); | 10 _BaseSendPort(this._isolateId); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 /** A send port that delivers messages in-memory via native JavaScript calls. */ | 47 /** A send port that delivers messages in-memory via native JavaScript calls. */ |
| 48 class _NativeJsSendPort extends _BaseSendPort implements SendPort { | 48 class _NativeJsSendPort extends _BaseSendPort implements SendPort { |
| 49 final _ReceivePortImpl _receivePort; | 49 final _ReceivePortImpl _receivePort; |
| 50 | 50 |
| 51 const _NativeJsSendPort(this._receivePort, int isolateId) : super(isolateId); | 51 const _NativeJsSendPort(this._receivePort, int isolateId) : super(isolateId); |
| 52 | 52 |
| 53 void send(var message, [SendPort replyTo = null]) { | 53 void send(var message, [SendPort replyTo = null]) { |
| 54 _waitForPendingPorts([message, replyTo], () { | 54 _waitForPendingPorts([message, replyTo], () { |
| 55 checkReplyTo(replyTo); | 55 checkReplyTo(replyTo); |
| 56 // Check that the isolate still runs and the port is still open | 56 // Check that the isolate still runs and the port is still open |
| 57 final isolate = globalState.isolates[_isolateId]; | 57 final isolate = _globalState.isolates[_isolateId]; |
| 58 if (isolate == null) return; | 58 if (isolate == null) return; |
| 59 if (_receivePort._callback == null) return; | 59 if (_receivePort._callback == null) return; |
| 60 | 60 |
| 61 // We force serialization/deserialization as a simple way to ensure | 61 // We force serialization/deserialization as a simple way to ensure |
| 62 // isolate communication restrictions are respected between isolates that | 62 // isolate communication restrictions are respected between isolates that |
| 63 // live in the same worker. [_NativeJsSendPort] delivers both messages | 63 // live in the same worker. [_NativeJsSendPort] delivers both messages |
| 64 // from the same worker and messages from other workers. In particular, | 64 // from the same worker and messages from other workers. In particular, |
| 65 // messages sent from a worker via a [_WorkerSendPort] are received at | 65 // messages sent from a worker via a [_WorkerSendPort] are received at |
| 66 // [_processWorkerMessage] and forwarded to a native port. In such cases, | 66 // [_processWorkerMessage] and forwarded to a native port. In such cases, |
| 67 // here we'll see [globalState.currentContext == null]. | 67 // here we'll see [_globalState.currentContext == null]. |
| 68 final shouldSerialize = globalState.currentContext != null | 68 final shouldSerialize = _globalState.currentContext != null |
| 69 && globalState.currentContext.id != _isolateId; | 69 && _globalState.currentContext.id != _isolateId; |
| 70 var msg = message; | 70 var msg = message; |
| 71 var reply = replyTo; | 71 var reply = replyTo; |
| 72 if (shouldSerialize) { | 72 if (shouldSerialize) { |
| 73 msg = _serializeMessage(msg); | 73 msg = _serializeMessage(msg); |
| 74 reply = _serializeMessage(reply); | 74 reply = _serializeMessage(reply); |
| 75 } | 75 } |
| 76 globalState.topEventLoop.enqueue(isolate, () { | 76 _globalState.topEventLoop.enqueue(isolate, () { |
| 77 if (_receivePort._callback != null) { | 77 if (_receivePort._callback != null) { |
| 78 if (shouldSerialize) { | 78 if (shouldSerialize) { |
| 79 msg = _deserializeMessage(msg); | 79 msg = _deserializeMessage(msg); |
| 80 reply = _deserializeMessage(reply); | 80 reply = _deserializeMessage(reply); |
| 81 } | 81 } |
| 82 _receivePort._callback(msg, reply); | 82 _receivePort._callback(msg, reply); |
| 83 } | 83 } |
| 84 }, 'receive ' + message); | 84 }, 'receive ' + message); |
| 85 }); | 85 }); |
| 86 } | 86 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 101 | 101 |
| 102 void send(var message, [SendPort replyTo = null]) { | 102 void send(var message, [SendPort replyTo = null]) { |
| 103 _waitForPendingPorts([message, replyTo], () { | 103 _waitForPendingPorts([message, replyTo], () { |
| 104 checkReplyTo(replyTo); | 104 checkReplyTo(replyTo); |
| 105 final workerMessage = _serializeMessage({ | 105 final workerMessage = _serializeMessage({ |
| 106 'command': 'message', | 106 'command': 'message', |
| 107 'port': this, | 107 'port': this, |
| 108 'msg': message, | 108 'msg': message, |
| 109 'replyTo': replyTo}); | 109 'replyTo': replyTo}); |
| 110 | 110 |
| 111 if (globalState.isWorker) { | 111 if (_globalState.isWorker) { |
| 112 // communication from one worker to another go through the main worker: | 112 // communication from one worker to another go through the main worker: |
| 113 globalState.mainWorker.postMessage(workerMessage); | 113 _globalState.mainWorker.postMessage(workerMessage); |
| 114 } else { | 114 } else { |
| 115 globalState.workers[_workerId].postMessage(workerMessage); | 115 _globalState.workers[_workerId].postMessage(workerMessage); |
| 116 } | 116 } |
| 117 }); | 117 }); |
| 118 } | 118 } |
| 119 | 119 |
| 120 bool operator ==(var other) { | 120 bool operator ==(var other) { |
| 121 return (other is _WorkerSendPort) && | 121 return (other is _WorkerSendPort) && |
| 122 (_workerId == other._workerId) && | 122 (_workerId == other._workerId) && |
| 123 (_isolateId == other._isolateId) && | 123 (_isolateId == other._isolateId) && |
| 124 (_receivePortId == other._receivePortId); | 124 (_receivePortId == other._receivePortId); |
| 125 } | 125 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 193 } |
| 194 | 194 |
| 195 /** Implementation of a multi-use [ReceivePort] on top of JavaScript. */ | 195 /** Implementation of a multi-use [ReceivePort] on top of JavaScript. */ |
| 196 class _ReceivePortImpl implements ReceivePort { | 196 class _ReceivePortImpl implements ReceivePort { |
| 197 int _id; | 197 int _id; |
| 198 Function _callback; | 198 Function _callback; |
| 199 static int _nextFreeId = 1; | 199 static int _nextFreeId = 1; |
| 200 | 200 |
| 201 _ReceivePortImpl() | 201 _ReceivePortImpl() |
| 202 : _id = _nextFreeId++ { | 202 : _id = _nextFreeId++ { |
| 203 globalState.currentContext.register(_id, this); | 203 _globalState.currentContext.register(_id, this); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void receive(void onMessage(var message, SendPort replyTo)) { | 206 void receive(void onMessage(var message, SendPort replyTo)) { |
| 207 _callback = onMessage; | 207 _callback = onMessage; |
| 208 } | 208 } |
| 209 | 209 |
| 210 void close() { | 210 void close() { |
| 211 _callback = null; | 211 _callback = null; |
| 212 globalState.currentContext.unregister(_id); | 212 _globalState.currentContext.unregister(_id); |
| 213 } | 213 } |
| 214 | 214 |
| 215 SendPort toSendPort() { | 215 SendPort toSendPort() { |
| 216 return new _NativeJsSendPort(this, globalState.currentContext.id); | 216 return new _NativeJsSendPort(this, _globalState.currentContext.id); |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 | 219 |
| 220 /** Implementation of a single-shot [ReceivePort]. */ | 220 /** Implementation of a single-shot [ReceivePort]. */ |
| 221 class _ReceivePortSingleShotImpl implements ReceivePort { | 221 class _ReceivePortSingleShotImpl implements ReceivePort { |
| 222 | 222 |
| 223 _ReceivePortSingleShotImpl() : _port = new _ReceivePortImpl() { } | 223 _ReceivePortSingleShotImpl() : _port = new _ReceivePortImpl() { } |
| 224 | 224 |
| 225 void receive(void callback(var message, SendPort replyTo)) { | 225 void receive(void callback(var message, SendPort replyTo)) { |
| 226 _port.receive((var message, SendPort replyTo) { | 226 _port.receive((var message, SendPort replyTo) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 // map.getValues().forEach(_dispatch); | 275 // map.getValues().forEach(_dispatch); |
| 276 map.getValues().forEach((e) => _dispatch(e)); | 276 map.getValues().forEach((e) => _dispatch(e)); |
| 277 } | 277 } |
| 278 | 278 |
| 279 visitBufferingSendPort(_BufferingSendPort port) { | 279 visitBufferingSendPort(_BufferingSendPort port) { |
| 280 if (port._port == null) { | 280 if (port._port == null) { |
| 281 ports.add(port._futurePort); | 281 ports.add(port._futurePort); |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 } | 284 } |
| OLD | NEW |