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 28 matching lines...) Expand all Loading... | |
77 * When present, the parameter `args` is set to the provided [args] list. | 80 * When present, the parameter `args` is set to the provided [args] list. |
78 * When present, the parameter `message` is set to the initial [message]. | 81 * When present, the parameter `message` is set to the initial [message]. |
79 * | 82 * |
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 /** |
Søren Gjesse
2014/03/04 10:16:25
I think we should move the WARNING up as the first
Lasse Reichstein Nielsen
2014/03/06 09:26:12
Done.
| |
88 * Requests the isolate to pause. | 91 * Requests the isolate to pause. |
89 * | 92 * |
90 * The isolate should stop handling events by pausing its event queue. | 93 * The isolate should stop handling events by pausing its event queue. |
91 * The request will eventually make the isolate stop doing anything. | 94 * The request will eventually make the isolate stop doing anything. |
92 * It will be handled before any other messages sent to the isolate from | 95 * It will be handled before any other messages that are later sent to the |
93 * the current isolate, but no other guarantees are provided. | 96 * isolate from the current isolate, but no other guarantees are provided. |
97 * | |
98 * The event loop may be paused before previously sent, but not yet exeuted, | |
99 * messages have been reached. | |
94 * | 100 * |
95 * If [resumeCapability] is provided, it is used to identity the pause, | 101 * If [resumeCapability] is provided, it is used to identity the pause, |
96 * and must be used again to end the pause using [resume]. | 102 * and must be used again to end the pause using [resume]. |
97 * Otherwise a new capability is created and returned. | 103 * Otherwise a new resume capability is created and returned. |
98 * | 104 * |
99 * If an isolate is paused more than once using the same capabilty, | 105 * 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. | 106 * only one resume with that capability is needed to end the pause. |
101 * | 107 * |
102 * If an isolate is paused using more than one capability, | 108 * If an isolate is paused using more than one capability, |
103 * they must all be individully ended before the isolate resumes. | 109 * they must all be individully ended before the isolate resumes. |
104 * | 110 * |
105 * Returns the capability that must be used to resume end the pause. | 111 * Returns the capability that must be used to resume end the pause. |
106 * | 112 * |
107 * WARNING: This method is not handled on any platform yet. | 113 * WARNING: This method is not handled on every 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 * |
122 * Sends a message to an isolate requesting that it ends a pause | 128 * Sends a message to an isolate requesting that it ends a pause |
123 * that was requested using the [resumeCapability]. | 129 * that was requested using the [resumeCapability]. |
124 * | 130 * |
131 * When all active pause requests have been cancelled, the isolate | |
132 * will continue handling normal messages. | |
133 * | |
125 * The capability must be one returned by a call to [pause] on this | 134 * The capability must be one returned by a call to [pause] on this |
126 * isolate, otherwise the resume call does nothing. | 135 * isolate, otherwise the resume call does nothing. |
127 * | 136 * |
128 * WARNING: This method is not handled on any platform yet. | 137 * WARNING: This method is not handled on every 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 * The isolate will send a `null` message on [responsePort] as the last | |
150 * thing before it terminates. It will run no further code after the message | |
151 * has been sent. | |
152 * | |
153 * If the isolate is already dead, no message will be sent. | |
154 * TODO(lrn): Can we do better? Can the system recognize this message and | |
155 * send a reply if the receiving isolate is dead? | |
Søren Gjesse
2014/03/04 10:16:25
Add the warning as for pause/resume.
Lasse Reichstein Nielsen
2014/03/06 09:26:12
Done.
| |
156 */ | |
157 void addOnExitListener(SendPort responsePort) { | |
Søren Gjesse
2014/03/04 10:16:25
No capability needed to "spy" on when an isolate e
Lasse Reichstein Nielsen
2014/03/06 09:26:12
No. It's considered harmless. You don't leak any i
| |
158 // TODO(lrn): Can we have an internal method that checks if the receiving | |
159 // isolate of a SendPort is still alive? | |
160 var message = new List(2) | |
161 ..[0] = "add-ondone" | |
162 ..[1] = responsePort; | |
163 controlPort.send(message); | |
164 } | |
165 | |
166 /** | |
167 * Stop listening on exit messages from the isolate. | |
168 * | |
169 * If a call has previously been made to [addOnExitListener] with the same | |
170 * send-port, this will unregister the port, and it will no longer receive | |
171 * a message when the isolate terminates. | |
172 * A response may still be sent until this operation is fully processed by | |
173 * the isolate. | |
174 */ | |
175 void removeOnExitListener(SendPort responsePort) { | |
176 var message = new List(2) | |
177 ..[0] = "remove-ondone" | |
178 ..[1] = responsePort; | |
179 controlPort.send(message); | |
180 } | |
136 } | 181 } |
137 | 182 |
138 /** | 183 /** |
139 * Sends messages to its [ReceivePort]s. | 184 * Sends messages to its [ReceivePort]s. |
140 * | 185 * |
141 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 186 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
142 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 187 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
143 * many [SendPort]s for the same [ReceivePort]. | 188 * many [SendPort]s for the same [ReceivePort]. |
144 * | 189 * |
145 * [SendPort]s can be transmitted to other isolates. | 190 * [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); | 336 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
292 | 337 |
293 String toString() { | 338 String toString() { |
294 return 'IsolateUnhandledException: exception while handling message: ' | 339 return 'IsolateUnhandledException: exception while handling message: ' |
295 '${message} \n ' | 340 '${message} \n ' |
296 '${source.toString().replaceAll("\n", "\n ")}\n' | 341 '${source.toString().replaceAll("\n", "\n ")}\n' |
297 'original stack trace:\n ' | 342 'original stack trace:\n ' |
298 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 343 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
299 } | 344 } |
300 } | 345 } |
OLD | NEW |