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

Side by Side Diff: frog/lib/isolate.dart

Issue 8467034: Isolates in frog - tweaks in existing js code to make things run (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: '' Created 9 years, 1 month 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) 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 SendPortImpl implements SendPort { 5 class SendPortImpl implements SendPort {
6 6
7 const SendPortImpl(this._workerId, this._isolateId, this._receivePortId); 7 const SendPortImpl(this._workerId, this._isolateId, this._receivePortId);
8 8
9 void send(var message, [SendPort replyTo = null]) { 9 void send(var message, [SendPort replyTo = null]) {
10 // TODO(kasperl): get rid of _sendNow. 10 if (replyTo !== null && !(replyTo is SendPortImpl)) {
11 this._sendNow(message, replyTo); 11 throw "SendPort::send: Illegal replyTo type.";
Jennifer Messerly 2011/11/08 05:04:35 probably want to throw an actual exception type?
Siggi Cherem (dart-lang) 2011/11/08 17:56:42 will do - the current throw was basically copied o
12 }
13 IsolateNatives.sendMessage(_workerId, _isolateId, _receivePortId,
14 _serializeMessage(message), _serializeMessage(replyTo));
12 } 15 }
13 16
14 void _sendNow(var message, SendPort replyTo) native; 17 // TODO(sigmund): get rid of _sendNow
Jennifer Messerly 2011/11/08 05:04:35 I think you can just remove it? I didn't see any c
Siggi Cherem (dart-lang) 2011/11/08 17:56:42 Unfortunately some other code in corelib is still
18 void _sendNow(var message, replyTo) { send(message, replyTo); }
19
20 _serializeMessage(message) {
21 if (IsolateNatives.shouldSerialize) {
22 return _IsolateJsUtil._serializeObject(message);
23 } else {
24 return _IsolateJsUtil._copyObject(message);
25 }
26 }
15 27
16 ReceivePortSingleShotImpl call(var message) { 28 ReceivePortSingleShotImpl call(var message) {
17 final result = new ReceivePortSingleShotImpl(); 29 final result = new ReceivePortSingleShotImpl();
18 this.send(message, result.toSendPort()); 30 this.send(message, result.toSendPort());
19 return result; 31 return result;
20 } 32 }
21 33
22 ReceivePortSingleShotImpl _callNow(var message) { 34 ReceivePortSingleShotImpl _callNow(var message) {
23 final result = new ReceivePortSingleShotImpl(); 35 final result = new ReceivePortSingleShotImpl();
24 this._sendNow(message, result.toSendPort()); 36 send(message, result.toSendPort());
25 return result; 37 return result;
26 } 38 }
27 39
28 bool operator==(var other) { 40 bool operator==(var other) {
29 return (other is SendPortImpl) && 41 return (other is SendPortImpl) &&
30 (_workerId == other._workerId) && 42 (_workerId == other._workerId) &&
31 (_isolateId == other._isolateId) && 43 (_isolateId == other._isolateId) &&
32 (_receivePortId == other._receivePortId); 44 (_receivePortId == other._receivePortId);
33 } 45 }
34 46
(...skipping 29 matching lines...) Expand all
64 factory ReceivePort.singleShot() { 76 factory ReceivePort.singleShot() {
65 return new ReceivePortSingleShotImpl(); 77 return new ReceivePortSingleShotImpl();
66 } 78 }
67 79
68 } 80 }
69 81
70 82
71 class ReceivePortImpl implements ReceivePort { 83 class ReceivePortImpl implements ReceivePort {
72 ReceivePortImpl() 84 ReceivePortImpl()
73 : _id = _nextFreeId++ { 85 : _id = _nextFreeId++ {
74 _register(_id); 86 IsolateNatives.registerPort(_id, this);
75 } 87 }
76 88
77 void receive(void onMessage(var message, SendPort replyTo)) { 89 void receive(void onMessage(var message, SendPort replyTo)) {
78 _callback = onMessage; 90 _callback = onMessage;
79 } 91 }
80 92
81 void close() { 93 void close() {
82 _callback = null; 94 _callback = null;
83 _unregister(_id); 95 IsolateNatives.unregisterPort(_id);
84 } 96 }
85 97
86 SendPort toSendPort() { 98 SendPort toSendPort() {
87 return _toNewSendPort(); 99 return _toNewSendPort();
88 } 100 }
89 101
90 /** 102 /**
91 * Returns a fresh [SendPort]. The implementation is not allowed to cache 103 * Returns a fresh [SendPort]. The implementation is not allowed to cache
92 * existing ports. 104 * existing ports.
93 */ 105 */
94 SendPort _toNewSendPort() { 106 SendPort _toNewSendPort() {
95 return new SendPortImpl(_currentWorkerId(), _currentIsolateId(), _id); 107 return new SendPortImpl(
108 IsolateNatives._currentWorkerId(),
109 IsolateNatives._currentIsolateId(), _id);
96 } 110 }
97 111
98 int _id; 112 int _id;
99 Function _callback = null; 113 Function _callback;
100 114
101 static int _nextFreeId = 1; 115 static int _nextFreeId = 1;
102 116
103 void _register(int id) native;
104 void _unregister(int id) native;
105
106 static int _currentWorkerId() native;
107 static int _currentIsolateId() native;
108
109 static void _invokeCallback(ReceivePortImpl port, message, replyTo) native {
110 if (port._callback !== null) (port._callback)(message, replyTo);
111 }
112
113 static int _getId(ReceivePortImpl port) native { 117 static int _getId(ReceivePortImpl port) native {
114 return port._id; 118 return port._id;
115 } 119 }
120
116 static Function _getCallback(ReceivePortImpl port) native { 121 static Function _getCallback(ReceivePortImpl port) native {
117 return port._callback; 122 return port._callback;
118 } 123 }
119 } 124 }
120 125
121 126
122 class ReceivePortSingleShotImpl implements ReceivePort { 127 class ReceivePortSingleShotImpl implements ReceivePort {
123 128
124 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { } 129 ReceivePortSingleShotImpl() : _port = new ReceivePortImpl() { }
125 130
(...skipping 19 matching lines...) Expand all
145 SendPort _toNewSendPort() { 150 SendPort _toNewSendPort() {
146 return _port._toNewSendPort(); 151 return _port._toNewSendPort();
147 } 152 }
148 153
149 final ReceivePortImpl _port; 154 final ReceivePortImpl _port;
150 155
151 } 156 }
152 157
153 final String _SPAWNED_SIGNAL = "spawned"; 158 final String _SPAWNED_SIGNAL = "spawned";
154 159
155 class IsolateNatives { 160 class IsolateNatives native "IsolateNatives" {
156 static Future<SendPort> spawn(Isolate isolate, bool isLight) { 161 static Future<SendPort> spawn(Isolate isolate, bool isLight) {
157 Completer<SendPort> completer = new Completer<SendPort>(); 162 Completer<SendPort> completer = new Completer<SendPort>();
158 ReceivePort port = new ReceivePort.singleShot(); 163 ReceivePort port = new ReceivePort.singleShot();
159 port.receive((msg, SendPort replyPort) { 164 port.receive((msg, SendPort replyPort) {
160 assert(msg == _SPAWNED_SIGNAL); 165 assert(msg == _SPAWNED_SIGNAL);
161 completer.complete(replyPort); 166 completer.complete(replyPort);
162 }); 167 });
163 _spawn(isolate, isLight, port.toSendPort()); 168 _spawn(isolate, isLight, port.toSendPort());
169 if (false) {
170 // TODO(sigmund): delete this code. This is temporarily added because we
171 // are tree-shaking methods that are only reachable from js
172 _IsolateJsUtil._startIsolate(null, null);
173 _IsolateJsUtil._deserializeMessage(null);
174 _IsolateJsUtil._print(null);
175 }
164 return completer.future; 176 return completer.future;
165 } 177 }
166 178
167 static SendPort _spawn(Isolate isolate, bool light, SendPort port) native; 179 static SendPort _spawn(Isolate isolate, bool light, SendPort port) native;
168 static Function bind(Function f) native; 180
181 static bool get shouldSerialize() native;
182
183 static void sendMessage(int workerId, int isolateId, int receivePortId,
184 message, replyTo) native;
185
186 /** Registers an active receive port. */
187 static void registerPort(int id, ReceivePort port) native;
188
189 /** Unregister an inactive receive port. */
190 static void unregisterPort(int id) native;
191
192 static int _currentWorkerId() native;
193
194 static int _currentIsolateId() native;
169 } 195 }
170 196
171 197
172 class _IsolateJsUtil { 198 class _IsolateJsUtil native "_IsolateJsUtil" {
173 static void _startIsolate(Isolate isolate, SendPort replyTo) native { 199 static void _startIsolate(Isolate isolate, SendPort replyTo) native {
174 ReceivePort port = new ReceivePort(); 200 ReceivePort port = new ReceivePort();
175 replyTo.send(_SPAWNED_SIGNAL, port.toSendPort()); 201 replyTo.send(_SPAWNED_SIGNAL, port.toSendPort());
176 isolate._run(port); 202 isolate._run(port);
177 } 203 }
178 204
179 static SendPort _toSendPort(port) native {
180 return port.toSendPort();
181 }
182
183 static void _print(String msg) native { 205 static void _print(String msg) native {
184 print(msg); 206 print(msg);
185 } 207 }
186 208
187 static _copyObject(obj) native { 209 static _copyObject(obj) native {
188 return new Copier().traverse(obj); 210 return new Copier().traverse(obj);
189 } 211 }
190 212
191 static _serializeObject(obj) native { 213 static _serializeObject(obj) native {
192 return new Serializer().traverse(obj); 214 return new Serializer().traverse(obj);
193 } 215 }
194 216
195 static _deserializeMessage(message) native { 217 static _deserializeMessage(message) native {
196 return new Deserializer().deserialize(message); 218 return new Deserializer().deserialize(message);
197 } 219 }
198 } 220 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698