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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 * It is not allowed to pass the value of function expressions or an instance | 137 * It is not allowed to pass the value of function expressions or an instance |
138 * method extracted from an object. | 138 * method extracted from an object. |
139 * | 139 * |
140 * The entry-point function is invoked with the initial [message]. | 140 * The entry-point function is invoked with the initial [message]. |
141 * Usually the initial [message] contains a [SendPort] so | 141 * Usually the initial [message] contains a [SendPort] so |
142 * that the spawner and spawnee can communicate with each other. | 142 * that the spawner and spawnee can communicate with each other. |
143 * | 143 * |
144 * If the [paused] parameter is set to `true`, | 144 * If the [paused] parameter is set to `true`, |
145 * the isolate will start up in a paused state, | 145 * the isolate will start up in a paused state, |
146 * as if by an initial call of `isolate.pause(isolate.pauseCapability)`. | 146 * as if by an initial call of `isolate.pause(isolate.pauseCapability)`. |
147 * This allows setting up error or exit listeners on the isolate | |
148 * before it starts running. | |
149 * To resume the isolate, call `isolate.resume(isolate.pauseCapability)`. | 147 * To resume the isolate, call `isolate.resume(isolate.pauseCapability)`. |
150 * | 148 * |
| 149 * If the [errorAreFatal], [onExit] and/or [onError] parameters are provided, |
| 150 * the isolate will act as if, respectively, [setErrorsFatal], |
| 151 * [addOnExitListener] and [addErrorListener] were called with the |
| 152 * corresponding parameter and was processed before the isolate starts |
| 153 * running. |
| 154 * |
| 155 * You can also call the [setErrorsFatal], [addOnExitListener] and |
| 156 * [addErrorListener] methods on the returned isolate, but unless the |
| 157 * isolate was started as [paused], it may already have terminated |
| 158 * before those methods can complete. |
| 159 * |
| 160 * WARNING: The [errorsAreFatal], [onExit] and [onError] parameters are not |
| 161 * implemented yet. |
| 162 * |
151 * Returns a future that will complete with an [Isolate] instance if the | 163 * Returns a future that will complete with an [Isolate] instance if the |
152 * spawning succeeded. It will complete with an error otherwise. | 164 * spawning succeeded. It will complete with an error otherwise. |
153 */ | 165 */ |
154 external static Future<Isolate> spawn(void entryPoint(message), var message, | 166 external static Future<Isolate> spawn(void entryPoint(message), var message, |
155 { bool paused: false }); | 167 { bool paused: false, |
| 168 bool errorsAreFatal, |
| 169 SendPort onExit, |
| 170 SendPort onError }); |
156 | 171 |
157 /** | 172 /** |
158 * Creates and spawns an isolate that runs the code from the library with | 173 * Creates and spawns an isolate that runs the code from the library with |
159 * the specified URI. | 174 * the specified URI. |
160 * | 175 * |
161 * The isolate starts executing the top-level `main` function of the library | 176 * The isolate starts executing the top-level `main` function of the library |
162 * with the given URI. | 177 * with the given URI. |
163 * | 178 * |
164 * The target `main` must be a subtype of one of these three signatures: | 179 * The target `main` must be callable with zero, one or two arguments. |
| 180 * Examples: |
165 * | 181 * |
166 * * `main()` | 182 * * `main()` |
167 * * `main(args)` | 183 * * `main(args)` |
168 * * `main(args, message)` | 184 * * `main(args, message)` |
169 * | 185 * |
170 * When present, the parameter `args` is set to the provided [args] list. | 186 * When present, the parameter `args` is set to the provided [args] list. |
171 * When present, the parameter `message` is set to the initial [message]. | 187 * When present, the parameter `message` is set to the initial [message]. |
172 * | 188 * |
173 * If the [paused] parameter is set to `true`, | 189 * If the [paused] parameter is set to `true`, |
174 * the isolate will start up in a paused state, | 190 * the isolate will start up in a paused state, |
175 * as if by an initial call of `isolate.pause(isolate.pauseCapability)`. | 191 * as if by an initial call of `isolate.pause(isolate.pauseCapability)`. |
176 * This allows setting up error or exit listeners on the isolate | |
177 * before it starts running. | |
178 * To resume the isolate, call `isolate.resume(isolate.pauseCapability)`. | 192 * To resume the isolate, call `isolate.resume(isolate.pauseCapability)`. |
179 * | 193 * |
| 194 * If the [errorAreFatal], [onExit] and/or [onError] parameters are provided, |
| 195 * the isolate will act as if, respectively, [setErrorsFatal], |
| 196 * [addOnExitListener] and [addErrorListener] were called with the |
| 197 * corresponding parameter and was processed before the isolate starts |
| 198 * running. |
| 199 * |
| 200 * You can also call the [setErrorsFatal], [addOnExitListener] and |
| 201 * [addErrorListener] methods on the returned isolate, but unless the |
| 202 * isolate was started as [paused], it may already have terminated |
| 203 * before those methods can complete. |
| 204 * |
| 205 * WARNING: The [errorsAreFatal], [onExit] and [onError] parameters are not |
| 206 * implemented yet. |
| 207 * |
180 * If the [checked] parameter is set to `true` or `false`, | 208 * If the [checked] parameter is set to `true` or `false`, |
181 * the new isolate will run code in checked mode, | 209 * the new isolate will run code in checked mode, |
182 * respectively in production mode, if possible. | 210 * respectively in production mode, if possible. |
183 * If the parameter is omitted, the new isolate will inherit the | 211 * If the parameter is omitted, the new isolate will inherit the |
184 * value from the current isolate. | 212 * value from the current isolate. |
185 * | 213 * |
186 * It may not always be possible to honor the `checked` parameter. | 214 * It may not always be possible to honor the `checked` parameter. |
187 * If the isolate code was pre-compiled, it may not be possible to change | 215 * If the isolate code was pre-compiled, it may not be possible to change |
188 * the checked mode setting dynamically. | 216 * the checked mode setting dynamically. |
189 * In that case, the `checked` parameter is ignored. | 217 * In that case, the `checked` parameter is ignored. |
(...skipping 17 matching lines...) Expand all Loading... |
207 * | 235 * |
208 * Returns a future that will complete with an [Isolate] instance if the | 236 * Returns a future that will complete with an [Isolate] instance if the |
209 * spawning succeeded. It will complete with an error otherwise. | 237 * spawning succeeded. It will complete with an error otherwise. |
210 */ | 238 */ |
211 external static Future<Isolate> spawnUri( | 239 external static Future<Isolate> spawnUri( |
212 Uri uri, | 240 Uri uri, |
213 List<String> args, | 241 List<String> args, |
214 var message, | 242 var message, |
215 {bool paused: false, | 243 {bool paused: false, |
216 bool checked, | 244 bool checked, |
217 Uri packageRoot}); | 245 Uri packageRoot, |
| 246 bool errorsAreFatal, |
| 247 SendPort onExit, |
| 248 SendPort onError}); |
218 | 249 |
219 /** | 250 /** |
220 * Requests the isolate to pause. | 251 * Requests the isolate to pause. |
221 * | 252 * |
222 * The isolate should stop handling events by pausing its event queue. | 253 * The isolate should stop handling events by pausing its event queue. |
223 * The request will eventually make the isolate stop doing anything. | 254 * The request will eventually make the isolate stop doing anything. |
224 * It will be handled before any other messages that are later sent to the | 255 * It will be handled before any other messages that are later sent to the |
225 * isolate from the current isolate, but no other guarantees are provided. | 256 * isolate from the current isolate, but no other guarantees are provided. |
226 * | 257 * |
227 * The event loop may be paused before previously sent, but not yet exeuted, | 258 * The event loop may be paused before previously sent, but not yet exeuted, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 * thing before it terminates. It will run no further code after the message | 300 * thing before it terminates. It will run no further code after the message |
270 * has been sent. | 301 * has been sent. |
271 * | 302 * |
272 * Adding the same port more than once will only cause it to receive one | 303 * Adding the same port more than once will only cause it to receive one |
273 * message, using the last response value that was added. | 304 * message, using the last response value that was added. |
274 * | 305 * |
275 * If the isolate is already dead, no message will be sent. | 306 * If the isolate is already dead, no message will be sent. |
276 * If `response` cannot be sent to the isolate, then the request is ignored. | 307 * If `response` cannot be sent to the isolate, then the request is ignored. |
277 * It is recommended to only use simple values that can be sent to all | 308 * It is recommended to only use simple values that can be sent to all |
278 * isolates, like `null`, booleans, numbers or strings. | 309 * isolates, like `null`, booleans, numbers or strings. |
279 * | 310 * |
280 * Since isolates run concurrently, it's possible for it to exit before the | 311 * Since isolates run concurrently, it's possible for it to exit before the |
281 * exit listener is established. To avoid this, start the isolate paused, | 312 * exit listener is established, and in that case no response will be |
282 * add the listener, then resume it. | 313 * sent on [responsePort]. |
| 314 * To avoid this, either use the corresponding parameter to the spawn |
| 315 * function, or start the isolate paused, add the listener and |
| 316 * then resume the isolate. |
283 */ | 317 */ |
284 /* TODO(lrn): Can we do better? Can the system recognize this message and | 318 /* TODO(lrn): Can we do better? Can the system recognize this message and |
285 * send a reply if the receiving isolate is dead? | 319 * send a reply if the receiving isolate is dead? |
286 */ | 320 */ |
287 external void addOnExitListener(SendPort responsePort, {Object response}); | 321 external void addOnExitListener(SendPort responsePort, {Object response}); |
288 | 322 |
289 /** | 323 /** |
290 * Stop listening on exit messages from the isolate. | 324 * Stop listening on exit messages from the isolate. |
291 * | 325 * |
292 * If a call has previously been made to [addOnExitListener] with the same | 326 * If a call has previously been made to [addOnExitListener] with the same |
293 * send-port, this will unregister the port, and it will no longer receive | 327 * send-port, this will unregister the port, and it will no longer receive |
294 * a message when the isolate terminates. | 328 * a message when the isolate terminates. |
295 * A response may still be sent until this operation is fully processed by | 329 * A response may still be sent until this operation is fully processed by |
296 * the isolate. | 330 * the isolate. |
297 */ | 331 */ |
298 external void removeOnExitListener(SendPort responsePort); | 332 external void removeOnExitListener(SendPort responsePort); |
299 | 333 |
300 /** | 334 /** |
301 * Set whether uncaught errors will terminate the isolate. | 335 * Set whether uncaught errors will terminate the isolate. |
302 * | 336 * |
303 * If errors are fatal, any uncaught error will terminate the isolate | 337 * If errors are fatal, any uncaught error will terminate the isolate |
304 * event loop and shut down the isolate. | 338 * event loop and shut down the isolate. |
305 * | 339 * |
306 * This call requires the [terminateCapability] for the isolate. | 340 * This call requires the [terminateCapability] for the isolate. |
307 * If the capability is not correct, no change is made. | 341 * If the capability is not correct, no change is made. |
| 342 * |
| 343 * Since isolates run concurrently, it's possible for it to exit due to an |
| 344 * error before errors are set non-fatal. |
| 345 * To avoid this, either use the corresponding parameter to the spawn |
| 346 * function, or start the isolate paused, set errors non-fatal and |
| 347 * then resume the isolate. |
308 */ | 348 */ |
309 external void setErrorsFatal(bool errorsAreFatal); | 349 external void setErrorsFatal(bool errorsAreFatal); |
310 | 350 |
311 /** | 351 /** |
312 * Requests the isolate to shut down. | 352 * Requests the isolate to shut down. |
313 * | 353 * |
314 * The isolate is requested to terminate itself. | 354 * The isolate is requested to terminate itself. |
315 * The [priority] argument specifies when this must happen. | 355 * The [priority] argument specifies when this must happen. |
316 * | 356 * |
317 * The [priority] must be one of [IMMEDIATE] or [BEFORE_NEXT_EVENT]. | 357 * The [priority] must be one of [IMMEDIATE] or [BEFORE_NEXT_EVENT]. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 * | 401 * |
362 * The errors are sent back as two elements lists. | 402 * The errors are sent back as two elements lists. |
363 * The first element is a `String` representation of the error, usually | 403 * The first element is a `String` representation of the error, usually |
364 * created by calling `toString` on the error. | 404 * created by calling `toString` on the error. |
365 * The second element is a `String` representation of an accompanying | 405 * The second element is a `String` representation of an accompanying |
366 * stack trace, or `null` if no stack trace was provided. | 406 * stack trace, or `null` if no stack trace was provided. |
367 * To convert this back to a [StackTrace] object, use [StackTrace.fromString]. | 407 * To convert this back to a [StackTrace] object, use [StackTrace.fromString]. |
368 * | 408 * |
369 * Listening using the same port more than once does nothing. It will only | 409 * Listening using the same port more than once does nothing. It will only |
370 * get each error once. | 410 * get each error once. |
371 * | 411 * |
372 * Since isolates run concurrently, it's possible for it to exit before the | 412 * Since isolates run concurrently, it's possible for it to exit before the |
373 * error listener is established. To avoid this, start the isolate paused, | 413 * error listener is established. To avoid this, start the isolate paused, |
374 * add the listener, then resume it. | 414 * add the listener and then resume the isolate. |
375 */ | 415 */ |
376 external void addErrorListener(SendPort port); | 416 external void addErrorListener(SendPort port); |
377 | 417 |
378 /** | 418 /** |
379 * Stop listening for uncaught errors through [port]. | 419 * Stop listening for uncaught errors through [port]. |
380 * | 420 * |
381 * The `port` should be a port that is listening for errors through | 421 * The `port` should be a port that is listening for errors through |
382 * [addErrorListener]. This call requests that the isolate stops sending | 422 * [addErrorListener]. This call requests that the isolate stops sending |
383 * errors on the port. | 423 * errors on the port. |
384 * | 424 * |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 * as the original error, but has no other features of the original error. | 638 * as the original error, but has no other features of the original error. |
599 */ | 639 */ |
600 class RemoteError implements Error { | 640 class RemoteError implements Error { |
601 final String _description; | 641 final String _description; |
602 final StackTrace stackTrace; | 642 final StackTrace stackTrace; |
603 RemoteError(String description, String stackDescription) | 643 RemoteError(String description, String stackDescription) |
604 : _description = description, | 644 : _description = description, |
605 stackTrace = new StackTrace.fromString(stackDescription); | 645 stackTrace = new StackTrace.fromString(stackDescription); |
606 String toString() => _description; | 646 String toString() => _description; |
607 } | 647 } |
OLD | NEW |