Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: sdk/lib/isolate/isolate.dart

Issue 186403003: Add Isolate ping functionality. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Reword and add warning. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/_internal/lib/isolate_helper.dart ('k') | tests/isolate/ping_pause_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 /** Argument to `ping`: Ask for immediate response. */
33 static const int PING_ALIVE = 0;
34 /** Argument to `ping`: Ask for response after control events. */
35 static const int PING_CONTROL = 1;
36 /** Argument to `ping`: Ask for response after normal events. */
37 static const int PING_EVENT = 2;
38
32 /** 39 /**
33 * Control port used to send control messages to the isolate. 40 * Control port used to send control messages to the isolate.
34 * 41 *
35 * This class provides helper functions that sends control messages 42 * This class provides helper functions that sends control messages
36 * to the control port. 43 * to the control port.
37 */ 44 */
38 final SendPort controlPort; 45 final SendPort controlPort;
39 final Capability pauseCapability; 46 final Capability pauseCapability;
40 47
41 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]); 48 Isolate._fromControlPort(this.controlPort, [this.pauseCapability]);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 * Otherwise a new capability is created and returned. 104 * Otherwise a new capability is created and returned.
98 * 105 *
99 * If an isolate is paused more than once using the same capabilty, 106 * If an isolate is paused more than once using the same capabilty,
100 * only one resume with that capability is needed to end the pause. 107 * only one resume with that capability is needed to end the pause.
101 * 108 *
102 * If an isolate is paused using more than one capability, 109 * If an isolate is paused using more than one capability,
103 * they must all be individully ended before the isolate resumes. 110 * they must all be individully ended before the isolate resumes.
104 * 111 *
105 * Returns the capability that must be used to resume end the pause. 112 * Returns the capability that must be used to resume end the pause.
106 * 113 *
107 * WARNING: This method is not handled on any platform yet. 114 * WARNING: This method is not handled on all platforms yet.
108 */ 115 */
109 Capability pause([Capability resumeCapability]) { 116 Capability pause([Capability resumeCapability]) {
110 if (resumeCapability == null) resumeCapability = new Capability(); 117 if (resumeCapability == null) resumeCapability = new Capability();
111 var message = new List(3) 118 var message = new List(3)
112 ..[0] = "pause" 119 ..[0] = "pause"
113 ..[1] = pauseCapability 120 ..[1] = pauseCapability
114 ..[2] = resumeCapability; 121 ..[2] = resumeCapability;
115 controlPort.send(message); 122 controlPort.send(message);
116 return resumeCapability; 123 return resumeCapability;
117 } 124 }
118 125
119 /** 126 /**
120 * Resumes a paused isolate. 127 * Resumes a paused isolate.
121 * 128 *
122 * Sends a message to an isolate requesting that it ends a pause 129 * Sends a message to an isolate requesting that it ends a pause
123 * that was requested using the [resumeCapability]. 130 * that was requested using the [resumeCapability].
124 * 131 *
125 * The capability must be one returned by a call to [pause] on this 132 * The capability must be one returned by a call to [pause] on this
126 * isolate, otherwise the resume call does nothing. 133 * isolate, otherwise the resume call does nothing.
127 * 134 *
128 * WARNING: This method is not handled on any platform yet. 135 * WARNING: This method is not handled on all platforms yet.
129 */ 136 */
130 void resume(Capability resumeCapability) { 137 void resume(Capability resumeCapability) {
131 var message = new List(2) 138 var message = new List(2)
132 ..[0] = "resume" 139 ..[0] = "resume"
133 ..[1] = resumeCapability; 140 ..[1] = resumeCapability;
134 controlPort.send(message); 141 controlPort.send(message);
135 } 142 }
143
144 /**
145 * Request that the isolate send a response on the [responsePort].
146 *
147 * If the isolate is alive, it will eventually send a `null` response on
148 * the response port.
149 *
150 * The [pingType] must be one of [PING_ALIVE], [PING_CONTROL] or [PING_EVENT].
151 * The response is sent at different times depending on the ping type:
152 *
153 * * `PING_ALIVE`: The the isolate responds as soon as possible.
Anders Johnsen 2014/03/04 14:06:20 The the
154 * The response should happen no sooner than if sent with `PING_CONTROL`.
155 * It may be sent earlier of the system has a way to do so.
Anders Johnsen 2014/03/04 14:06:20 of -> if
156 * * `PING_CONTROL`: The response it not sent until all previously sent
157 * control messages from the current isolate to the receiving isolate
158 * have been processed. This can be used to wait for
159 * previously sent control messages.
160 * * `PING_EVENT`: The response is not sent until all prevously sent
161 * non-control messages from the current isolate to the receiving isolate
162 * have been processed.
163 * The ping effectively puts the resonse into the normal event queue after
164 * previously sent messages.
165 * This can be used to wait for a another event to be processed.
166 *
167 * WARNING: This method is not handled on all platforms yet.
168 */
169 void ping(SendPort responsePort, [int pingType = PING_ALIVE]) {
170 var message = new List(3)
171 ..[0] = "ping"
172 ..[1] = responsePort
173 ..[2] = pingType;
174 controlPort.send(message);
175 }
136 } 176 }
137 177
138 /** 178 /**
139 * Sends messages to its [ReceivePort]s. 179 * Sends messages to its [ReceivePort]s.
140 * 180 *
141 * [SendPort]s are created from [ReceivePort]s. Any message sent through 181 * [SendPort]s are created from [ReceivePort]s. Any message sent through
142 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be 182 * a [SendPort] is delivered to its corresponding [ReceivePort]. There might be
143 * many [SendPort]s for the same [ReceivePort]. 183 * many [SendPort]s for the same [ReceivePort].
144 * 184 *
145 * [SendPort]s can be transmitted to other isolates. 185 * [SendPort]s can be transmitted to other isolates.
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 const _IsolateUnhandledException(this.message, this.source, this.stackTrace); 331 const _IsolateUnhandledException(this.message, this.source, this.stackTrace);
292 332
293 String toString() { 333 String toString() {
294 return 'IsolateUnhandledException: exception while handling message: ' 334 return 'IsolateUnhandledException: exception while handling message: '
295 '${message} \n ' 335 '${message} \n '
296 '${source.toString().replaceAll("\n", "\n ")}\n' 336 '${source.toString().replaceAll("\n", "\n ")}\n'
297 'original stack trace:\n ' 337 'original stack trace:\n '
298 '${stackTrace.toString().replaceAll("\n","\n ")}'; 338 '${stackTrace.toString().replaceAll("\n","\n ")}';
299 } 339 }
300 } 340 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/lib/isolate_helper.dart ('k') | tests/isolate/ping_pause_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698