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 work by sending messages | |
36 * to the control port. | |
37 * | |
38 * The accepted messages of a control port are all lists containing | |
floitsch
2014/02/04 12:39:25
I would prefer not to expose the protocol just yet
Lasse Reichstein Nielsen
2014/02/04 14:02:29
I thought we were going to expose the control port
floitsch
2014/02/04 16:57:49
If we are certain that this is the protocol we wan
| |
39 * a string identifying the request, and optionally a number of parameters. | |
40 * | |
41 * NOTICE: The formats of these messages are tentative. | |
42 * | |
43 * * Pause: `["pause", pauseCapability, resumeCapability]`. | |
44 * * Resume: `["resume", resumeCapability]`. | |
45 * | |
46 */ | |
47 final SendPort controlPort; | |
48 final Capability pauseCapability; | |
32 | 49 |
33 final SendPort _controlPort; | 50 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]); |
Lasse Reichstein Nielsen
2014/02/04 14:02:29
Should eventually be public.
| |
34 | |
35 Isolate._fromControlPort(SendPort controlPort) | |
36 : this._controlPort = controlPort; | |
37 | 51 |
38 /** | 52 /** |
39 * Creates and spawns an isolate that shares the same code as the current | 53 * Creates and spawns an isolate that shares the same code as the current |
40 * isolate. | 54 * isolate. |
41 * | 55 * |
42 * The argument [entryPoint] specifies the entry point of the spawned | 56 * The argument [entryPoint] specifies the entry point of the spawned |
43 * isolate. It must be a top-level function or a static method that | 57 * 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 | 58 * takes one argument - that is, one-parameter functions that can be |
45 * compile-time constant function values. | 59 * compile-time constant function values. |
46 * It is not allowed to pass the value of function expressions or an instance | 60 * 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)` | 83 * * `main(args, message)` |
70 * | 84 * |
71 * When present, the parameter `args` is set to the provided [args] list. | 85 * When present, the parameter `args` is set to the provided [args] list. |
72 * When present, the parameter `message` is set to the initial [message]. | 86 * When present, the parameter `message` is set to the initial [message]. |
73 * | 87 * |
74 * Returns a future that will complete with an [Isolate] instance if the | 88 * Returns a future that will complete with an [Isolate] instance if the |
75 * spawning succeeded. It will complete with an error otherwise. | 89 * spawning succeeded. It will complete with an error otherwise. |
76 */ | 90 */ |
77 external static Future<Isolate> spawnUri( | 91 external static Future<Isolate> spawnUri( |
78 Uri uri, List<String> args, var message); | 92 Uri uri, List<String> args, var message); |
93 | |
94 | |
95 /** | |
96 * Requests the isolate to pause. | |
97 * | |
98 * The isolate should stop handling events by pausing its event queue. | |
99 * The request will eventually make the isolate stop doing anything. | |
100 * It will be handled before any other messages sent to the isolate from | |
101 * the current isolate, but no other guarantees are provided. | |
102 * | |
103 * If [resumeCapability] is provided, it is used to identity the pause, | |
104 * and must be used again to end the pause using [resume]. | |
105 * Otherwise a new capability is created and returned. | |
106 * | |
107 * If an isolate is paused more than once using the same capabilty, | |
108 * only one resume with that capability is needed to end the pause. | |
109 * | |
110 * If an isolate is paused using more than one capability, | |
111 * they must all be individully ended before the isolate resumes. | |
112 * | |
113 * Returns the capability that must be used to resume end the pause. | |
114 * | |
115 * WARNING: This method is not handled on any platform yet. | |
116 */ | |
117 Capability pause([Capability resumeCapability]) { | |
118 if (resumeCapability == null) resumeCapability = new Capability(); | |
119 var message = new List(3) | |
120 ..[0] = "pause" | |
121 ..[1] = pauseCapability | |
122 ..[2] = resumeCapability; | |
123 controlPort.send(message); | |
124 return resumeCapability; | |
125 } | |
126 | |
127 /** | |
128 * Resumes a paused isolate. | |
129 * | |
130 * Sends a message to an isolate requresting that it ends a pause | |
floitsch
2014/02/04 12:39:25
requesting
| |
131 * that was requested using the [resumeCapability]. | |
132 * The resume will happen eventually, but not necessarily right away. | |
floitsch
2014/02/04 12:39:25
Remove that line.
| |
133 * | |
134 * The capability must be one returned by a call to [pause] on this | |
135 * isolate, otherwise the resume call does nothing. | |
136 * | |
137 * WARNING: This method is not handled on any platform yet. | |
138 */ | |
139 void resume(Capability resumeCapability) { | |
140 var message = new List(2) | |
141 ..[0] = "resume" | |
142 ..[1] = resumeCapability; | |
143 controlPort.send(message); | |
144 } | |
79 } | 145 } |
80 | 146 |
81 /** | 147 /** |
82 * Sends messages to its [ReceivePort]s. | 148 * Sends messages to its [ReceivePort]s. |
83 * | 149 * |
84 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 150 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
85 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be | 151 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be |
86 * many [SendPort]s for the same [ReceivePort]. | 152 * many [SendPort]s for the same [ReceivePort]. |
87 * | 153 * |
88 * [SendPort]s can be transmitted to other isolates. | 154 * [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); | 300 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); |
235 | 301 |
236 String toString() { | 302 String toString() { |
237 return 'IsolateUnhandledException: exception while handling message: ' | 303 return 'IsolateUnhandledException: exception while handling message: ' |
238 '${message} \n ' | 304 '${message} \n ' |
239 '${source.toString().replaceAll("\n", "\n ")}\n' | 305 '${source.toString().replaceAll("\n", "\n ")}\n' |
240 'original stack trace:\n ' | 306 'original stack trace:\n ' |
241 '${stackTrace.toString().replaceAll("\n","\n ")}'; | 307 '${stackTrace.toString().replaceAll("\n","\n ")}'; |
242 } | 308 } |
243 } | 309 } |
OLD | NEW |