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 import "dart:collection" show HashMap; | |
19 | 18 |
20 /** | 19 /** |
21 * Thrown when an isolate cannot be created. | 20 * Thrown when an isolate cannot be created. |
22 */ | 21 */ |
23 class IsolateSpawnException implements Exception { | 22 class IsolateSpawnException implements Exception { |
24 // TODO(floitsch): clean up spawn exception. | 23 // TODO(floitsch): clean up spawn exception. |
25 const IsolateSpawnException(String this._s); | 24 const IsolateSpawnException(String this._s); |
26 String toString() => "IsolateSpawnException: '$_s'"; | 25 String toString() => "IsolateSpawnException: '$_s'"; |
27 final String _s; | 26 final String _s; |
28 } | 27 } |
29 | 28 |
30 class Isolate { | 29 class Isolate { |
31 | 30 |
32 final SendPort _controlPort; | 31 final SendPort _controlPort; |
33 | 32 |
34 Isolate._fromControlPort(SendPort controlPort) | 33 Isolate._fromControlPort(SendPort controlPort) |
35 : this._controlPort = controlPort; | 34 : this._controlPort = controlPort; |
36 | 35 |
37 /** | 36 /** |
38 * Creates and spawns an isolate that shares the same code as the current | 37 * Creates and spawns an isolate that shares the same code as the current |
39 * isolate. | 38 * isolate. |
40 * | 39 * |
41 * The argument [entryPoint] specifies the entry point of the spawned | 40 * The argument [entryPoint] specifies the entry point of the spawned |
42 * isolate. It must be a static top-level function or a static method that | 41 * isolate. It must be a top-level function or a static method that |
43 * takes no arguments. It is not allowed to pass a function closure. | 42 * takes one argument - that is, those that can be compile-time constant |
| 43 * function values. |
| 44 * It is not allowed to pass the value of function expressions or an instance |
| 45 * method extracted from an object. |
44 * | 46 * |
45 * The entry-point function is invoked with the initial [message]. | 47 * The entry-point function is invoked with the initial [message]. |
46 * Usually the initial [message] contains a [SendPort] so | 48 * Usually the initial [message] contains a [SendPort] so |
47 * that the spawner and spawnee can communicate with each other. | 49 * that the spawner and spawnee can communicate with each other. |
48 * | 50 * |
49 * Returns a future that will complete with an [Isolate] instance. The | 51 * Returns a future that will complete with an [Isolate] instance. The |
50 * isolate instance can be used to control the spawned isolate. | 52 * isolate instance can be used to control the spawned isolate. |
51 */ | 53 */ |
52 external static Future<Isolate> spawn(void entryPoint(message), var message); | 54 external static Future<Isolate> spawn(void entryPoint(message), var message); |
53 | 55 |
54 /** | 56 /** |
55 * Creates and spawns an isolate that runs the code from the library with | 57 * Creates and spawns an isolate that runs the code from the library with |
56 * the specified URI. | 58 * the specified URI. |
57 * | 59 * |
58 * The isolate starts executing the top-level `main` function of the library | 60 * The isolate starts executing the top-level `main` function of the library |
59 * with the given URI. | 61 * with the given URI. |
60 * | 62 * |
61 * The target `main` may have one of the four following signatures: | 63 * The target `main` may have one of three signatures: |
62 * | 64 * |
63 * * `main()` | 65 * * `main()` |
64 * * `main(args)` | 66 * * `main(args)` |
65 * * `main(args, message)` | 67 * * `main(args, message)` |
66 * | 68 * |
67 * When present, the argument `message` is set to the initial [message]. | 69 * When present, the parameter `args` is set to the provided [args] list. |
68 * When present, the argument `args` is set to the provided [args] list. | 70 * When present, the parameter `message` is set to the initial [message]. |
69 * | 71 * |
70 * Returns a future that will complete with an [Isolate] instance. The | 72 * Returns a future that will complete with an [Isolate] instance. The |
71 * isolate instance can be used to control the spawned isolate. | 73 * isolate instance can be used to control the spawned isolate. |
72 */ | 74 */ |
73 external static Future<Isolate> spawnUri( | 75 external static Future<Isolate> spawnUri( |
74 Uri uri, List<String> args, var message); | 76 Uri uri, List<String> args, var message); |
75 } | 77 } |
76 | 78 |
77 /** | 79 /** |
78 * Sends messages to its [ReceivePort]s. | 80 * Sends messages to its [ReceivePort]s. |
79 * | 81 * |
80 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 82 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
81 * a [SendPort] is delivered to its respective [ReceivePort]. There might be | 83 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
82 * many [SendPort]s for the same [ReceivePort]. | 84 * many [SendPort]s for the same [ReceivePort]. |
83 * | 85 * |
84 * [SendPort]s can be transmitted to other isolates. | 86 * [SendPort]s can be transmitted to other isolates. |
85 */ | 87 */ |
86 abstract class SendPort { | 88 abstract class SendPort { |
87 | 89 |
88 /** | 90 /** |
89 * Sends an asynchronous [message] to this send port. The message is copied to | 91 * Sends an asynchronous [message] through this send port, to its |
90 * the receiving isolate. | 92 * corresponding `ReceivePort`. |
91 * | 93 * |
92 * The content of [message] can be: primitive values (null, num, bool, double, | 94 * The content of [message] can be: primitive values (null, num, bool, double, |
93 * String), instances of [SendPort], and lists and maps whose elements are any | 95 * String), instances of [SendPort], and lists and maps whose elements are any |
94 * of these. List and maps are also allowed to be cyclic. | 96 * of these. List and maps are also allowed to be cyclic. |
95 * | 97 * |
96 * In the special circumstances when two isolates share the same code and are | 98 * In the special circumstances when two isolates share the same code and are |
97 * running in the same process (e.g. isolates created via [spawnFunction]), it | 99 * running in the same process (e.g. isolates created via [Isolate.spawn]), it |
98 * is also possible to send object instances (which would be copied in the | 100 * is also possible to send object instances (which would be copied in the |
99 * process). This is currently only supported by the dartvm. For now, the | 101 * process). This is currently only supported by the dartvm. For now, the |
100 * dart2js compiler only supports the restricted messages described above. | 102 * dart2js compiler only supports the restricted messages described above. |
101 */ | 103 */ |
102 void send(var message); | 104 void send(var message); |
103 | 105 |
104 /** | 106 /** |
105 * Tests whether [other] is a [SendPort] pointing to the same | 107 * Tests whether [other] is a [SendPort] pointing to the same |
106 * [ReceivePort] as this one. | 108 * [ReceivePort] as this one. |
107 */ | 109 */ |
108 bool operator==(var other); | 110 bool operator==(var other); |
109 | 111 |
110 /** | 112 /** |
111 * Returns an immutable hash code for this send port that is | 113 * Returns an immutable hash code for this send port that is |
112 * consistent with the == operator. | 114 * consistent with the == operator. |
113 */ | 115 */ |
114 int get hashCode; | 116 int get hashCode; |
115 } | 117 } |
116 | 118 |
117 /** | 119 /** |
118 * Together with [SendPort], the only means of communication between isolates. | 120 * Together with [SendPort], the only means of communication between isolates. |
119 * | 121 * |
120 * [ReceivePort]s have a `sendport` getter which returns a [SendPort]. | 122 * [ReceivePort]s have a `sendPort` getter which returns a [SendPort]. |
121 * Any message that is sent through this [SendPort] | 123 * Any message that is sent through this [SendPort] |
122 * is delivered to the [ReceivePort] it has been created from. There, the | 124 * is delivered to the [ReceivePort] it has been created from. There, the |
123 * message is dispatched to its listener. | 125 * message is dispatched to the `ReceivePort`'s listener. |
124 * | 126 * |
125 * A [ReceivePort] is a non-broadcast stream. This means that it buffers | 127 * A [ReceivePort] is a non-broadcast stream. This means that it buffers |
126 * incoming messages until a listener is registered. Only one listener can | 128 * incoming messages until a listener is registered. Only one listener can |
127 * receive messages. See [Stream.asBroadcastStream] for transforming the port | 129 * receive messages. See [Stream.asBroadcastStream] for transforming the port |
128 * to a broadcast stream. | 130 * to a broadcast stream. |
129 * | 131 * |
130 * A [ReceivePort] may have many [SendPort]s. | 132 * A [ReceivePort] may have many [SendPort]s. |
131 */ | 133 */ |
132 abstract class ReceivePort implements Stream { | 134 abstract class ReceivePort implements Stream { |
133 | 135 |
(...skipping 13 matching lines...) Expand all Loading... |
147 * Creates a [ReceivePort] from a [RawReceivePort]. | 149 * Creates a [ReceivePort] from a [RawReceivePort]. |
148 * | 150 * |
149 * The handler of the given [rawPort] is overwritten during the construction | 151 * The handler of the given [rawPort] is overwritten during the construction |
150 * of the result. | 152 * of the result. |
151 */ | 153 */ |
152 external factory ReceivePort.fromRawReceivePort(RawReceivePort rawPort); | 154 external factory ReceivePort.fromRawReceivePort(RawReceivePort rawPort); |
153 | 155 |
154 /** | 156 /** |
155 * Inherited from [Stream]. | 157 * Inherited from [Stream]. |
156 * | 158 * |
157 * Note that all named arguments are ignored since a ReceivePort will never | 159 * Note that [onError] and [cancelOnError] are ignored since a ReceivePort |
158 * receive an error, or done message. | 160 * will never receive an error. |
| 161 * |
| 162 * The [onDone] handler will be called when the stream closes. |
| 163 * The stream closes when [close] is called. |
159 */ | 164 */ |
160 StreamSubscription listen(void onData(var message), | 165 StreamSubscription listen(void onData(var message), |
161 { Function onError, | 166 { Function onError, |
162 void onDone(), | 167 void onDone(), |
163 bool cancelOnError }); | 168 bool cancelOnError }); |
164 | 169 |
165 /** | 170 /** |
166 * Closes `this`. | 171 * Closes `this`. |
167 * | 172 * |
168 * If the stream has not been canceled yet, adds a close-event to the event | 173 * If the stream has not been canceled yet, adds a close-event to the event |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 227 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
223 | 228 |
224 String toString() { | 229 String toString() { |
225 return 'IsolateUnhandledException: exception while handling message: ' | 230 return 'IsolateUnhandledException: exception while handling message: ' |
226 '${message} \n ' | 231 '${message} \n ' |
227 '${source.toString().replaceAll("\n", "\n ")}\n' | 232 '${source.toString().replaceAll("\n", "\n ")}\n' |
228 'original stack trace:\n ' | 233 'original stack trace:\n ' |
229 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 234 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
230 } | 235 } |
231 } | 236 } |
OLD | NEW |