Chromium Code Reviews| 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 * |
| 11 * See also: | 11 * See also: |
| 12 * [dart:isolate - Concurrency with Isolates](https://www.dartlang.org/docs/dart -up-and-running/contents/ch03.html#ch03-dartisolate---concurrency-with-isolates) | 12 * [dart:isolate - Concurrency with Isolates](https://www.dartlang.org/docs/dart -up-and-running/contents/ch03.html#ch03-dartisolate---concurrency-with-isolates) |
| 13 * in the library tour. | 13 * in the library tour. |
| 14 */ | 14 */ |
| 15 library dart.isolate; | 15 library dart.isolate; |
| 16 | 16 |
| 17 import "dart:async"; | 17 import "dart:async"; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Thrown when an isolate cannot be created. | 20 * Thrown when an isolate cannot be created. |
| 21 */ | 21 */ |
| 22 class IsolateSpawnException implements Exception { | 22 class IsolateSpawnException implements Exception { |
| 23 // TODO(floitsch): clean up spawn exception. | 23 // TODO(floitsch): clean up spawn exception. |
| 24 const IsolateSpawnException(String this._s); | 24 const IsolateSpawnException(String this._s); |
| 25 String toString() => "IsolateSpawnException: '$_s'"; | 25 String toString() => "IsolateSpawnException: '$_s'"; |
| 26 final String _s; | 26 final String _s; |
| 27 } | 27 } |
| 28 | 28 |
| 29 /** | |
| 30 * Thrown when an isolate message is invalid. | |
| 31 */ | |
| 32 class IsolateMessageException implements Exception { | |
| 33 final String _message; | |
| 34 | |
| 35 // TODO(floitsch): give access to the message that couldn't be sent. | |
|
Lasse Reichstein Nielsen
2013/12/11 10:38:28
Ah, agree :)
| |
| 36 const IsolateMessageException(String this._message); | |
| 37 | |
| 38 String toString() => "IsolateMessageException: '$_message'"; | |
| 39 } | |
| 40 | |
| 29 class Isolate { | 41 class Isolate { |
| 30 | 42 |
| 31 final SendPort _controlPort; | 43 final SendPort _controlPort; |
| 32 | 44 |
| 33 Isolate._fromControlPort(SendPort controlPort) | 45 Isolate._fromControlPort(SendPort controlPort) |
| 34 : this._controlPort = controlPort; | 46 : this._controlPort = controlPort; |
| 35 | 47 |
| 36 /** | 48 /** |
| 37 * Creates and spawns an isolate that shares the same code as the current | 49 * Creates and spawns an isolate that shares the same code as the current |
| 38 * isolate. | 50 * isolate. |
| 39 * | 51 * |
| 40 * The argument [entryPoint] specifies the entry point of the spawned | 52 * The argument [entryPoint] specifies the entry point of the spawned |
| 41 * isolate. It must be a top-level function or a static method that | 53 * isolate. It must be a top-level function or a static method that |
| 42 * takes one argument - that is, one-parameter functions that can be | 54 * takes one argument - that is, one-parameter functions that can be |
| 43 * compile-time constant function values. | 55 * compile-time constant function values. |
| 44 * It is not allowed to pass the value of function expressions or an instance | 56 * It is not allowed to pass the value of function expressions or an instance |
| 45 * method extracted from an object. | 57 * method extracted from an object. |
| 46 * | 58 * |
| 47 * The entry-point function is invoked with the initial [message]. | 59 * The entry-point function is invoked with the initial [message]. |
| 48 * Usually the initial [message] contains a [SendPort] so | 60 * Usually the initial [message] contains a [SendPort] so |
| 49 * that the spawner and spawnee can communicate with each other. | 61 * that the spawner and spawnee can communicate with each other. |
| 50 * | 62 * |
| 51 * Returns a future that will complete with an [Isolate] instance if the | 63 * Returns a future that will complete with an [Isolate] instance if the |
| 52 * spawning succeeded. It will complete with an error otherwise. | 64 * spawning succeeded. It will complete with an error otherwise. |
| 65 * | |
| 66 * All errors are reported asynchronously through the returned future. | |
|
Lasse Reichstein Nielsen
2013/12/11 10:38:28
Not necessarily all errors.
Let's specify which ar
floitsch
2013/12/11 12:14:16
Yes.
This should be changed to list all the possib
| |
| 53 */ | 67 */ |
| 54 external static Future<Isolate> spawn(void entryPoint(message), var message); | 68 external static Future<Isolate> spawn(void entryPoint(message), var message); |
| 55 | 69 |
| 56 /** | 70 /** |
| 57 * Creates and spawns an isolate that runs the code from the library with | 71 * Creates and spawns an isolate that runs the code from the library with |
| 58 * the specified URI. | 72 * the specified URI. |
| 59 * | 73 * |
| 60 * The isolate starts executing the top-level `main` function of the library | 74 * The isolate starts executing the top-level `main` function of the library |
| 61 * with the given URI. | 75 * with the given URI. |
| 62 * | 76 * |
| 63 * The target `main` must be a subtype of one of these three signatures: | 77 * The target `main` must be a subtype of one of these three signatures: |
| 64 * | 78 * |
| 65 * * `main()` | 79 * * `main()` |
| 66 * * `main(args)` | 80 * * `main(args)` |
| 67 * * `main(args, message)` | 81 * * `main(args, message)` |
| 68 * | 82 * |
| 69 * When present, the parameter `args` is set to the provided [args] list. | 83 * When present, the parameter `args` is set to the provided [args] list. |
| 70 * When present, the parameter `message` is set to the initial [message]. | 84 * When present, the parameter `message` is set to the initial [message]. |
| 71 * | 85 * |
| 72 * Returns a future that will complete with an [Isolate] instance if the | 86 * Returns a future that will complete with an [Isolate] instance if the |
| 73 * spawning succeeded. It will complete with an error otherwise. | 87 * spawning succeeded. It will complete with an error otherwise. |
| 88 * | |
| 89 * All errors are reported asynchronously through the returned future. | |
|
Lasse Reichstein Nielsen
2013/12/11 10:38:28
Ditto.
| |
| 74 */ | 90 */ |
| 75 external static Future<Isolate> spawnUri( | 91 external static Future<Isolate> spawnUri( |
| 76 Uri uri, List<String> args, var message); | 92 Uri uri, List<String> args, var message); |
| 77 } | 93 } |
| 78 | 94 |
| 79 /** | 95 /** |
| 80 * Sends messages to its [ReceivePort]s. | 96 * Sends messages to its [ReceivePort]s. |
| 81 * | 97 * |
| 82 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 98 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
| 83 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 99 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 248 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
| 233 | 249 |
| 234 String toString() { | 250 String toString() { |
| 235 return 'IsolateUnhandledException: exception while handling message: ' | 251 return 'IsolateUnhandledException: exception while handling message: ' |
| 236 '${message} \n ' | 252 '${message} \n ' |
| 237 '${source.toString().replaceAll("\n", "\n ")}\n' | 253 '${source.toString().replaceAll("\n", "\n ")}\n' |
| 238 'original stack trace:\n ' | 254 'original stack trace:\n ' |
| 239 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 255 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
| 240 } | 256 } |
| 241 } | 257 } |
| OLD | NEW |