Chromium Code Reviews| 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++; | |
| 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() { | |
|
Anton Muhin
2011/11/23 19:28:14
nit: I'd write
static int _getNumLivePorts() => _
turnidge
2011/11/23 21:45:37
Done.
| |
| 49 return (_numLivePorts === null ? 0 : _numLivePorts); | |
| 50 } | |
| 51 | |
| 45 // Called from the VM to dispatch to the handler. | 52 // Called from the VM to dispatch to the handler. |
| 46 static void handleMessage_(int id, int replyId, var message) { | 53 static void _handleMessage(int id, int replyId, var message) { |
| 47 assert(_portMap !== null); | 54 assert(_portMap !== null); |
| 48 ReceivePort port = _portMap[id]; | 55 ReceivePort port = _portMap[id]; |
| 49 SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); | 56 SendPort replyTo = (replyId == 0) ? null : new SendPortImpl(replyId); |
| 50 (port._onMessage)(message, replyTo); | 57 (port._onMessage)(message, replyTo); |
| 51 } | 58 } |
| 52 | 59 |
| 53 // Call into the VM to close the VM maintained mappings. | 60 // Call into the VM to close the VM maintained mappings. |
| 54 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 61 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; |
| 55 | 62 |
| 56 final int _id; | 63 final int _id; |
| 57 var _onMessage; | 64 var _onMessage; |
| 58 | 65 |
| 59 // id to ReceivePort mapping. | 66 // id to ReceivePort mapping. |
| 60 static Map _portMap; | 67 static Map _portMap; |
| 68 | |
| 69 // The number of ReceivePorts which have called receive. | |
| 70 static int _numLivePorts; | |
| 61 } | 71 } |
| 62 | 72 |
| 63 | 73 |
| 64 class ReceivePortSingleShotImpl implements ReceivePort { | 74 class ReceivePortSingleShotImpl implements ReceivePort { |
| 65 | 75 |
| 66 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } | 76 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } |
| 67 | 77 |
| 68 void receive(void callback(var message, SendPort replyTo)) { | 78 void receive(void callback(var message, SendPort replyTo)) { |
| 69 _port.receive((var message, SendPort replyTo) { | 79 _port.receive((var message, SendPort replyTo) { |
| 70 _port.close(); | 80 _port.close(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 return (other is SendPortImpl) && _id == other._id; | 122 return (other is SendPortImpl) && _id == other._id; |
| 113 } | 123 } |
| 114 | 124 |
| 115 int hashCode() { | 125 int hashCode() { |
| 116 return _id; | 126 return _id; |
| 117 } | 127 } |
| 118 | 128 |
| 119 /*--- private implementation ---*/ | 129 /*--- private implementation ---*/ |
| 120 const SendPortImpl(int id) : _id = id; | 130 const SendPortImpl(int id) : _id = id; |
| 121 | 131 |
| 122 // SendPortImpl.create_ is called from the VM when a new SendPort instance is | 132 // SendPortImpl._create is called from the VM when a new SendPort instance is |
| 123 // needed by the VM code. | 133 // needed by the VM code. |
| 124 static SendPort create_(int id) { | 134 static SendPort _create(int id) { |
| 125 return new SendPortImpl(id); | 135 return new SendPortImpl(id); |
| 126 } | 136 } |
| 127 | 137 |
| 128 // Forward the implementation of sending messages to the VM. Only port ids | 138 // Forward the implementation of sending messages to the VM. Only port ids |
| 129 // are being handed to the VM. | 139 // are being handed to the VM. |
| 130 static _sendInternal(int sendId, int replyId, var message) | 140 static _sendInternal(int sendId, int replyId, var message) |
| 131 native "SendPortImpl_sendInternal_"; | 141 native "SendPortImpl_sendInternal_"; |
| 132 | 142 |
| 133 final int _id; | 143 final int _id; |
| 134 } | 144 } |
| 135 | 145 |
| 136 | 146 |
| 137 class IsolateNatives { | 147 class IsolateNatives { |
| 138 static Future<SendPort> spawn(Isolate isolate, bool isLight) { | 148 static Future<SendPort> spawn(Isolate isolate, bool isLight) { |
| 139 Completer<SendPort> completer = new Completer<SendPort>(); | 149 Completer<SendPort> completer = new Completer<SendPort>(); |
| 140 SendPort port = _start(isolate, isLight); | 150 SendPort port = _start(isolate, isLight); |
| 141 completer.complete(port); | 151 completer.complete(port); |
| 142 return completer.future; | 152 return completer.future; |
| 143 } | 153 } |
| 144 | 154 |
| 145 // Starts a new isolate calling the run method on a new instance of the | 155 // Starts a new isolate calling the run method on a new instance of the |
| 146 // remote class's type. | 156 // remote class's type. |
| 147 // Returns the send port which is passed to the newly created isolate. | 157 // 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. | 158 // This method is being dispatched to from the public core library code. |
| 149 static SendPort _start(Isolate isolate, bool light) | 159 static SendPort _start(Isolate isolate, bool light) |
| 150 native "IsolateNatives_start"; | 160 native "IsolateNatives_start"; |
| 151 } | 161 } |
| OLD | NEW |