| 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 /** |
| 6 * Concurrent programming using _isolates_: |
| 7 * independent workers that are similar to threads |
| 8 * but don't share memory, |
| 9 * communicating only via messages. |
| 10 * |
| 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) |
| 13 * in the library tour. |
| 14 */ |
| 5 library dart.isolate; | 15 library dart.isolate; |
| 6 | 16 |
| 7 import "dart:async"; | 17 import "dart:async"; |
| 8 | 18 |
| 9 part "isolate_stream.dart"; | 19 part "isolate_stream.dart"; |
| 10 | 20 |
| 11 class IsolateSpawnException implements Exception { | 21 class IsolateSpawnException implements Exception { |
| 12 const IsolateSpawnException(String this._s); | 22 const IsolateSpawnException(String this._s); |
| 13 String toString() => "IsolateSpawnException: '$_s'"; | 23 String toString() => "IsolateSpawnException: '$_s'"; |
| 14 final String _s; | 24 final String _s; |
| 15 } | 25 } |
| 16 | 26 |
| 17 /** | 27 /** |
| 18 * The initial [ReceivePort] available by default for this isolate. This | 28 * The initial ReceivePort available by default for this isolate. |
| 19 * [ReceivePort] is created automatically and it is commonly used to establish | 29 * |
| 20 * the first communication between isolates (see [spawnFunction] and | 30 * This ReceivePort is created automatically |
| 21 * [spawnUri]). | 31 * and is commonly used to establish |
| 32 * the first communication between isolates. |
| 33 * (See [spawnFunction] and [spawnUri].) |
| 22 */ | 34 */ |
| 23 ReceivePort get port => _Isolate.port; | 35 ReceivePort get port => _Isolate.port; |
| 24 | 36 |
| 25 /** | 37 /** |
| 26 * Creates and spawns an isolate that shares the same code as the current | 38 * Creates and spawns an isolate |
| 27 * isolate, but that starts from [topLevelFunction]. The [topLevelFunction] | 39 * that shares the same code as the current isolate, |
| 28 * argument must be a static top-level function or a static method that takes no | 40 * but that starts from the specified function. |
| 41 * |
| 42 * The [topLevelFunction] argument must be |
| 43 * a static top-level function or a static method that takes no |
| 29 * arguments. It is illegal to pass a function closure. | 44 * arguments. It is illegal to pass a function closure. |
| 30 * | 45 * |
| 31 * When any isolate starts (even the main script of the application), a default | 46 * When any isolate starts (even the main script of the application), a default |
| 32 * [ReceivePort] is created for it. This port is available from the top-level | 47 * [ReceivePort] is created for it. This port is available from the top-level |
| 33 * getter [port] defined in this library. | 48 * getter [port] defined in this library. |
| 34 * | 49 * |
| 35 * [spawnFunction] returns a [SendPort] derived from the child isolate's default | 50 * This function returns a [SendPort] derived from |
| 36 * port. | 51 * the child isolate's default port. |
| 37 * | 52 * |
| 38 * The optional [unhandledExceptionCallback] argument is invoked whenever an | 53 * The optional [unhandledExceptionCallback] argument is invoked whenever an |
| 39 * exception inside the isolate is unhandled. It can be seen as a big | 54 * exception inside the isolate is unhandled. It can be seen as a big |
| 40 * `try/catch` around everything that is executed inside the isolate. The | 55 * `try/catch` around everything that is executed inside the isolate. The |
| 41 * callback should return `true` when it was able to handled the exception. | 56 * callback should return `true` if it was able to handle the exception. |
| 42 */ | 57 */ |
| 43 SendPort spawnFunction(void topLevelFunction(), | 58 SendPort spawnFunction(void topLevelFunction(), |
| 44 [bool unhandledExceptionCallback(IsolateUnhandledException e)]) | 59 [bool unhandledExceptionCallback(IsolateUnhandledException e)]) |
| 45 => _Isolate.spawnFunction(topLevelFunction, unhandledExceptionCallback); | 60 => _Isolate.spawnFunction(topLevelFunction, unhandledExceptionCallback); |
| 46 | 61 |
| 47 /** | 62 /** |
| 48 * Creates and spawns an isolate whose code is available at [uri]. Like with | 63 * Creates and spawns an isolate that runs the code from the specified URI. |
| 49 * [spawnFunction], the child isolate will have a default [ReceivePort], and a | 64 * |
| 50 * this function returns a [SendPort] derived from it. | 65 * As with [spawnFunction], |
| 66 * the child isolate has a default [ReceivePort], |
| 67 * and this function returns a [SendPort] derived from it. |
| 51 */ | 68 */ |
| 52 SendPort spawnUri(String uri) => _Isolate.spawnUri(uri); | 69 SendPort spawnUri(String uri) => _Isolate.spawnUri(uri); |
| 53 | 70 |
| 54 /** | 71 /** |
| 72 * Together with [ReceivePort], |
| 73 * the only means of communication between isolates. |
| 74 * |
| 55 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 75 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
| 56 * a [SendPort] is delivered to its respective [ReceivePort]. There might be | 76 * a [SendPort] is delivered to its respective [ReceivePort]. There might be |
| 57 * many [SendPort]s for the same [ReceivePort]. | 77 * many [SendPort]s for the same [ReceivePort]. |
| 58 * | 78 * |
| 59 * [SendPort]s can be transmitted to other isolates. | 79 * [SendPort]s can be transmitted to other isolates. |
| 60 */ | 80 */ |
| 61 abstract class SendPort { | 81 abstract class SendPort { |
| 62 | 82 |
| 63 /** | 83 /** |
| 64 * Sends an asynchronous [message] to this send port. The message is copied to | 84 * Sends an asynchronous [message] to this send port. The message is copied to |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 118 |
| 99 /** | 119 /** |
| 100 * Returns an immutable hash code for this send port that is | 120 * Returns an immutable hash code for this send port that is |
| 101 * consistent with the == operator. | 121 * consistent with the == operator. |
| 102 */ | 122 */ |
| 103 int get hashCode; | 123 int get hashCode; |
| 104 | 124 |
| 105 } | 125 } |
| 106 | 126 |
| 107 /** | 127 /** |
| 108 * [ReceivePort]s, together with [SendPort]s, are the only means of | 128 * Together with [SendPort], the only means of |
| 109 * communication between isolates. [ReceivePort]s have a [:toSendPort:] method | 129 * communication between isolates. |
| 130 * |
| 131 * [ReceivePort]s have a [:toSendPort:] method |
| 110 * which returns a [SendPort]. Any message that is sent through this [SendPort] | 132 * which returns a [SendPort]. Any message that is sent through this [SendPort] |
| 111 * is delivered to the [ReceivePort] it has been created from. There, they are | 133 * is delivered to the [ReceivePort] it has been created from. There, they are |
| 112 * dispatched to the callback that has been registered on the receive port. | 134 * dispatched to the callback that has been registered on the receive port. |
| 113 * | 135 * |
| 114 * A [ReceivePort] may have many [SendPort]s. | 136 * A [ReceivePort] may have many [SendPort]s. |
| 115 */ | 137 */ |
| 116 abstract class ReceivePort { | 138 abstract class ReceivePort { |
| 117 | 139 |
| 118 /** | 140 /** |
| 119 * Opens a long-lived port for receiving messages. The returned port | 141 * Opens a long-lived port for receiving messages. The returned port |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 const IsolateUnhandledException(this.message, this.source, this.stackTrace); | 220 const IsolateUnhandledException(this.message, this.source, this.stackTrace); |
| 199 | 221 |
| 200 String toString() { | 222 String toString() { |
| 201 return 'IsolateUnhandledException: exception while handling message: ' | 223 return 'IsolateUnhandledException: exception while handling message: ' |
| 202 '${message} \n ' | 224 '${message} \n ' |
| 203 '${source.toString().replaceAll("\n", "\n ")}\n' | 225 '${source.toString().replaceAll("\n", "\n ")}\n' |
| 204 'original stack trace:\n ' | 226 'original stack trace:\n ' |
| 205 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 227 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
| 206 } | 228 } |
| 207 } | 229 } |
| OLD | NEW |