OLD | NEW |
1 // Copyright (c) 2011, 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 class IsolateSpawnException implements Exception { | 5 class IsolateSpawnException implements Exception { |
6 const IsolateSpawnException(String this._s); | 6 const IsolateSpawnException(String this._s); |
7 String toString() => "IsolateSpawnException: '$_s'"; | 7 String toString() => "IsolateSpawnException: '$_s'"; |
8 final String _s; | 8 final String _s; |
9 } | 9 } |
10 | 10 |
11 /** | 11 /** |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 SendPort toSendPort(); | 139 SendPort toSendPort(); |
140 | 140 |
141 } | 141 } |
142 | 142 |
143 // TODO(kasperl): Make this hashable and document it. | 143 // TODO(kasperl): Make this hashable and document it. |
144 interface SendPortSync { | 144 interface SendPortSync { |
145 | 145 |
146 callSync(var message); | 146 callSync(var message); |
147 | 147 |
148 } | 148 } |
149 | |
150 /** | |
151 * NOTE: This API will be deprecated soon. | |
152 * | |
153 * The [Isolate] class serves two purposes: (1) as template for spawning a new | |
154 * isolate, and (2) as entry-point for the newly spawned isolate. | |
155 * | |
156 * New isolates are spawned by sub-classing [Isolate] and then invoking | |
157 * [:spawn:] on the instance. This will spawn a new isolate, which creates a | |
158 * new instance of the class, initializes the instance's [port] field | |
159 * and invokes the instance method [main]. | |
160 * | |
161 * The new instance is created by invoking the default constructor of the | |
162 * class that served as template for spawning the new isolate. This means, that | |
163 * sub-classes must have a default constructor (i.e. no-argument constructor). | |
164 * | |
165 * Isolates may be "heavy" or "light". Heavy isolates live in their own thread, | |
166 * whereas "light" isolates live in the same thread as the isolate which spawned | |
167 * them. | |
168 * | |
169 * NOTE: once [spawnFunction] and [spawnUri] are supported in both frog and the | |
170 * dartvm, this class will be removed. The distinction of heavy and light will | |
171 * be removed too. Thus far the main use for 'light' isolates is for running | |
172 * isolates that share access to the DOM. A special API will be added for this | |
173 * purpose soon. See the library top-level comments for more details. | |
174 */ | |
175 // TODO(sigmund): delete once we implement the new API in the vm | |
176 class Isolate { | |
177 | |
178 /** | |
179 * Redirects to [Isolate.light]. | |
180 */ | |
181 Isolate() : this.light(); | |
182 | |
183 /** | |
184 * Creates a new isolate-template for a light isolate. | |
185 */ | |
186 Isolate.light() : _isLight = true; | |
187 | |
188 /** | |
189 * Creates a new isolate-template for a heavy isolate. | |
190 */ | |
191 Isolate.heavy() : _isLight = false; | |
192 | |
193 /** | |
194 * Spawns a new isolate, using this instance as template. | |
195 * | |
196 * The new isolate lives in a new thread (for heavy templates) | |
197 * or in the same thread as the current isolate (for light templates), if | |
198 * possible. | |
199 * | |
200 * During the initialization of the new isolate a [ReceivePort] is created | |
201 * inside the new isolate and stored in the read-only field [port]. | |
202 * A corresponding [SendPort] is sent to the isolate that invoked [spawn]. | |
203 * Since spawning an isolate is an asynchronous operation this method returns | |
204 * a [Future] of this [SendPort]. | |
205 * | |
206 * A common pattern to instantiate new isolates is to enqueue the instructions | |
207 * using [Future.then]. | |
208 * [:myIsolate.spawn().then((SendPort port) { port.send('hi there'); });:] | |
209 */ | |
210 Future<SendPort> spawn() { | |
211 return _IsolateNatives.spawn(this, _isLight); | |
212 } | |
213 | |
214 // The private run method is invoked with the receive port. Before | |
215 // main is invoked we store the port in a field so it can be | |
216 // accessed from subclasses of Isolate. | |
217 void _run(ReceivePort port) { | |
218 _port = port; | |
219 main(); | |
220 } | |
221 | |
222 /** | |
223 * When [Isolate]s are used as entry-points, the [port] field contains a | |
224 * [ReceivePort]. The isolate that initiated the spawn holds a corresponding | |
225 * [SendPort]. | |
226 * | |
227 * Note that isolates should generally close their [ReceivePort]s when they | |
228 * are done, including this port. | |
229 */ | |
230 ReceivePort get port() { | |
231 return _port; | |
232 } | |
233 | |
234 /** | |
235 * When isolates are created, an instance of the template's class is | |
236 * instantiated in the new isolate. After the [port] has been set up, this | |
237 * [main] method is invoked on the instance. | |
238 */ | |
239 abstract void main(); | |
240 | |
241 final bool _isLight; | |
242 ReceivePort _port; | |
243 } | |
OLD | NEW |