| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 * The initial [ReceivePort] available by default for this isolate. This |
| 7 * [ReceivePort] is created automatically and it is commonly used to establish |
| 8 * the first communication between isolates (see [spawnFunction] and |
| 9 * [spawnUri]). |
| 10 */ |
| 11 ReceivePort get port() => _port; |
| 12 |
| 13 /** |
| 14 * Creates and spawns an isolate that shares the same code as the current |
| 15 * isolate, but that starts from [topLevelFunction]. The [topLevelFunction] |
| 16 * argument must be a static top-level function or a static method that takes no |
| 17 * arguments. It is illegal to pass a function closure. |
| 18 * |
| 19 * When any isolate starts (even the main script of the application), a default |
| 20 * [ReceivePort] is created for it. This port is available from the top-level |
| 21 * getter [port] defined in this library. |
| 22 * |
| 23 * [spawnFunction] returns a [SendPort] derived from the child isolate's default |
| 24 * port. |
| 25 * |
| 26 * See comments at the top of this library for more details. |
| 27 */ |
| 28 // Note this feature is not yet available in the dartvm. |
| 29 SendPort spawnFunction(void topLevelFunction()) { |
| 30 return _spawnFunction(topLevelFunction); |
| 31 } |
| 32 |
| 33 /** |
| 34 * Creates and spawns an isolate whose code is available at [uri]. Like with |
| 35 * [spawnFunction], the child isolate will have a default [ReceivePort], and a |
| 36 * this function returns a [SendPort] derived from it. |
| 37 * |
| 38 * See comments at the top of this library for more details. |
| 39 */ |
| 40 SendPort spawnUri(String uri) { |
| 41 return _spawnUri(uri); |
| 42 } |
| 43 |
| 44 /** |
| 6 * [SendPort]s are created from [ReceivePort]s. Any message sent through | 45 * [SendPort]s are created from [ReceivePort]s. Any message sent through |
| 7 * a [SendPort] is delivered to its respective [ReceivePort]. There might be | 46 * a [SendPort] is delivered to its respective [ReceivePort]. There might be |
| 8 * many [SendPort]s for the same [ReceivePort]. | 47 * many [SendPort]s for the same [ReceivePort]. |
| 9 * | 48 * |
| 10 * [SendPort]s can be transmitted to other isolates. | 49 * [SendPort]s can be transmitted to other isolates. |
| 11 */ | 50 */ |
| 12 interface SendPort extends Hashable { | 51 interface SendPort extends Hashable { |
| 13 | 52 |
| 14 /** | 53 /** |
| 15 * Sends an asynchronous [message] to this send port. The message is | 54 * Sends an asynchronous [message] to this send port. The message is |
| (...skipping 19 matching lines...) Expand all Loading... |
| 35 bool operator==(var other); | 74 bool operator==(var other); |
| 36 | 75 |
| 37 /** | 76 /** |
| 38 * Returns an immutable hash code for this send port that is | 77 * Returns an immutable hash code for this send port that is |
| 39 * consistent with the == operator. | 78 * consistent with the == operator. |
| 40 */ | 79 */ |
| 41 int hashCode(); | 80 int hashCode(); |
| 42 | 81 |
| 43 } | 82 } |
| 44 | 83 |
| 45 | |
| 46 /** | 84 /** |
| 47 * [ReceivePort]s, together with [SendPort]s, are the only means of | 85 * [ReceivePort]s, together with [SendPort]s, are the only means of |
| 48 * communication between isolates. [ReceivePort]s have a [:toSendPort:] method | 86 * communication between isolates. [ReceivePort]s have a [:toSendPort:] method |
| 49 * which returns a [SendPort]. Any message that is sent through this [SendPort] | 87 * which returns a [SendPort]. Any message that is sent through this [SendPort] |
| 50 * is delivered to the [ReceivePort] it has been created from. There, they are | 88 * is delivered to the [ReceivePort] it has been created from. There, they are |
| 51 * dispatched to the callback that has been registered on the receive port. | 89 * dispatched to the callback that has been registered on the receive port. |
| 52 * | 90 * |
| 53 * A [ReceivePort] may have many [SendPort]s. | 91 * A [ReceivePort] may have many [SendPort]s. |
| 54 */ | 92 */ |
| 55 interface ReceivePort default _ReceivePortFactory { | 93 interface ReceivePort default _ReceivePortFactory { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 85 | 123 |
| 86 /** | 124 /** |
| 87 * Creates a new send port that sends to this receive port. It is legal to | 125 * Creates a new send port that sends to this receive port. It is legal to |
| 88 * create several [SendPort]s from the same [ReceivePort]. | 126 * create several [SendPort]s from the same [ReceivePort]. |
| 89 */ | 127 */ |
| 90 SendPort toSendPort(); | 128 SendPort toSendPort(); |
| 91 | 129 |
| 92 } | 130 } |
| 93 | 131 |
| 94 /** | 132 /** |
| 133 * NOTE: This API will be deprecated soon. |
| 134 * |
| 95 * The [Isolate] class serves two purposes: (1) as template for spawning a new | 135 * The [Isolate] class serves two purposes: (1) as template for spawning a new |
| 96 * isolate, and (2) as entry-point for the newly spawned isolate. | 136 * isolate, and (2) as entry-point for the newly spawned isolate. |
| 97 * | 137 * |
| 98 * New isolates are spawned by sub-classing [Isolate] and then invoking | 138 * New isolates are spawned by sub-classing [Isolate] and then invoking |
| 99 * [:spawn:] on the instance. This will spawn a new isolate, which creates a | 139 * [:spawn:] on the instance. This will spawn a new isolate, which creates a |
| 100 * new instance of the class, initializes the instance's [port] field | 140 * new instance of the class, initializes the instance's [port] field |
| 101 * and invokes the instance method [main]. | 141 * and invokes the instance method [main]. |
| 102 * | 142 * |
| 103 * The new instance is created by invoking the default constructor of the | 143 * The new instance is created by invoking the default constructor of the |
| 104 * class that served as template for spawning the new isolate. This means, that | 144 * class that served as template for spawning the new isolate. This means, that |
| 105 * sub-classes must have a default constructor (i.e. no-argument constructor). | 145 * sub-classes must have a default constructor (i.e. no-argument constructor). |
| 106 * | 146 * |
| 107 * Isolates may be "heavy" or "light". Heavy isolates live in their own thread, | 147 * Isolates may be "heavy" or "light". Heavy isolates live in their own thread, |
| 108 * whereas "light" isolates live in the same thread as the isolate which spawned | 148 * whereas "light" isolates live in the same thread as the isolate which spawned |
| 109 * them. | 149 * them. |
| 150 * |
| 151 * NOTE: once [spawnFunction] and [spawnUri] are supported in both frog and the |
| 152 * dartvm, this class will be removed. The distinction of heavy and light will |
| 153 * be removed too. Thus far the main use for 'light' isolates is for running |
| 154 * isolates that share access to the DOM. A special API will be added for this |
| 155 * purpose soon. See the library top-level comments for more details. |
| 110 */ | 156 */ |
| 111 // TODO(sigmund): delete once we implement the new isolates in the vm | 157 // TODO(sigmund): delete once we implement the new API in the vm |
| 112 class Isolate { | 158 class Isolate { |
| 113 | 159 |
| 114 /** | 160 /** |
| 115 * Redirects to [Isolate.light]. | 161 * Redirects to [Isolate.light]. |
| 116 */ | 162 */ |
| 117 Isolate() : this.light(); | 163 Isolate() : this.light(); |
| 118 | 164 |
| 119 /** | 165 /** |
| 120 * Creates a new isolate-template for a light isolate. | 166 * Creates a new isolate-template for a light isolate. |
| 121 */ | 167 */ |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 /** | 216 /** |
| 171 * When isolates are created, an instance of the template's class is | 217 * When isolates are created, an instance of the template's class is |
| 172 * instantiated in the new isolate. After the [port] has been set up, this | 218 * instantiated in the new isolate. After the [port] has been set up, this |
| 173 * [main] method is invoked on the instance. | 219 * [main] method is invoked on the instance. |
| 174 */ | 220 */ |
| 175 abstract void main(); | 221 abstract void main(); |
| 176 | 222 |
| 177 final bool _isLight; | 223 final bool _isLight; |
| 178 ReceivePort _port; | 224 ReceivePort _port; |
| 179 } | 225 } |
| 180 | |
| 181 /** | |
| 182 * [Isolate2] provides APIs to spawn, communicate, and stop an isolate. An | |
| 183 * isolate can be spawned by simply creating a new instance of [Isolate2]. The | |
| 184 * [Isolate2] instance exposes a port to communicate with the isolate and | |
| 185 * methods to control its behavior remotely. | |
| 186 */ | |
| 187 // TODO(sigmund): rename to Isolate once we delete the old implementation | |
| 188 interface Isolate2 default _IsolateFactory { | |
| 189 | |
| 190 /** | |
| 191 * Create and spawn an isolate that shares the same code as the current | |
| 192 * isolate, but that starts from [topLevelFunction]. The [topLevelFunction] | |
| 193 * argument must be a static method closure that takes exactly one | |
| 194 * argument of type [ReceivePort]. It is illegal to pass a function closure | |
| 195 * that captures values in scope. | |
| 196 * | |
| 197 * When an child isolate is spawned, a new [ReceivePort] is created for it. | |
| 198 * This port is passed to [topLevelFunction]. A [SendPort] derived from | |
| 199 * such port is sent to the spawner isolate, which is accessible in | |
| 200 * [Isolate2.sendPort] field of this instance. | |
| 201 */ | |
| 202 Isolate2.fromCode(Function topLevelFunction); | |
| 203 | |
| 204 /** | |
| 205 * Create and spawn an isolate whose code is available at [uri]. | |
| 206 * The code in [uri] must have an method called [: isolateMain :], which takes | |
| 207 * exactly one argument of type [ReceivePort]. | |
| 208 * Like with [Isolate2.fromCode], a [ReceivePort] is created in the child | |
| 209 * isolate, and a [SendPort] to it is stored in [Isolate2.sendPort]. | |
| 210 */ | |
| 211 Isolate2.fromUri(String uri); | |
| 212 | |
| 213 /** Port used to communicate with this isolate. */ | |
| 214 SendPort sendPort; | |
| 215 } | |
| OLD | NEW |