Chromium Code Reviews| 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 final Capability pauseCapability; | 46 final Capability pauseCapability; |
| 40 | 47 |
| 41 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]); | 48 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 * isolate, otherwise the resume call does nothing. | 133 * isolate, otherwise the resume call does nothing. |
| 127 * | 134 * |
| 128 * WARNING: This method is not handled on any platform yet. | 135 * WARNING: This method is not handled on any platform yet. |
| 129 */ | 136 */ |
| 130 void resume(Capability resumeCapability) { | 137 void resume(Capability resumeCapability) { |
| 131 var message = new List(2) | 138 var message = new List(2) |
| 132 ..[0] = "resume" | 139 ..[0] = "resume" |
| 133 ..[1] = resumeCapability; | 140 ..[1] = resumeCapability; |
| 134 controlPort.send(message); | 141 controlPort.send(message); |
| 135 } | 142 } |
| 143 | |
| 144 /** | |
|
Søren Gjesse
2014/03/04 10:20:35
Add WARNING.
Lasse Reichstein Nielsen
2014/03/04 12:03:32
Done.
| |
| 145 * Request that the isolate send a response on the [responsePort]. | |
| 146 * | |
| 147 * If the isolate is alive, it will eventually send a `null` response on | |
| 148 * the response port. | |
| 149 * | |
| 150 * The [pingType] must be one of [PING_ALIVE], [PING_CONTROL] or [PING_EVENT]. | |
| 151 * The response is sent at different times depending on the ping type: | |
| 152 * | |
| 153 * * `PING_ALIVE`: The the isolate responds as soon as possible. | |
| 154 * The response should happen no sooner than if sent with `PING_CONTROL`. | |
|
floitsch
2014/03/04 12:12:51
no later?
Lasse Reichstein Nielsen
2014/03/06 12:47:17
Done.
| |
| 155 * It may be sent earlier of the system has a way to do so. | |
|
floitsch
2014/03/04 12:12:51
if the system
Lasse Reichstein Nielsen
2014/03/06 12:47:17
Done.
| |
| 156 * * `PING_CONTROL`: The response it not sent until all previously sent | |
| 157 * control messages from the current isolate to the receiving isolate | |
| 158 * have been processed. This can be used to wait for | |
| 159 * previously sent control messages. | |
| 160 * * `PING_EVENT`: The response is not sent until all prevously sent | |
| 161 * non-control messages from the current isolate to the receiving isolate | |
| 162 * have been processed. | |
| 163 * The ping effectively puts the resonse into the normal event queue after | |
| 164 * previously sent messages. | |
| 165 * This can be used to wait for a another event to be processed. | |
| 166 */ | |
| 167 void ping(SendPort responsePort, [int pingType = PING_ALIVE]) { | |
|
Søren Gjesse
2014/03/04 10:20:35
No capability needed for this?
Lasse Reichstein Nielsen
2014/03/04 12:03:32
No. You don't affect how the isolate runs and don'
floitsch
2014/03/04 12:12:51
I think that ping is the minimal thing we can do.
| |
| 168 var message = new List(3) | |
| 169 ..[0] = "ping" | |
| 170 ..[1] = responsePort | |
| 171 ..[2] = pingType; | |
| 172 controlPort.send(message); | |
| 173 } | |
| 136 } | 174 } |
| 137 | 175 |
| 138 /** | 176 /** |
| 139 * Sends messages to its [ReceivePort]s. | 177 * Sends messages to its [ReceivePort]s. |
| 140 * | 178 * |
| 141 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 179 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
| 142 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 180 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
| 143 * many [SendPort]s for the same [ReceivePort]. | 181 * many [SendPort]s for the same [ReceivePort]. |
| 144 * | 182 * |
| 145 * [SendPort]s can be transmitted to other isolates. | 183 * [SendPort]s can be transmitted to other isolates. |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 329 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
| 292 | 330 |
| 293 String toString() { | 331 String toString() { |
| 294 return 'IsolateUnhandledException: exception while handling message: ' | 332 return 'IsolateUnhandledException: exception while handling message: ' |
| 295 '${message} \n ' | 333 '${message} \n ' |
| 296 '${source.toString().replaceAll("\n", "\n ")}\n' | 334 '${source.toString().replaceAll("\n", "\n ")}\n' |
| 297 'original stack trace:\n ' | 335 'original stack trace:\n ' |
| 298 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 336 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
| 299 } | 337 } |
| 300 } | 338 } |
| OLD | NEW |