Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: runtime/lib/isolate_patch.dart

Issue 27215002: Very simple version of Isolates. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 {
6 /// This token is sent from [IsolateSink]s to [IsolateStream]s to ask them to
7 /// close themselves.
8 const _CloseToken();
9 }
10
11 patch bool _isCloseToken(var object) {
12 // TODO(floitsch): can we compare against const _CloseToken()?
13 return object is _CloseToken;
14 }
15
16 patch class MessageBox {
17 /* patch */ MessageBox.oneShot() : this._oneShot(new ReceivePort());
18 MessageBox._oneShot(ReceivePort receivePort)
19 : stream = new IsolateStream._fromOriginalReceivePortOneShot(receivePort),
20 sink = new _IsolateSink._fromPort(receivePort.toSendPort());
21
22 /* patch */ MessageBox() : this._(new ReceivePort());
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 { 5 patch class ReceivePort {
63 /* patch */ factory ReceivePort() { 6 /* patch */ factory ReceivePort() {
64 return new _ReceivePortImpl(); 7 return new _ReceivePortImpl();
65 } 8 }
66 } 9 }
67 10
68 class _ReceivePortImpl implements ReceivePort { 11 class _ReceivePortImpl extends Stream implements ReceivePort {
69 factory _ReceivePortImpl() native "ReceivePortImpl_factory"; 12 factory _ReceivePortImpl() native "ReceivePortImpl_factory";
70 13
71 receive(void onMessage(var message, SendPort replyTo)) {
72 _onMessage = onMessage;
73 }
74
75 close() { 14 close() {
76 _portMap.remove(_id); 15 _portMap.remove(_id);
77 _closeInternal(_id); 16 _closeInternal(_id);
17 _controller.close();
78 } 18 }
79 19
80 SendPort toSendPort() { 20 SendPort get sendPort {
81 return new _SendPortImpl(_id); 21 return new _SendPortImpl(_id);
82 } 22 }
83 23
24 StreamSubscription listen(void onData(var message),
25 { Function onError,
26 void onDone(),
27 bool cancelOnError }) {
28 return _controller.stream.listen(onData);
29 }
30
84 /**** Internal implementation details ****/ 31 /**** Internal implementation details ****/
85 // Called from the VM to create a new ReceivePort instance. 32 // Called from the VM to create a new ReceivePort instance.
86 static _ReceivePortImpl _get_or_create(int id) { 33 static _ReceivePortImpl _get_or_create(int id) {
87 if (_portMap != null) { 34 if (_portMap != null) {
88 _ReceivePortImpl port = _portMap[id]; 35 _ReceivePortImpl port = _portMap[id];
89 if (port != null) { 36 if (port != null) {
90 return port; 37 return port;
91 } 38 }
92 } 39 }
93 return new _ReceivePortImpl._internal(id); 40 return new _ReceivePortImpl._internal(id);
94 } 41 }
95 42
96 _ReceivePortImpl._internal(int id) : _id = id { 43 _ReceivePortImpl._internal(int id) : _id = id {
97 if (_portMap == null) { 44 if (_portMap == null) {
98 _portMap = new Map(); 45 _portMap = new Map();
99 } 46 }
100 _portMap[id] = this; 47 _portMap[id] = this;
48
49 // TODO(floitsch): remove the hack to close receive-ports on cancel.
50 _controller = new StreamController(onCancel: close, sync: true);
101 } 51 }
102 52
103 // Called from the VM to retrieve the ReceivePort for a message. 53 // Called from the VM to retrieve the ReceivePort for a message.
104 static _ReceivePortImpl _lookupReceivePort(int id) { 54 static _ReceivePortImpl _lookupReceivePort(int id) {
105 assert(_portMap != null); 55 assert(_portMap != null);
106 return _portMap[id]; 56 return _portMap[id];
107 } 57 }
108 58
109 // Called from the VM to dispatch to the handler. 59 // Called from the VM to dispatch to the handler.
110 static void _handleMessage(_ReceivePortImpl port, int replyId, var message) { 60 static void _handleMessage(_ReceivePortImpl port, int replyId, var message) {
111 assert(port != null); 61 assert(port != null);
112 SendPort replyTo = (replyId == 0) ? null : new _SendPortImpl(replyId); 62 port._controller.add(message);
113 (port._onMessage)(message, replyTo);
114 } 63 }
115 64
116 // Call into the VM to close the VM maintained mappings. 65 // Call into the VM to close the VM maintained mappings.
117 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; 66 static _closeInternal(int id) native "ReceivePortImpl_closeInternal";
118 67
119 final int _id; 68 final int _id;
120 var _onMessage; 69 StreamController _controller;
121 70
122 // id to ReceivePort mapping. 71 // id to ReceivePort mapping.
123 static Map _portMap; 72 static Map _portMap;
124 } 73 }
125 74
126 75
127 class _SendPortImpl implements SendPort { 76 class _SendPortImpl implements SendPort {
128 /*--- public interface ---*/ 77 /*--- public interface ---*/
129 void send(var message, [SendPort replyTo = null]) { 78 void send(var message, [SendPort replyTo = null]) {
130 this._sendNow(message, replyTo); 79 this._sendNow(message, replyTo);
131 } 80 }
132 81
133 void _sendNow(var message, SendPort replyTo) { 82 void _sendNow(var message, SendPort replyTo) {
134 int replyId = (replyTo == null) ? 0 : replyTo._id; 83 int replyId = (replyTo == null) ? 0 : replyTo._id;
135 _sendInternal(_id, replyId, message); 84 _sendInternal(_id, replyId, message);
136 } 85 }
137 86
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) { 87 bool operator==(var other) {
154 return (other is _SendPortImpl) && _id == other._id; 88 return (other is _SendPortImpl) && _id == other._id;
155 } 89 }
156 90
157 int get hashCode { 91 int get hashCode {
158 return _id; 92 return _id;
159 } 93 }
160 94
161 /*--- private implementation ---*/ 95 /*--- private implementation ---*/
162 const _SendPortImpl(int id) : _id = id; 96 const _SendPortImpl(int id) : _id = id;
163 97
164 // _SendPortImpl._create is called from the VM when a new SendPort instance is 98 // _SendPortImpl._create is called from the VM when a new SendPort instance is
165 // needed by the VM code. 99 // needed by the VM code.
166 static SendPort _create(int id) { 100 static SendPort _create(int id) {
167 return new _SendPortImpl(id); 101 return new _SendPortImpl(id);
168 } 102 }
169 103
170 // Forward the implementation of sending messages to the VM. Only port ids 104 // Forward the implementation of sending messages to the VM. Only port ids
171 // are being handed to the VM. 105 // are being handed to the VM.
172 static _sendInternal(int sendId, int replyId, var message) 106 static _sendInternal(int sendId, int replyId, var message)
173 native "SendPortImpl_sendInternal_"; 107 native "SendPortImpl_sendInternal_";
174 108
175 final int _id; 109 final int _id;
176 } 110 }
177 111
178 _getPortInternal() native "isolate_getPortInternal"; 112 _getPortInternal() native "isolate_getPortInternal";
179 113
180 ReceivePort _portInternal; 114 ReceivePort _portInternal;
181 115
116 /**
117 * Takes the real entry point as argument and invokes it with the initial
118 * message.
119 *
120 * The initial message is (currently) received through the global port variable.
121 */
122 void _startIsolate(void entryPoint(message)) {
123 _Isolate.port.first.then((message) {
124 var initialMessage = message[0];
125 var reply = message[1];
126 reply.send("started");
127 entryPoint(initialMessage);
128 });
129 }
130
131 patch class Isolate {
132 /* patch */ static Future<Isolate> spawn(
133 void entryPoint(message), var message) {
134 return new Future<Isolate>.sync(() {
135 // The VM will invoke [_startIsolate] with entryPoint as argument.
136 SendPort controlPort = _Isolate._spawnFunction(entryPoint);
137 ReceivePort readyPort = new ReceivePort();
138 controlPort.send([message, readyPort.sendPort]);
139 Completer completer = new Completer<Isolate>();
140 readyPort.first.then((_) {
141 completer.complete(new Isolate._fromControlPort(controlPort));
142 });
143 return completer.future;
144 });
145 }
146
147 /* patch */ static Future<Isolate> spawnUri(Uri uri, var message) {
148 throw new UnimplementedError("Isolate.spawnUri");
149 }
150 }
151
182 patch class _Isolate { 152 patch class _Isolate {
183 /* patch */ static ReceivePort get port { 153 /* patch */ static ReceivePort get port {
184 if (_portInternal == null) { 154 if (_portInternal == null) {
185 _portInternal = _getPortInternal(); 155 _portInternal = _getPortInternal();
186 } 156 }
187 return _portInternal; 157 return _portInternal;
188 } 158 }
189 159
190 /* patch */ static SendPort spawnFunction(void topLevelFunction(), 160 static SendPort _spawnFunction(Function topLevelFunction)
191 [bool unhandledExceptionCallback(IsolateUnhandledException e)])
192 native "isolate_spawnFunction"; 161 native "isolate_spawnFunction";
193 162
194 /* patch */ static SendPort spawnUri(String uri) native "isolate_spawnUri"; 163 /* patch */ static SendPort spawnUri(String uri) native "isolate_spawnUri";
195 } 164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698