| 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 11 matching lines...) Expand all Loading... |
| 22 * Thrown when an isolate cannot be created. | 22 * Thrown when an isolate cannot be created. |
| 23 */ | 23 */ |
| 24 class IsolateSpawnException implements Exception { | 24 class IsolateSpawnException implements Exception { |
| 25 // TODO(floitsch): clean up spawn exception. | 25 // TODO(floitsch): clean up spawn exception. |
| 26 const IsolateSpawnException(String this._s); | 26 const IsolateSpawnException(String this._s); |
| 27 String toString() => "IsolateSpawnException: '$_s'"; | 27 String toString() => "IsolateSpawnException: '$_s'"; |
| 28 final String _s; | 28 final String _s; |
| 29 } | 29 } |
| 30 | 30 |
| 31 class Isolate { | 31 class Isolate { |
| 32 /** Argument to `ping`: Ask for immediate response. */ | 32 /** Argument to `ping` and `kill`: Ask for immediate action. */ |
| 33 static const int PING_ALIVE = 0; | 33 static const int IMMEDIATE = 0; |
| 34 /** Argument to `ping`: Ask for response after control events. */ | 34 /** Argument to `ping` and `kill`: Ask for action before the next event. */ |
| 35 static const int PING_CONTROL = 1; | 35 static const int BEFORE_NEXT_EVENT = 1; |
| 36 /** Argument to `ping`: Ask for response after normal events. */ | 36 /** Argument to `ping` and `kill`: Ask for action after normal events. */ |
| 37 static const int PING_EVENT = 2; | 37 static const int AS_EVENT = 2; |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * Control port used to send control messages to the isolate. | 40 * Control port used to send control messages to the isolate. |
| 41 * | 41 * |
| 42 * This class provides helper functions that sends control messages | 42 * This class provides helper functions that sends control messages |
| 43 * to the control port. | 43 * to the control port. |
| 44 */ | 44 */ |
| 45 final SendPort controlPort; | 45 final SendPort controlPort; |
| 46 /** | 46 /** |
| 47 * Capability granting the ability to pause the isolate. | 47 * Capability granting the ability to pause the isolate. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 * | 108 * |
| 109 * When present, the parameter `args` is set to the provided [args] list. | 109 * When present, the parameter `args` is set to the provided [args] list. |
| 110 * When present, the parameter `message` is set to the initial [message]. | 110 * When present, the parameter `message` is set to the initial [message]. |
| 111 * | 111 * |
| 112 * Returns a future that will complete with an [Isolate] instance if the | 112 * Returns a future that will complete with an [Isolate] instance if the |
| 113 * spawning succeeded. It will complete with an error otherwise. | 113 * spawning succeeded. It will complete with an error otherwise. |
| 114 */ | 114 */ |
| 115 external static Future<Isolate> spawnUri( | 115 external static Future<Isolate> spawnUri( |
| 116 Uri uri, List<String> args, var message, { bool paused: false }); | 116 Uri uri, List<String> args, var message, { bool paused: false }); |
| 117 | 117 |
| 118 | |
| 119 /** | 118 /** |
| 120 * Requests the isolate to pause. | 119 * Requests the isolate to pause. |
| 121 * | 120 * |
| 122 * WARNING: This method is experimental and not handled on every platform yet. | 121 * WARNING: This method is experimental and not handled on every platform yet. |
| 123 * | 122 * |
| 124 * The isolate should stop handling events by pausing its event queue. | 123 * The isolate should stop handling events by pausing its event queue. |
| 125 * The request will eventually make the isolate stop doing anything. | 124 * The request will eventually make the isolate stop doing anything. |
| 126 * It will be handled before any other messages that are later sent to the | 125 * It will be handled before any other messages that are later sent to the |
| 127 * isolate from the current isolate, but no other guarantees are provided. | 126 * isolate from the current isolate, but no other guarantees are provided. |
| 128 * | 127 * |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 */ | 224 */ |
| 226 void setErrorsFatal(bool errorsAreFatal) { | 225 void setErrorsFatal(bool errorsAreFatal) { |
| 227 var message = new List(3) | 226 var message = new List(3) |
| 228 ..[0] = "set-errors-fatal" | 227 ..[0] = "set-errors-fatal" |
| 229 ..[1] = terminateCapability | 228 ..[1] = terminateCapability |
| 230 ..[2] = errorsAreFatal; | 229 ..[2] = errorsAreFatal; |
| 231 controlPort.send(message); | 230 controlPort.send(message); |
| 232 } | 231 } |
| 233 | 232 |
| 234 /** | 233 /** |
| 234 * Requests the isolate to shut down. |
| 235 * |
| 236 * WARNING: This method is experimental and not handled on every platform yet. |
| 237 * |
| 238 * The isolate is requested to terminate itself. |
| 239 * The [priority] argument specifies when this must happen. |
| 240 * |
| 241 * The [priority] must be one of [IMMEDIATE], [BEFORE_NEXT_EVENT] or |
| 242 * [AS_EVENT]. |
| 243 * The shutdown is performed at different times depending on the priority: |
| 244 * |
| 245 * * `IMMEDIATE`: The the isolate shuts down as soon as possible. |
| 246 * Control messages are handled in order, so all previously sent control |
| 247 * events from this isolate will all have been processed. |
| 248 * The shutdown should happen no later than if sent with |
| 249 * `BEFORE_NEXT_EVENT`. |
| 250 * It may happen earlier if the system has a way to shut down cleanly |
| 251 * at an earlier time, even during the execution of another event. |
| 252 * * `BEFORE_NEXT_EVENT`: The shutdown is scheduled for the next time |
| 253 * control returns to the event loop of the receiving isolate. |
| 254 * If more than one such event are scheduled, they are executed in |
| 255 * the order their control messages were received. |
| 256 * * `AS_EVENT`: The shutdown does not happen until all prevously sent |
| 257 * non-control messages from the current isolate to the receiving isolate |
| 258 * have been processed. |
| 259 * The kill operation effectively puts the shutdown into the normal event |
| 260 * queue after previously sent messages, and it is affected by any control |
| 261 * messages that affect normal events, including `pause`. |
| 262 * This can be used to wait for a another event to be processed. |
| 263 */ |
| 264 void kill([int priority = BEFORE_NEXT_EVENT]) { |
| 265 controlPort.send(["kill", terminateCapability, priority]); |
| 266 } |
| 267 |
| 268 /** |
| 235 * Request that the isolate send a response on the [responsePort]. | 269 * Request that the isolate send a response on the [responsePort]. |
| 236 * | 270 * |
| 237 * WARNING: This method is experimental and not handled on every platform yet. | 271 * WARNING: This method is experimental and not handled on every platform yet. |
| 238 * | 272 * |
| 239 * If the isolate is alive, it will eventually send a `null` response on | 273 * If the isolate is alive, it will eventually send a `null` response on |
| 240 * the response port. | 274 * the response port. |
| 241 * | 275 * |
| 242 * The [pingType] must be one of [PING_ALIVE], [PING_CONTROL] or [PING_EVENT]. | 276 * The [pingType] must be one of [IMMEDIATE], [BEFORE_NEXT_EVENT] or |
| 277 * [AS_EVENT]. |
| 243 * The response is sent at different times depending on the ping type: | 278 * The response is sent at different times depending on the ping type: |
| 244 * | 279 * |
| 245 * * `PING_ALIVE`: The the isolate responds as soon as possible. | 280 * * `IMMEDIATE`: The the isolate responds as soon as it receives the |
| 246 * The response should happen no later than if sent with `PING_CONTROL`. | 281 * control message. |
| 247 * It may be sent earlier if the system has a way to do so. | 282 * * `BEFORE_NEXT_EVENT`: The response is scheduled for the next time |
| 248 * * `PING_CONTROL`: The response it not sent until all previously sent | 283 * control returns to the event loop of the receiving isolate. |
| 249 * control messages from the current isolate to the receiving isolate | 284 * If more than one such event are scheduled, they are executed in |
| 250 * have been processed. This can be used to wait for | 285 * the order their control messages were received. |
| 251 * previously sent control messages. | 286 * * `AS_EVENT`: The response is not sent until all prevously sent |
| 252 * * `PING_EVENT`: The response is not sent until all prevously sent | |
| 253 * non-control messages from the current isolate to the receiving isolate | 287 * non-control messages from the current isolate to the receiving isolate |
| 254 * have been processed. | 288 * have been processed. |
| 255 * The ping effectively puts the resonse into the normal event queue after | 289 * The ping effectively puts the response into the normal event queue |
| 256 * previously sent messages. | 290 * after previously sent messages, and it is affected by any control |
| 291 * messages that affect normal events, including `pause`. |
| 257 * This can be used to wait for a another event to be processed. | 292 * This can be used to wait for a another event to be processed. |
| 258 */ | 293 */ |
| 259 void ping(SendPort responsePort, [int pingType = PING_ALIVE]) { | 294 void ping(SendPort responsePort, [int pingType = IMMEDIATE]) { |
| 260 var message = new List(3) | 295 var message = new List(3) |
| 261 ..[0] = "ping" | 296 ..[0] = "ping" |
| 262 ..[1] = responsePort | 297 ..[1] = responsePort |
| 263 ..[2] = pingType; | 298 ..[2] = pingType; |
| 264 controlPort.send(message); | 299 controlPort.send(message); |
| 265 } | 300 } |
| 266 } | 301 } |
| 267 | 302 |
| 268 /** | 303 /** |
| 269 * Sends messages to its [ReceivePort]s. | 304 * Sends messages to its [ReceivePort]s. |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 457 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
| 423 | 458 |
| 424 String toString() { | 459 String toString() { |
| 425 return 'IsolateUnhandledException: exception while handling message: ' | 460 return 'IsolateUnhandledException: exception while handling message: ' |
| 426 '${message} \n ' | 461 '${message} \n ' |
| 427 '${source.toString().replaceAll("\n", "\n ")}\n' | 462 '${source.toString().replaceAll("\n", "\n ")}\n' |
| 428 'original stack trace:\n ' | 463 'original stack trace:\n ' |
| 429 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 464 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
| 430 } | 465 } |
| 431 } | 466 } |
| OLD | NEW |