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` and `kill`: Ask for immediate action. */ | 32 /** Argument to `ping`: Ask for immediate response. */ |
33 static const int IMMEDIATE = 0; | 33 static const int PING_ALIVE = 0; |
34 /** Argument to `ping` and `kill`: Ask for action before the next event. */ | 34 /** Argument to `ping`: Ask for response after control events. */ |
35 static const int BEFORE_NEXT_EVENT = 1; | 35 static const int PING_CONTROL = 1; |
36 /** Argument to `ping` and `kill`: Ask for action after normal events. */ | 36 /** Argument to `ping`: Ask for response after normal events. */ |
37 static const int AS_EVENT = 2; | 37 static const int PING_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 |
118 /** | 119 /** |
119 * Requests the isolate to pause. | 120 * Requests the isolate to pause. |
120 * | 121 * |
121 * WARNING: This method is experimental and not handled on every platform yet. | 122 * WARNING: This method is experimental and not handled on every platform yet. |
122 * | 123 * |
123 * The isolate should stop handling events by pausing its event queue. | 124 * The isolate should stop handling events by pausing its event queue. |
124 * The request will eventually make the isolate stop doing anything. | 125 * The request will eventually make the isolate stop doing anything. |
125 * It will be handled before any other messages that are later sent to the | 126 * It will be handled before any other messages that are later sent to the |
126 * isolate from the current isolate, but no other guarantees are provided. | 127 * isolate from the current isolate, but no other guarantees are provided. |
127 * | 128 * |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 */ | 225 */ |
225 void setErrorsFatal(bool errorsAreFatal) { | 226 void setErrorsFatal(bool errorsAreFatal) { |
226 var message = new List(3) | 227 var message = new List(3) |
227 ..[0] = "set-errors-fatal" | 228 ..[0] = "set-errors-fatal" |
228 ..[1] = terminateCapability | 229 ..[1] = terminateCapability |
229 ..[2] = errorsAreFatal; | 230 ..[2] = errorsAreFatal; |
230 controlPort.send(message); | 231 controlPort.send(message); |
231 } | 232 } |
232 | 233 |
233 /** | 234 /** |
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 /** | |
269 * Request that the isolate send a response on the [responsePort]. | 235 * Request that the isolate send a response on the [responsePort]. |
270 * | 236 * |
271 * WARNING: This method is experimental and not handled on every platform yet. | 237 * WARNING: This method is experimental and not handled on every platform yet. |
272 * | 238 * |
273 * If the isolate is alive, it will eventually send a `null` response on | 239 * If the isolate is alive, it will eventually send a `null` response on |
274 * the response port. | 240 * the response port. |
275 * | 241 * |
276 * The [pingType] must be one of [IMMEDIATE], [BEFORE_NEXT_EVENT] or | 242 * The [pingType] must be one of [PING_ALIVE], [PING_CONTROL] or [PING_EVENT]. |
277 * [AS_EVENT]. | |
278 * The response is sent at different times depending on the ping type: | 243 * The response is sent at different times depending on the ping type: |
279 * | 244 * |
280 * * `IMMEDIATE`: The the isolate responds as soon as it receives the | 245 * * `PING_ALIVE`: The the isolate responds as soon as possible. |
281 * control message. | 246 * The response should happen no later than if sent with `PING_CONTROL`. |
282 * * `BEFORE_NEXT_EVENT`: The response is scheduled for the next time | 247 * It may be sent earlier if the system has a way to do so. |
283 * control returns to the event loop of the receiving isolate. | 248 * * `PING_CONTROL`: The response it not sent until all previously sent |
284 * If more than one such event are scheduled, they are executed in | 249 * control messages from the current isolate to the receiving isolate |
285 * the order their control messages were received. | 250 * have been processed. This can be used to wait for |
286 * * `AS_EVENT`: The response is not sent until all prevously sent | 251 * previously sent control messages. |
| 252 * * `PING_EVENT`: The response is not sent until all prevously sent |
287 * non-control messages from the current isolate to the receiving isolate | 253 * non-control messages from the current isolate to the receiving isolate |
288 * have been processed. | 254 * have been processed. |
289 * The ping effectively puts the response into the normal event queue | 255 * The ping effectively puts the resonse into the normal event queue after |
290 * after previously sent messages, and it is affected by any control | 256 * previously sent messages. |
291 * messages that affect normal events, including `pause`. | |
292 * This can be used to wait for a another event to be processed. | 257 * This can be used to wait for a another event to be processed. |
293 */ | 258 */ |
294 void ping(SendPort responsePort, [int pingType = IMMEDIATE]) { | 259 void ping(SendPort responsePort, [int pingType = PING_ALIVE]) { |
295 var message = new List(3) | 260 var message = new List(3) |
296 ..[0] = "ping" | 261 ..[0] = "ping" |
297 ..[1] = responsePort | 262 ..[1] = responsePort |
298 ..[2] = pingType; | 263 ..[2] = pingType; |
299 controlPort.send(message); | 264 controlPort.send(message); |
300 } | 265 } |
301 } | 266 } |
302 | 267 |
303 /** | 268 /** |
304 * Sends messages to its [ReceivePort]s. | 269 * Sends messages to its [ReceivePort]s. |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 422 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
458 | 423 |
459 String toString() { | 424 String toString() { |
460 return 'IsolateUnhandledException: exception while handling message: ' | 425 return 'IsolateUnhandledException: exception while handling message: ' |
461 '${message} \n ' | 426 '${message} \n ' |
462 '${source.toString().replaceAll("\n", "\n ")}\n' | 427 '${source.toString().replaceAll("\n", "\n ")}\n' |
463 'original stack trace:\n ' | 428 'original stack trace:\n ' |
464 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 429 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
465 } | 430 } |
466 } | 431 } |
OLD | NEW |