OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 // Dart core library. | |
6 // TODO(sigmund): move to dart:isolate | |
7 | |
8 /** | |
9 * [SendPort]s are created from [ReceivePort]s. Any message sent through | |
10 * a [SendPort] is delivered to its respective [ReceivePort]. There might be | |
11 * many [SendPort]s for the same [ReceivePort]. | |
12 * | |
13 * [SendPort]s can be transmitted to other isolates. | |
14 */ | |
15 interface SendPort extends Hashable { | |
16 | |
17 /** | |
18 * Sends an asynchronous [message] to this send port. The message is | |
19 * copied to the receiving isolate. If the message contains any | |
20 * receive ports, they are translated to the corresponding send port | |
21 * before being transmitted. If specified, the [replyTo] port will be | |
22 * provided to the receiver to facilitate exchanging sequences of | |
23 * messages. | |
24 */ | |
25 void send(var message, [SendPort replyTo]); | |
26 | |
27 /** | |
28 * Creates a new single-shot receive port, sends a message to this | |
29 * send port with replyTo set to the opened port, and returns the | |
30 * receive port. | |
31 */ | |
32 ReceivePort call(var message); | |
33 | |
34 /** | |
35 * Tests whether [other] is a [SendPort] pointing to the same | |
36 * [ReceivePort] as this one. | |
37 */ | |
38 bool operator==(var other); | |
39 | |
40 /** | |
41 * Returns an immutable hash code for this send port that is | |
42 * consistent with the == operator. | |
43 */ | |
44 int hashCode(); | |
45 | |
46 } | |
47 | |
48 | |
49 /** | |
50 * [ReceivePort]s, together with [SendPort]s, are the only means of | |
51 * communication between isolates. [ReceivePort]s have a [:toSendPort:] method | |
52 * which returns a [SendPort]. Any message that is sent through this [SendPort] | |
53 * is delivered to the [ReceivePort] it has been created from. There, they are | |
54 * dispatched to the callback that has been registered on the receive port. | |
55 * | |
56 * A [ReceivePort] may have many [SendPort]s. | |
57 */ | |
58 interface ReceivePort default ReceivePortFactory { | |
59 | |
60 /** | |
61 * Opens a long-lived port for receiving messages. The returned port | |
62 * must be explicitly closed through [ReceivePort.close]. | |
63 */ | |
64 ReceivePort(); | |
65 | |
66 /** | |
67 * Opens a single-shot reply port. Once a message has been received | |
68 * on this port, it is automatically closed -- obviously without | |
69 * throwing the message away before it can be processed. This | |
70 * constructor is used indirectly through [SendPort.call]. | |
71 */ | |
72 ReceivePort.singleShot(); | |
73 | |
74 /** | |
75 * Sets up a callback function for receiving pending or future | |
76 * messages on this receive port. | |
77 */ | |
78 void receive(void callback(var message, SendPort replyTo)); | |
79 | |
80 /** | |
81 * Closes this receive port immediately. Pending messages will not | |
82 * be processed and it is impossible to re-open the port. Single-shot | |
83 * reply ports, such as those created through [SendPort.call], are | |
84 * automatically closed when the reply has been received. Multiple | |
85 * invocations of [close] are allowed but ignored. | |
86 */ | |
87 void close(); | |
88 | |
89 /** | |
90 * Creates a new send port that sends to this receive port. It is legal to | |
91 * create several [SendPort]s from the same [ReceivePort]. | |
92 */ | |
93 SendPort toSendPort(); | |
94 | |
95 } | |
96 | |
97 /** | |
98 * The [Isolate] class serves two purposes: (1) as template for spawning a new | |
99 * isolate, and (2) as entry-point for the newly spawned isolate. | |
100 * | |
101 * New isolates are spawned by sub-classing [Isolate] and then invoking | |
102 * [:spawn:] on the instance. This will spawn a new isolate, which creates a | |
103 * new instance of the class, initializes the instance's [port] field | |
104 * and invokes the instance method [main]. | |
105 * | |
106 * The new instance is created by invoking the default constructor of the | |
107 * class that served as template for spawning the new isolate. This means, that | |
108 * sub-classes must have a default constructor (i.e. no-argument constructor). | |
109 * | |
110 * Isolates may be "heavy" or "light". Heavy isolates live in their own thread, | |
111 * whereas "light" isolates live in the same thread as the isolate which spawned | |
112 * them. | |
113 */ | |
114 // TODO(sigmund): delete once we implement the new isolates in the vm | |
115 class Isolate { | |
116 | |
117 /** | |
118 * Redirects to [Isolate.light]. | |
119 */ | |
120 Isolate() : this.light(); | |
121 | |
122 /** | |
123 * Creates a new isolate-template for a light isolate. | |
124 */ | |
125 Isolate.light() : _isLight = true; | |
126 | |
127 /** | |
128 * Creates a new isolate-template for a heavy isolate. | |
129 */ | |
130 Isolate.heavy() : _isLight = false; | |
131 | |
132 /** | |
133 * Spawns a new isolate, using this instance as template. | |
134 * | |
135 * The new isolate lives in a new thread (for heavy templates) | |
136 * or in the same thread as the current isolate (for light templates), if | |
137 * possible. | |
138 * | |
139 * During the initialization of the new isolate a [ReceivePort] is created | |
140 * inside the new isolate and stored in the read-only field [port]. | |
141 * A corresponding [SendPort] is sent to the isolate that invoked [spawn]. | |
142 * Since spawning an isolate is an asynchronous operation this method returns | |
143 * a [Future] of this [SendPort]. | |
144 * | |
145 * A common pattern to instantiate new isolates is to enqueue the instructions | |
146 * using [Future.then]. | |
147 * [:myIsolate.spawn().then((SendPort port) { port.send('hi there'); });:] | |
148 */ | |
149 Future<SendPort> spawn() { | |
150 return IsolateNatives.spawn(this, _isLight); | |
151 } | |
152 | |
153 // The private run method is invoked with the receive port. Before | |
154 // main is invoked we store the port in a field so it can be | |
155 // accessed from subclasses of Isolate. | |
156 void _run(ReceivePort port) { | |
157 _port = port; | |
158 main(); | |
159 } | |
160 | |
161 /** | |
162 * When [Isolate]s are used as entry-points, the [port] field contains a | |
163 * [ReceivePort]. The isolate that initiated the spawn holds a corresponding | |
164 * [SendPort]. | |
165 * | |
166 * Note that isolates should generally close their [ReceivePort]s when they | |
167 * are done, including this port. | |
168 */ | |
169 ReceivePort get port() { | |
170 return _port; | |
171 } | |
172 | |
173 /** | |
174 * When isolates are created, an instance of the template's class is | |
175 * instantiated in the new isolate. After the [port] has been set up, this | |
176 * [main] method is invoked on the instance. | |
177 */ | |
178 abstract void main(); | |
179 | |
180 final bool _isLight; | |
181 ReceivePort _port; | |
182 } | |
OLD | NEW |