| 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. */ |
| 33 static const int PING_ALIVE = 0; |
| 34 /** Argument to `ping`: Ask for response after control events. */ |
| 35 static const int PING_CONTROL = 1; |
| 36 /** Argument to `ping`: Ask for response after normal events. */ |
| 37 static const int PING_EVENT = 2; |
| 38 |
| 32 /** | 39 /** |
| 33 * Control port used to send control messages to the isolate. | 40 * Control port used to send control messages to the isolate. |
| 34 * | 41 * |
| 35 * This class provides helper functions that sends control messages | 42 * This class provides helper functions that sends control messages |
| 36 * to the control port. | 43 * to the control port. |
| 37 */ | 44 */ |
| 38 final SendPort controlPort; | 45 final SendPort controlPort; |
| 39 /** | 46 /** |
| 40 * Capability granting the ability to pause the isolate. | 47 * Capability granting the ability to pause the isolate. |
| 41 */ | 48 */ |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 * This call requires the [terminateCapability] for the isolate. | 223 * This call requires the [terminateCapability] for the isolate. |
| 217 * If the capability is not correct, no change is made. | 224 * If the capability is not correct, no change is made. |
| 218 */ | 225 */ |
| 219 void setErrorsFatal(bool errorsAreFatal) { | 226 void setErrorsFatal(bool errorsAreFatal) { |
| 220 var message = new List(3) | 227 var message = new List(3) |
| 221 ..[0] = "set-errors-fatal" | 228 ..[0] = "set-errors-fatal" |
| 222 ..[1] = terminateCapability | 229 ..[1] = terminateCapability |
| 223 ..[2] = errorsAreFatal; | 230 ..[2] = errorsAreFatal; |
| 224 controlPort.send(message); | 231 controlPort.send(message); |
| 225 } | 232 } |
| 233 |
| 234 /** |
| 235 * Request that the isolate send a response on the [responsePort]. |
| 236 * |
| 237 * WARNING: This method is experimental and not handled on every platform yet. |
| 238 * |
| 239 * If the isolate is alive, it will eventually send a `null` response on |
| 240 * the response port. |
| 241 * |
| 242 * The [pingType] must be one of [PING_ALIVE], [PING_CONTROL] or [PING_EVENT]. |
| 243 * The response is sent at different times depending on the ping type: |
| 244 * |
| 245 * * `PING_ALIVE`: The the isolate responds as soon as possible. |
| 246 * The response should happen no later than if sent with `PING_CONTROL`. |
| 247 * It may be sent earlier if the system has a way to do so. |
| 248 * * `PING_CONTROL`: The response it not sent until all previously sent |
| 249 * control messages from the current isolate to the receiving isolate |
| 250 * have been processed. This can be used to wait for |
| 251 * previously sent control messages. |
| 252 * * `PING_EVENT`: The response is not sent until all prevously sent |
| 253 * non-control messages from the current isolate to the receiving isolate |
| 254 * have been processed. |
| 255 * The ping effectively puts the resonse into the normal event queue after |
| 256 * previously sent messages. |
| 257 * This can be used to wait for a another event to be processed. |
| 258 */ |
| 259 void ping(SendPort responsePort, [int pingType = PING_ALIVE]) { |
| 260 var message = new List(3) |
| 261 ..[0] = "ping" |
| 262 ..[1] = responsePort |
| 263 ..[2] = pingType; |
| 264 controlPort.send(message); |
| 265 } |
| 226 } | 266 } |
| 227 | 267 |
| 228 /** | 268 /** |
| 229 * Sends messages to its [ReceivePort]s. | 269 * Sends messages to its [ReceivePort]s. |
| 230 * | 270 * |
| 231 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 271 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
| 232 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 272 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
| 233 * many [SendPort]s for the same [ReceivePort]. | 273 * many [SendPort]s for the same [ReceivePort]. |
| 234 * | 274 * |
| 235 * [SendPort]s can be transmitted to other isolates. | 275 * [SendPort]s can be transmitted to other isolates. |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 421 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
| 382 | 422 |
| 383 String toString() { | 423 String toString() { |
| 384 return 'IsolateUnhandledException: exception while handling message: ' | 424 return 'IsolateUnhandledException: exception while handling message: ' |
| 385 '${message} \n ' | 425 '${message} \n ' |
| 386 '${source.toString().replaceAll("\n", "\n ")}\n' | 426 '${source.toString().replaceAll("\n", "\n ")}\n' |
| 387 'original stack trace:\n ' | 427 'original stack trace:\n ' |
| 388 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 428 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
| 389 } | 429 } |
| 390 } | 430 } |
| OLD | NEW |