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 /** |
| 33 * Control port used to send control messages to the isolate. |
| 34 * |
| 35 * This class provides helper functions that sends control messages |
| 36 * to the control port. |
| 37 */ |
| 38 final SendPort controlPort; |
| 39 final Capability pauseCapability; |
32 | 40 |
33 final SendPort _controlPort; | 41 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]); |
34 | |
35 Isolate._fromControlPort(SendPort controlPort) | |
36 : this._controlPort = controlPort; | |
37 | 42 |
38 /** | 43 /** |
39 * Creates and spawns an isolate that shares the same code as the current | 44 * Creates and spawns an isolate that shares the same code as the current |
40 * isolate. | 45 * isolate. |
41 * | 46 * |
42 * The argument [entryPoint] specifies the entry point of the spawned | 47 * The argument [entryPoint] specifies the entry point of the spawned |
43 * isolate. It must be a top-level function or a static method that | 48 * isolate. It must be a top-level function or a static method that |
44 * takes one argument - that is, one-parameter functions that can be | 49 * takes one argument - that is, one-parameter functions that can be |
45 * compile-time constant function values. | 50 * compile-time constant function values. |
46 * It is not allowed to pass the value of function expressions or an instance | 51 * It is not allowed to pass the value of function expressions or an instance |
(...skipping 22 matching lines...) Expand all Loading... |
69 * * `main(args, message)` | 74 * * `main(args, message)` |
70 * | 75 * |
71 * When present, the parameter `args` is set to the provided [args] list. | 76 * When present, the parameter `args` is set to the provided [args] list. |
72 * When present, the parameter `message` is set to the initial [message]. | 77 * When present, the parameter `message` is set to the initial [message]. |
73 * | 78 * |
74 * Returns a future that will complete with an [Isolate] instance if the | 79 * Returns a future that will complete with an [Isolate] instance if the |
75 * spawning succeeded. It will complete with an error otherwise. | 80 * spawning succeeded. It will complete with an error otherwise. |
76 */ | 81 */ |
77 external static Future<Isolate> spawnUri( | 82 external static Future<Isolate> spawnUri( |
78 Uri uri, List<String> args, var message); | 83 Uri uri, List<String> args, var message); |
| 84 |
| 85 |
| 86 /** |
| 87 * Requests the isolate to pause. |
| 88 * |
| 89 * The isolate should stop handling events by pausing its event queue. |
| 90 * The request will eventually make the isolate stop doing anything. |
| 91 * It will be handled before any other messages sent to the isolate from |
| 92 * the current isolate, but no other guarantees are provided. |
| 93 * |
| 94 * If [resumeCapability] is provided, it is used to identity the pause, |
| 95 * and must be used again to end the pause using [resume]. |
| 96 * Otherwise a new capability is created and returned. |
| 97 * |
| 98 * If an isolate is paused more than once using the same capabilty, |
| 99 * only one resume with that capability is needed to end the pause. |
| 100 * |
| 101 * If an isolate is paused using more than one capability, |
| 102 * they must all be individully ended before the isolate resumes. |
| 103 * |
| 104 * Returns the capability that must be used to resume end the pause. |
| 105 * |
| 106 * WARNING: This method is not handled on any platform yet. |
| 107 */ |
| 108 Capability pause([Capability resumeCapability]) { |
| 109 if (resumeCapability == null) resumeCapability = new Capability(); |
| 110 var message = new List(3) |
| 111 ..[0] = "pause" |
| 112 ..[1] = pauseCapability |
| 113 ..[2] = resumeCapability; |
| 114 controlPort.send(message); |
| 115 return resumeCapability; |
| 116 } |
| 117 |
| 118 /** |
| 119 * Resumes a paused isolate. |
| 120 * |
| 121 * Sends a message to an isolate requesting that it ends a pause |
| 122 * that was requested using the [resumeCapability]. |
| 123 * |
| 124 * The capability must be one returned by a call to [pause] on this |
| 125 * isolate, otherwise the resume call does nothing. |
| 126 * |
| 127 * WARNING: This method is not handled on any platform yet. |
| 128 */ |
| 129 void resume(Capability resumeCapability) { |
| 130 var message = new List(2) |
| 131 ..[0] = "resume" |
| 132 ..[1] = resumeCapability; |
| 133 controlPort.send(message); |
| 134 } |
79 } | 135 } |
80 | 136 |
81 /** | 137 /** |
82 * Sends messages to its [ReceivePort]s. | 138 * Sends messages to its [ReceivePort]s. |
83 * | 139 * |
84 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 140 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
85 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 141 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
86 * many [SendPort]s for the same [ReceivePort]. | 142 * many [SendPort]s for the same [ReceivePort]. |
87 * | 143 * |
88 * [SendPort]s can be transmitted to other isolates. | 144 * [SendPort]s can be transmitted to other isolates. |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); | 290 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
235 | 291 |
236 String toString() { | 292 String toString() { |
237 return 'IsolateUnhandledException: exception while handling message: ' | 293 return 'IsolateUnhandledException: exception while handling message: ' |
238 '${message} \n ' | 294 '${message} \n ' |
239 '${source.toString().replaceAll("\n", "\n ")}\n' | 295 '${source.toString().replaceAll("\n", "\n ")}\n' |
240 'original stack trace:\n ' | 296 'original stack trace:\n ' |
241 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 297 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
242 } | 298 } |
243 } | 299 } |
OLD | NEW |