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 18 matching lines...) Expand all Loading... |
29 } | 29 } |
30 | 30 |
31 class Isolate { | 31 class Isolate { |
32 /** | 32 /** |
33 * Control port used to send control messages to the isolate. | 33 * Control port used to send control messages to the isolate. |
34 * | 34 * |
35 * This class provides helper functions that sends control messages | 35 * This class provides helper functions that sends control messages |
36 * to the control port. | 36 * to the control port. |
37 */ | 37 */ |
38 final SendPort controlPort; | 38 final SendPort controlPort; |
| 39 /** |
| 40 * Capability granting the ability to pause the isolate. |
| 41 */ |
39 final Capability pauseCapability; | 42 final Capability pauseCapability; |
40 | 43 |
41 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]); | 44 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]); |
42 | 45 |
43 /** | 46 /** |
44 * Creates and spawns an isolate that shares the same code as the current | 47 * Creates and spawns an isolate that shares the same code as the current |
45 * isolate. | 48 * isolate. |
46 * | 49 * |
47 * The argument [entryPoint] specifies the entry point of the spawned | 50 * The argument [entryPoint] specifies the entry point of the spawned |
48 * isolate. It must be a top-level function or a static method that | 51 * isolate. It must be a top-level function or a static method that |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 * Returns a future that will complete with an [Isolate] instance if the | 83 * Returns a future that will complete with an [Isolate] instance if the |
81 * spawning succeeded. It will complete with an error otherwise. | 84 * spawning succeeded. It will complete with an error otherwise. |
82 */ | 85 */ |
83 external static Future<Isolate> spawnUri( | 86 external static Future<Isolate> spawnUri( |
84 Uri uri, List<String> args, var message, { bool paused: false }); | 87 Uri uri, List<String> args, var message, { bool paused: false }); |
85 | 88 |
86 | 89 |
87 /** | 90 /** |
88 * Requests the isolate to pause. | 91 * Requests the isolate to pause. |
89 * | 92 * |
| 93 * WARNING: This method is experimental and not handled on every platform yet. |
| 94 * |
90 * The isolate should stop handling events by pausing its event queue. | 95 * The isolate should stop handling events by pausing its event queue. |
91 * The request will eventually make the isolate stop doing anything. | 96 * The request will eventually make the isolate stop doing anything. |
92 * It will be handled before any other messages sent to the isolate from | 97 * It will be handled before any other messages that are later sent to the |
93 * the current isolate, but no other guarantees are provided. | 98 * isolate from the current isolate, but no other guarantees are provided. |
| 99 * |
| 100 * The event loop may be paused before previously sent, but not yet exeuted, |
| 101 * messages have been reached. |
94 * | 102 * |
95 * If [resumeCapability] is provided, it is used to identity the pause, | 103 * If [resumeCapability] is provided, it is used to identity the pause, |
96 * and must be used again to end the pause using [resume]. | 104 * and must be used again to end the pause using [resume]. |
97 * Otherwise a new capability is created and returned. | 105 * Otherwise a new resume capability is created and returned. |
98 * | 106 * |
99 * If an isolate is paused more than once using the same capabilty, | 107 * If an isolate is paused more than once using the same capability, |
100 * only one resume with that capability is needed to end the pause. | 108 * only one resume with that capability is needed to end the pause. |
101 * | 109 * |
102 * If an isolate is paused using more than one capability, | 110 * If an isolate is paused using more than one capability, |
103 * they must all be individully ended before the isolate resumes. | 111 * they must all be individully ended before the isolate resumes. |
104 * | 112 * |
105 * Returns the capability that must be used to resume end the pause. | 113 * Returns the capability that must be used to resume end the pause. |
106 * | |
107 * WARNING: This method is not handled on any platform yet. | |
108 */ | 114 */ |
109 Capability pause([Capability resumeCapability]) { | 115 Capability pause([Capability resumeCapability]) { |
110 if (resumeCapability == null) resumeCapability = new Capability(); | 116 if (resumeCapability == null) resumeCapability = new Capability(); |
111 var message = new List(3) | 117 var message = new List(3) |
112 ..[0] = "pause" | 118 ..[0] = "pause" |
113 ..[1] = pauseCapability | 119 ..[1] = pauseCapability |
114 ..[2] = resumeCapability; | 120 ..[2] = resumeCapability; |
115 controlPort.send(message); | 121 controlPort.send(message); |
116 return resumeCapability; | 122 return resumeCapability; |
117 } | 123 } |
118 | 124 |
119 /** | 125 /** |
120 * Resumes a paused isolate. | 126 * Resumes a paused isolate. |
121 * | 127 * |
| 128 * WARNING: This method is experimental and not handled on every platform yet. |
| 129 * |
122 * Sends a message to an isolate requesting that it ends a pause | 130 * Sends a message to an isolate requesting that it ends a pause |
123 * that was requested using the [resumeCapability]. | 131 * that was requested using the [resumeCapability]. |
124 * | 132 * |
| 133 * When all active pause requests have been cancelled, the isolate |
| 134 * will continue handling normal messages. |
| 135 * |
125 * The capability must be one returned by a call to [pause] on this | 136 * The capability must be one returned by a call to [pause] on this |
126 * isolate, otherwise the resume call does nothing. | 137 * isolate, otherwise the resume call does nothing. |
127 * | |
128 * WARNING: This method is not handled on any platform yet. | |
129 */ | 138 */ |
130 void resume(Capability resumeCapability) { | 139 void resume(Capability resumeCapability) { |
131 var message = new List(2) | 140 var message = new List(2) |
132 ..[0] = "resume" | 141 ..[0] = "resume" |
133 ..[1] = resumeCapability; | 142 ..[1] = resumeCapability; |
134 controlPort.send(message); | 143 controlPort.send(message); |
135 } | 144 } |
| 145 |
| 146 /** |
| 147 * Asks the isolate to send a message on [responsePort] when it terminates. |
| 148 * |
| 149 * WARNING: This method is experimental and not handled on every platform yet. |
| 150 * |
| 151 * The isolate will send a `null` message on [responsePort] as the last |
| 152 * thing before it terminates. It will run no further code after the message |
| 153 * has been sent. |
| 154 * |
| 155 * If the isolate is already dead, no message will be sent. |
| 156 * TODO(lrn): Can we do better? Can the system recognize this message and |
| 157 * send a reply if the receiving isolate is dead? |
| 158 */ |
| 159 void addOnExitListener(SendPort responsePort) { |
| 160 // TODO(lrn): Can we have an internal method that checks if the receiving |
| 161 // isolate of a SendPort is still alive? |
| 162 var message = new List(2) |
| 163 ..[0] = "add-ondone" |
| 164 ..[1] = responsePort; |
| 165 controlPort.send(message); |
| 166 } |
| 167 |
| 168 /** |
| 169 * Stop listening on exit messages from the isolate. |
| 170 * |
| 171 * WARNING: This method is experimental and not handled on every platform yet. |
| 172 * |
| 173 * If a call has previously been made to [addOnExitListener] with the same |
| 174 * send-port, this will unregister the port, and it will no longer receive |
| 175 * a message when the isolate terminates. |
| 176 * A response may still be sent until this operation is fully processed by |
| 177 * the isolate. |
| 178 */ |
| 179 void removeOnExitListener(SendPort responsePort) { |
| 180 var message = new List(2) |
| 181 ..[0] = "remove-ondone" |
| 182 ..[1] = responsePort; |
| 183 controlPort.send(message); |
| 184 } |
136 } | 185 } |
137 | 186 |
138 /** | 187 /** |
139 * Sends messages to its [ReceivePort]s. | 188 * Sends messages to its [ReceivePort]s. |
140 * | 189 * |
141 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 190 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
142 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 191 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
143 * many [SendPort]s for the same [ReceivePort]. | 192 * many [SendPort]s for the same [ReceivePort]. |
144 * | 193 * |
145 * [SendPort]s can be transmitted to other isolates. | 194 * [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); | 340 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
292 | 341 |
293 String toString() { | 342 String toString() { |
294 return 'IsolateUnhandledException: exception while handling message: ' | 343 return 'IsolateUnhandledException: exception while handling message: ' |
295 '${message} \n ' | 344 '${message} \n ' |
296 '${source.toString().replaceAll("\n", "\n ")}\n' | 345 '${source.toString().replaceAll("\n", "\n ")}\n' |
297 'original stack trace:\n ' | 346 'original stack trace:\n ' |
298 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 347 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
299 } | 348 } |
300 } | 349 } |
OLD | NEW |