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 import "dart:collection" show HashMap; | 5 import "dart:collection" show HashMap; |
6 | 6 |
7 patch class ReceivePort { | 7 patch class ReceivePort { |
8 /* patch */ factory ReceivePort() = _ReceivePortImpl; | 8 /* patch */ factory ReceivePort() = _ReceivePortImpl; |
9 | 9 |
10 /* patch */ factory ReceivePort.fromRawReceivePort(RawReceivePort rawPort) = | 10 /* patch */ factory ReceivePort.fromRawReceivePort(RawReceivePort rawPort) = |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 246 |
247 patch class Isolate { | 247 patch class Isolate { |
248 /* patch */ static Future<Isolate> spawn( | 248 /* patch */ static Future<Isolate> spawn( |
249 void entryPoint(message), var message, { bool paused: false }) { | 249 void entryPoint(message), var message, { bool paused: false }) { |
250 // `paused` isn't handled yet. | 250 // `paused` isn't handled yet. |
251 RawReceivePort readyPort; | 251 RawReceivePort readyPort; |
252 try { | 252 try { |
253 // The VM will invoke [_startIsolate] with entryPoint as argument. | 253 // The VM will invoke [_startIsolate] with entryPoint as argument. |
254 readyPort = new RawReceivePort(); | 254 readyPort = new RawReceivePort(); |
255 _spawnFunction(readyPort.sendPort, entryPoint, message); | 255 _spawnFunction(readyPort.sendPort, entryPoint, message); |
256 return spawnCommon(readyPort); | 256 Completer completer = new Completer<Isolate>.sync(); |
| 257 readyPort.handler = (readyMessage) { |
| 258 readyPort.close(); |
| 259 assert(readyMessage is List); |
| 260 assert(readyMessage.length == 2); |
| 261 SendPort controlPort = readyMessage[0]; |
| 262 List capabilities = readyMessage[1]; |
| 263 completer.complete(new Isolate(controlPort, |
| 264 pauseCapability: capabilities[0], |
| 265 terminateCapability: capabilities[1])); |
| 266 }; |
| 267 return completer.future; |
257 } catch (e, st) { | 268 } catch (e, st) { |
258 if (readyPort != null) { | 269 if (readyPort != null) { |
259 readyPort.close(); | 270 readyPort.close(); |
260 } | 271 } |
261 return new Future<Isolate>.error(e, st); | 272 return new Future<Isolate>.error(e, st); |
262 }; | 273 }; |
263 } | 274 } |
264 | 275 |
265 /* patch */ static Future<Isolate> spawnUri( | 276 /* patch */ static Future<Isolate> spawnUri( |
266 Uri uri, List<String> args, var message, { bool paused: false }) { | 277 Uri uri, List<String> args, var message, { bool paused: false }) { |
267 // `paused` isn't handled yet. | 278 // `paused` isn't handled yet. |
268 RawReceivePort readyPort; | 279 RawReceivePort readyPort; |
269 try { | 280 try { |
270 // The VM will invoke [_startIsolate] and not `main`. | 281 // The VM will invoke [_startIsolate] and not `main`. |
271 readyPort = new RawReceivePort(); | 282 readyPort = new RawReceivePort(); |
272 _spawnUri(readyPort.sendPort, uri.toString(), args, message); | 283 _spawnUri(readyPort.sendPort, uri.toString(), args, message); |
273 return spawnCommon(readyPort); | 284 Completer completer = new Completer<Isolate>.sync(); |
| 285 readyPort.handler = (readyMessage) { |
| 286 readyPort.close(); |
| 287 assert(readyMessage is List); |
| 288 assert(readyMessage.length == 2); |
| 289 SendPort controlPort = readyMessage[0]; |
| 290 List capabilities = readyMessage[1]; |
| 291 completer.complete(new Isolate(controlPort, |
| 292 pauseCapability: capabilities[0], |
| 293 terminateCapability: capabilities[1])); |
| 294 }; |
| 295 return completer.future; |
274 } catch (e, st) { | 296 } catch (e, st) { |
275 if (readyPort != null) { | 297 if (readyPort != null) { |
276 readyPort.close(); | 298 readyPort.close(); |
277 } | 299 } |
278 return new Future<Isolate>.error(e, st); | 300 return new Future<Isolate>.error(e, st); |
279 }; | 301 }; |
280 } | |
281 | |
282 static Future<Isolate> spawnCommon(RawReceivePort readyPort) { | |
283 Completer completer = new Completer<Isolate>.sync(); | |
284 readyPort.handler = (readyMessage) { | |
285 readyPort.close(); | |
286 if (readyMessage is String) { | |
287 // We encountered an error starting the new isolate. | |
288 completer.completeError(new IsolateSpawnException( | |
289 "Unable to spawn child isolate:\n${readyMessage}")); | |
290 } else if (readyMessage is List && | |
291 readyMessage.length == 2) { | |
292 SendPort controlPort = readyMessage[0]; | |
293 List capabilities = readyMessage[1]; | |
294 completer.complete(new Isolate(controlPort, | |
295 pauseCapability: capabilities[0], | |
296 terminateCapability: capabilities[1])); | |
297 } else { | |
298 completer.completeError(new IsolateSpawnException( | |
299 "Internal error: received unexpected message $readyMessage")); | |
300 } | |
301 }; | |
302 return completer.future; | 302 return completer.future; |
303 } | 303 } |
304 | 304 |
305 // TODO(iposva): Cleanup to have only one definition. | 305 // TODO(iposva): Cleanup to have only one definition. |
306 // These values need to be kept in sync with the class IsolateMessageHandler | 306 // These values need to be kept in sync with the class IsolateMessageHandler |
307 // in vm/isolate.cc. | 307 // in vm/isolate.cc. |
308 static const _PAUSE = 1; | 308 static const _PAUSE = 1; |
309 static const _RESUME = 2; | 309 static const _RESUME = 2; |
310 | 310 |
311 static SendPort _spawnFunction(SendPort readyPort, Function topLevelFunction, | 311 static SendPort _spawnFunction(SendPort readyPort, Function topLevelFunction, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 } | 357 } |
358 | 358 |
359 /* patch */ void addErrorListener(SendPort port) { | 359 /* patch */ void addErrorListener(SendPort port) { |
360 throw new UnsupportedError("addErrorListener"); | 360 throw new UnsupportedError("addErrorListener"); |
361 } | 361 } |
362 | 362 |
363 /* patch */ void removeErrorListener(SendPort port) { | 363 /* patch */ void removeErrorListener(SendPort port) { |
364 throw new UnsupportedError("removeErrorListener"); | 364 throw new UnsupportedError("removeErrorListener"); |
365 } | 365 } |
366 } | 366 } |
OLD | NEW |