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 /** | 5 /** |
6 * Concurrent programming using _isolates_: | 6 * Concurrent programming using _isolates_: |
7 * independent workers that are similar to threads | 7 * independent workers that are similar to threads |
8 * but don't share memory, | 8 * but don't share memory, |
9 * communicating only via messages. | 9 * communicating only via messages. |
10 * | 10 * |
(...skipping 29 matching lines...) Expand all Loading... |
40 * Control port used to send control messages to the isolate. | 40 * Control port used to send control messages to the isolate. |
41 * | 41 * |
42 * This class provides helper functions that sends control messages | 42 * This class provides helper functions that sends control messages |
43 * to the control port. | 43 * to the control port. |
44 */ | 44 */ |
45 final SendPort controlPort; | 45 final SendPort controlPort; |
46 /** | 46 /** |
47 * Capability granting the ability to pause the isolate. | 47 * Capability granting the ability to pause the isolate. |
48 */ | 48 */ |
49 final Capability pauseCapability; | 49 final Capability pauseCapability; |
| 50 |
50 /** | 51 /** |
51 * Capability granting the ability to terminate the isolate. | 52 * Capability granting the ability to terminate the isolate. |
52 */ | 53 */ |
53 final Capability terminateCapability; | 54 final Capability terminateCapability; |
54 | 55 |
55 /** | 56 /** |
56 * Create a new [Isolate] object with a restricted set of capabilities. | 57 * Create a new [Isolate] object with a restricted set of capabilities. |
57 * | 58 * |
58 * The port should be a control port for an isolate, as taken from | 59 * The port should be a control port for an isolate, as taken from |
59 * another `Isolate` object. | 60 * another `Isolate` object. |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 * messages that affect normal events, including `pause`. | 292 * messages that affect normal events, including `pause`. |
292 * This can be used to wait for a another event to be processed. | 293 * This can be used to wait for a another event to be processed. |
293 */ | 294 */ |
294 void ping(SendPort responsePort, [int pingType = IMMEDIATE]) { | 295 void ping(SendPort responsePort, [int pingType = IMMEDIATE]) { |
295 var message = new List(3) | 296 var message = new List(3) |
296 ..[0] = "ping" | 297 ..[0] = "ping" |
297 ..[1] = responsePort | 298 ..[1] = responsePort |
298 ..[2] = pingType; | 299 ..[2] = pingType; |
299 controlPort.send(message); | 300 controlPort.send(message); |
300 } | 301 } |
| 302 |
| 303 /** |
| 304 * Requests that uncaught errors of the isolate are sent back to [port]. |
| 305 * |
| 306 * The errors are sent back as two elements lists. |
| 307 * The first element is a `String` representation of the error, usually |
| 308 * created by calling `toString` on the error. |
| 309 * The second element is a `String` representation of an accompanying |
| 310 * stack trace, or `null` if no stack trace was provided. |
| 311 * |
| 312 * Listening using the same port more than once does nothing. It will only |
| 313 * get each error once. |
| 314 */ |
| 315 void addErrorListener(SendPort port) { |
| 316 var message = new List(2) |
| 317 ..[0] = "getErrors" |
| 318 ..[1] = port; |
| 319 controlPort.send(message); |
| 320 } |
| 321 |
| 322 /** |
| 323 * Stop listening for uncaught errors through [port]. |
| 324 * |
| 325 * The `port` should be a port that is listening for errors through |
| 326 * [addErrorListener]. This call requests that the isolate stops sending |
| 327 * errors on the port. |
| 328 * |
| 329 * If the same port has been passed via `addErrorListener` more than once, |
| 330 * only one call to `removeErrorListener` is needed to stop it from receiving |
| 331 * errors. |
| 332 * |
| 333 * Closing the receive port at the end of the send port will not stop the |
| 334 * isolate from sending errors, they are just going to be lost. |
| 335 */ |
| 336 void removeErrorListener(SendPort port) { |
| 337 var message = new List(2) |
| 338 ..[0] = "stopErrors" |
| 339 ..[1] = port; |
| 340 controlPort.send(message); |
| 341 } |
| 342 |
| 343 /** |
| 344 * Returns a broadcast stream of uncaught errors from the isolate. |
| 345 * |
| 346 * Each error is provided as an error event on the stream. |
| 347 * |
| 348 * The actual error object and stackTraces will not necessarily |
| 349 * be the same object types as in the actual isolate, but they will |
| 350 * always have the same [Object.toString] result. |
| 351 * |
| 352 * This stream is based on [addErrorListener] and [removeErrorListener]. |
| 353 */ |
| 354 Stream get errors { |
| 355 StreamController controller; |
| 356 RawReceivePort port; |
| 357 void handleError(message) { |
| 358 String errorDescription = message[0]; |
| 359 String stackDescription = message[1]; |
| 360 var error = new RemoteError(errorDescription, stackDescription); |
| 361 controller.addError(error, error.stackTrace); |
| 362 } |
| 363 controller = new StreamController.broadcast( |
| 364 sync: true, |
| 365 onListen: () { |
| 366 port = new RawReceivePort(handleError); |
| 367 this.addErrorListener(port.sendPort); |
| 368 }, |
| 369 onCancel: () { |
| 370 this.removeErrorListener(port.sendPort); |
| 371 port.close(); |
| 372 port = null; |
| 373 }); |
| 374 return controller.stream; |
| 375 } |
301 } | 376 } |
302 | 377 |
303 /** | 378 /** |
304 * Sends messages to its [ReceivePort]s. | 379 * Sends messages to its [ReceivePort]s. |
305 * | 380 * |
306 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 381 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
307 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 382 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
308 * many [SendPort]s for the same [ReceivePort]. | 383 * many [SendPort]s for the same [ReceivePort]. |
309 * | 384 * |
310 * [SendPort]s can be transmitted to other isolates, and they preserve equality | 385 * [SendPort]s can be transmitted to other isolates, and they preserve equality |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 532 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
458 | 533 |
459 String toString() { | 534 String toString() { |
460 return 'IsolateUnhandledException: exception while handling message: ' | 535 return 'IsolateUnhandledException: exception while handling message: ' |
461 '${message} \n ' | 536 '${message} \n ' |
462 '${source.toString().replaceAll("\n", "\n ")}\n' | 537 '${source.toString().replaceAll("\n", "\n ")}\n' |
463 'original stack trace:\n ' | 538 'original stack trace:\n ' |
464 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 539 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
465 } | 540 } |
466 } | 541 } |
| 542 |
| 543 /** |
| 544 * Description of an error from another isolate. |
| 545 * |
| 546 * This error has the same `toString()` and `stackTrace.toString()` behavior |
| 547 * as the original error, but has no other features of the original error. |
| 548 */ |
| 549 class RemoteError implements Error { |
| 550 final String _description; |
| 551 final StackTrace stackTrace; |
| 552 RemoteError(String description, String stackDescription) |
| 553 : _description = description, |
| 554 stackTrace = new _RemoteStackTrace(stackDescription); |
| 555 String toString() => _description; |
| 556 } |
| 557 |
| 558 class _RemoteStackTrace implements StackTrace { |
| 559 String _trace; |
| 560 _RemoteStackTrace(this._trace); |
| 561 String toString() => _trace; |
| 562 } |
OLD | NEW |