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