| 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 * To use this library in your code: | 11 * To use this library in your code: |
| 12 * | 12 * |
| 13 * import 'dart:isolate'; | 13 * import 'dart:isolate'; |
| 14 */ | 14 */ |
| 15 library dart.isolate; | 15 library dart.isolate; |
| 16 | 16 |
| 17 import "dart:async"; | 17 import "dart:async"; |
| 18 | 18 |
| 19 part "capability.dart"; | 19 part "capability.dart"; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Thrown when an isolate cannot be created. | 22 * Thrown when an isolate cannot be created. |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 * If there is no valid mapping from the package: URI in the current | 155 * If there is no valid mapping from the package: URI in the current |
| 156 * isolate, then this call returns `null`. Non-package: URIs are | 156 * isolate, then this call returns `null`. Non-package: URIs are |
| 157 * returned unmodified. | 157 * returned unmodified. |
| 158 */ | 158 */ |
| 159 external static Future<Uri> resolvePackageUri(Uri packageUri); | 159 external static Future<Uri> resolvePackageUri(Uri packageUri); |
| 160 | 160 |
| 161 /** | 161 /** |
| 162 * Creates and spawns an isolate that shares the same code as the current | 162 * Creates and spawns an isolate that shares the same code as the current |
| 163 * isolate. | 163 * isolate. |
| 164 * | 164 * |
| 165 * The argument [entryPoint] specifies the entry point of the spawned | 165 * The argument [entryPoint] specifies the initial function to call |
| 166 * isolate. It must be a top-level function or a static method that | 166 * in the spawned isolate. |
| 167 * takes one argument - that is, one-parameter functions that can be | 167 * The entry-point function is invoked in the new isolate with [message] |
| 168 * compile-time constant function values. | 168 * as the only argument. |
| 169 * It is not allowed to pass the value of function expressions or an instance | |
| 170 * method extracted from an object. | |
| 171 * | 169 * |
| 172 * The entry-point function is invoked with the initial [message]. | 170 * The function must be a top-level function or a static method |
| 171 * that can be called with a single argument, |
| 172 * that is, a compile-time constant function value |
| 173 * which accepts at least one positional parameter |
| 174 * and has at most one required positional parameter. |
| 175 * The function may accept any number of optional parameters, |
| 176 * as long as it *can* be called with just a single argument. |
| 177 * The function must not be the value of a function expression |
| 178 * or an instance method tear-off. |
| 179 * |
| 173 * Usually the initial [message] contains a [SendPort] so | 180 * Usually the initial [message] contains a [SendPort] so |
| 174 * that the spawner and spawnee can communicate with each other. | 181 * that the spawner and spawnee can communicate with each other. |
| 175 * | 182 * |
| 176 * If the [paused] parameter is set to `true`, | 183 * If the [paused] parameter is set to `true`, |
| 177 * the isolate will start up in a paused state, | 184 * the isolate will start up in a paused state, |
| 185 * just before calling the [entryPoint] function with the [message], |
| 178 * as if by an initial call of `isolate.pause(isolate.pauseCapability)`. | 186 * as if by an initial call of `isolate.pause(isolate.pauseCapability)`. |
| 179 * To resume the isolate, call `isolate.resume(isolate.pauseCapability)`. | 187 * To resume the isolate, call `isolate.resume(isolate.pauseCapability)`. |
| 180 * | 188 * |
| 181 * If the [errorAreFatal], [onExit] and/or [onError] parameters are provided, | 189 * If the [errorAreFatal], [onExit] and/or [onError] parameters are provided, |
| 182 * the isolate will act as if, respectively, [setErrorsFatal], | 190 * the isolate will act as if, respectively, [setErrorsFatal], |
| 183 * [addOnExitListener] and [addErrorListener] were called with the | 191 * [addOnExitListener] and [addErrorListener] were called with the |
| 184 * corresponding parameter and was processed before the isolate starts | 192 * corresponding parameter and was processed before the isolate starts |
| 185 * running. | 193 * running. |
| 186 * | 194 * |
| 195 * If [errorsAreFatal] is omitted, the platform may choose a default behavior |
| 196 * or inherit the current isolate's behavior. |
| 197 * |
| 187 * You can also call the [setErrorsFatal], [addOnExitListener] and | 198 * You can also call the [setErrorsFatal], [addOnExitListener] and |
| 188 * [addErrorListener] methods on the returned isolate, but unless the | 199 * [addErrorListener] methods on the returned isolate, but unless the |
| 189 * isolate was started as [paused], it may already have terminated | 200 * isolate was started as [paused], it may already have terminated |
| 190 * before those methods can complete. | 201 * before those methods can complete. |
| 191 * | 202 * |
| 192 * Returns a future that will complete with an [Isolate] instance if the | 203 * Returns a future which will complete with an [Isolate] instance if the |
| 193 * spawning succeeded. It will complete with an error otherwise. | 204 * spawning succeeded. It will complete with an error otherwise. |
| 194 */ | 205 */ |
| 195 external static Future<Isolate> spawn(void entryPoint(message), var message, | 206 external static Future<Isolate> spawn(void entryPoint(message), var message, |
| 196 { bool paused: false, | 207 {bool paused: false, |
| 197 bool errorsAreFatal, | 208 bool errorsAreFatal, |
| 198 SendPort onExit, | 209 SendPort onExit, |
| 199 SendPort onError }); | 210 SendPort onError}); |
| 200 | 211 |
| 201 /** | 212 /** |
| 202 * Creates and spawns an isolate that runs the code from the library with | 213 * Creates and spawns an isolate that runs the code from the library with |
| 203 * the specified URI. | 214 * the specified URI. |
| 204 * | 215 * |
| 205 * The isolate starts executing the top-level `main` function of the library | 216 * The isolate starts executing the top-level `main` function of the library |
| 206 * with the given URI. | 217 * with the given URI. |
| 207 * | 218 * |
| 208 * The target `main` must be callable with zero, one or two arguments. | 219 * The target `main` must be callable with zero, one or two arguments. |
| 209 * Examples: | 220 * Examples: |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 bool errorsAreFatal, | 295 bool errorsAreFatal, |
| 285 bool checked, | 296 bool checked, |
| 286 Map<String, String> environment, | 297 Map<String, String> environment, |
| 287 Uri packageRoot, | 298 Uri packageRoot, |
| 288 Uri packageConfig, | 299 Uri packageConfig, |
| 289 bool automaticPackageResolution: false}); | 300 bool automaticPackageResolution: false}); |
| 290 | 301 |
| 291 /** | 302 /** |
| 292 * Requests the isolate to pause. | 303 * Requests the isolate to pause. |
| 293 * | 304 * |
| 294 * The isolate should stop handling events by pausing its event queue. | 305 * When the isolate receives the pause command, it stops |
| 295 * The request will eventually make the isolate stop doing anything. | 306 * processing events from the event loop queue. |
| 296 * It will be handled before any other messages that are later sent to the | 307 * It may still add new events to the queue in response to, e.g., timers |
| 297 * isolate from the current isolate, but no other guarantees are provided. | 308 * or receive-port messages. When the isolate is resumed, it handles |
| 309 * the already enqueued events. |
| 298 * | 310 * |
| 299 * The event loop may be paused before previously sent, but not yet exeuted, | 311 * The pause request is sent through the isolate's command port, |
| 300 * messages have been reached. | 312 * which bypasses the receiving isolate's event loop. |
| 313 * The pause takes effect when it is received, pausing the event loop |
| 314 * as it is at that time. |
| 301 * | 315 * |
| 302 * If [resumeCapability] is provided, it is used to identity the pause, | 316 * If [resumeCapability] is provided, it is used to identity the pause, |
| 303 * and must be used again to end the pause using [resume]. | 317 * and must be used again to end the pause using [resume]. |
| 304 * Otherwise a new resume capability is created and returned. | 318 * Otherwise a new resume capability is created and returned. |
| 305 * | 319 * |
| 306 * If an isolate is paused more than once using the same capability, | 320 * If an isolate is paused more than once using the same capability, |
| 307 * only one resume with that capability is needed to end the pause. | 321 * only one resume with that capability is needed to end the pause. |
| 308 * | 322 * |
| 309 * If an isolate is paused using more than one capability, | 323 * If an isolate is paused using more than one capability, |
| 310 * they must all be individully ended before the isolate resumes. | 324 * each pause must be individually ended before the isolate resumes. |
| 311 * | 325 * |
| 312 * Returns the capability that must be used to resume end the pause. | 326 * Returns the capability that must be used to end the pause. |
| 313 */ | 327 */ |
| 314 Capability pause([Capability resumeCapability]) { | 328 Capability pause([Capability resumeCapability]) { |
| 315 if (resumeCapability == null) resumeCapability = new Capability(); | 329 resumeCapability ??= new Capability(); |
| 316 _pause(resumeCapability); | 330 _pause(resumeCapability); |
| 317 return resumeCapability; | 331 return resumeCapability; |
| 318 } | 332 } |
| 319 | 333 |
| 320 /** Internal implementation of [pause]. */ | 334 /** Internal implementation of [pause]. */ |
| 321 external void _pause(Capability resumeCapability); | 335 external void _pause(Capability resumeCapability); |
| 322 | 336 |
| 323 /** | 337 /** |
| 324 * Resumes a paused isolate. | 338 * Resumes a paused isolate. |
| 325 * | 339 * |
| 326 * Sends a message to an isolate requesting that it ends a pause | 340 * Sends a message to an isolate requesting that it ends a pause |
| 327 * that was requested using the [resumeCapability]. | 341 * that was requested using the [resumeCapability]. |
| 328 * | 342 * |
| 329 * When all active pause requests have been cancelled, the isolate | 343 * When all active pause requests have been cancelled, the isolate |
| 330 * will continue handling normal messages. | 344 * will continue processing events and handling normal messages. |
| 331 * | 345 * |
| 332 * The capability must be one returned by a call to [pause] on this | 346 * The capability must be one returned by a call to [pause] on this |
| 333 * isolate, otherwise the resume call does nothing. | 347 * isolate, otherwise the resume call does nothing. |
| 334 */ | 348 */ |
| 335 external void resume(Capability resumeCapability); | 349 external void resume(Capability resumeCapability); |
| 336 | 350 |
| 337 /** | 351 /** |
| 338 * Asks the isolate to send [response] on [responsePort] when it terminates. | 352 * Asks the isolate to send [response] on [responsePort] when it terminates. |
| 339 * | 353 * |
| 340 * The isolate will send a `response` message on `responsePort` as the last | 354 * The isolate will send a `response` message on `responsePort` as the last |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 * as the original error, but has no other features of the original error. | 670 * as the original error, but has no other features of the original error. |
| 657 */ | 671 */ |
| 658 class RemoteError implements Error { | 672 class RemoteError implements Error { |
| 659 final String _description; | 673 final String _description; |
| 660 final StackTrace stackTrace; | 674 final StackTrace stackTrace; |
| 661 RemoteError(String description, String stackDescription) | 675 RemoteError(String description, String stackDescription) |
| 662 : _description = description, | 676 : _description = description, |
| 663 stackTrace = new StackTrace.fromString(stackDescription); | 677 stackTrace = new StackTrace.fromString(stackDescription); |
| 664 String toString() => _description; | 678 String toString() => _description; |
| 665 } | 679 } |
| OLD | NEW |