| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class IsolateSpawnException implements Exception { | |
| 6 const IsolateSpawnException(String this._s); | |
| 7 String toString() => "IsolateSpawnException: '$_s'"; | |
| 8 final String _s; | |
| 9 } | |
| 10 | |
| 11 /** | |
| 12 * The initial [ReceivePort] available by default for this isolate. This | |
| 13 * [ReceivePort] is created automatically and it is commonly used to establish | |
| 14 * the first communication between isolates (see [spawnFunction] and | |
| 15 * [spawnUri]). | |
| 16 */ | |
| 17 ReceivePort get port() => _port; | |
| 18 | |
| 19 /** | |
| 20 * Creates and spawns an isolate that shares the same code as the current | |
| 21 * isolate, but that starts from [topLevelFunction]. The [topLevelFunction] | |
| 22 * argument must be a static top-level function or a static method that takes no | |
| 23 * arguments. It is illegal to pass a function closure. | |
| 24 * | |
| 25 * When any isolate starts (even the main script of the application), a default | |
| 26 * [ReceivePort] is created for it. This port is available from the top-level | |
| 27 * getter [port] defined in this library. | |
| 28 * | |
| 29 * [spawnFunction] returns a [SendPort] derived from the child isolate's default | |
| 30 * port. | |
| 31 * | |
| 32 * See comments at the top of this library for more details. | |
| 33 */ | |
| 34 // Note this feature is not yet available in the dartvm. | |
| 35 SendPort spawnFunction(void topLevelFunction()) { | |
| 36 return _spawnFunction(topLevelFunction); | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Creates and spawns an isolate whose code is available at [uri]. Like with | |
| 41 * [spawnFunction], the child isolate will have a default [ReceivePort], and a | |
| 42 * this function returns a [SendPort] derived from it. | |
| 43 * | |
| 44 * See comments at the top of this library for more details. | |
| 45 */ | |
| 46 SendPort spawnUri(String uri) { | |
| 47 return _spawnUri(uri); | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * [SendPort]s are created from [ReceivePort]s. Any message sent through | |
| 52 * a [SendPort] is delivered to its respective [ReceivePort]. There might be | |
| 53 * many [SendPort]s for the same [ReceivePort]. | |
| 54 * | |
| 55 * [SendPort]s can be transmitted to other isolates. | |
| 56 */ | |
| 57 interface SendPort extends Hashable { | |
| 58 | |
| 59 /** | |
| 60 * Sends an asynchronous [message] to this send port. The message is copied to | |
| 61 * the receiving isolate. If specified, the [replyTo] port will be provided to | |
| 62 * the receiver to facilitate exchanging sequences of messages. | |
| 63 * | |
| 64 * The content of [message] can be: primitive values (null, num, bool, double, | |
| 65 * String), instances of [SendPort], and lists and maps whose elements are any | |
| 66 * of these. List and maps are also allowed to be cyclic. | |
| 67 * | |
| 68 * In the special circumstances when two isolates share the same code and are | |
| 69 * running in the same process (e.g. isolates created via [spawnFunction]), it | |
| 70 * is also possible to send object instances (which would be copied in the | |
| 71 * process). This is currently only supported by the dartvm. For now, the | |
| 72 * frog compiler only supports the restricted messages described above. | |
| 73 * | |
| 74 * Deprecation note: it is no longer valid to transmit a [ReceivePort] in a | |
| 75 * message. Previously they were translated to the corresponding send port | |
| 76 * before being transmitted. | |
| 77 */ | |
| 78 void send(var message, [SendPort replyTo]); | |
| 79 | |
| 80 /** | |
| 81 * Sends a message to this send port and returns a [Future] of the reply. | |
| 82 * Basically, this internally creates a new receive port, sends a | |
| 83 * message to this send port with replyTo set to such receive port, and, when | |
| 84 * a reply is received, it closes the receive port and completes the returned | |
| 85 * future. | |
| 86 */ | |
| 87 Future call(var message); | |
| 88 | |
| 89 /** | |
| 90 * Tests whether [other] is a [SendPort] pointing to the same | |
| 91 * [ReceivePort] as this one. | |
| 92 */ | |
| 93 bool operator==(var other); | |
| 94 | |
| 95 /** | |
| 96 * Returns an immutable hash code for this send port that is | |
| 97 * consistent with the == operator. | |
| 98 */ | |
| 99 int hashCode(); | |
| 100 | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * [ReceivePort]s, together with [SendPort]s, are the only means of | |
| 105 * communication between isolates. [ReceivePort]s have a [:toSendPort:] method | |
| 106 * which returns a [SendPort]. Any message that is sent through this [SendPort] | |
| 107 * is delivered to the [ReceivePort] it has been created from. There, they are | |
| 108 * dispatched to the callback that has been registered on the receive port. | |
| 109 * | |
| 110 * A [ReceivePort] may have many [SendPort]s. | |
| 111 */ | |
| 112 interface ReceivePort default _ReceivePortFactory { | |
| 113 | |
| 114 /** | |
| 115 * Opens a long-lived port for receiving messages. The returned port | |
| 116 * must be explicitly closed through [ReceivePort.close]. | |
| 117 */ | |
| 118 ReceivePort(); | |
| 119 | |
| 120 /** | |
| 121 * Sets up a callback function for receiving pending or future | |
| 122 * messages on this receive port. | |
| 123 */ | |
| 124 void receive(void callback(var message, SendPort replyTo)); | |
| 125 | |
| 126 /** | |
| 127 * Closes this receive port immediately. Pending messages will not | |
| 128 * be processed and it is impossible to re-open the port. Single-shot | |
| 129 * reply ports, such as those created through [SendPort.call], are | |
| 130 * automatically closed when the reply has been received. Multiple | |
| 131 * invocations of [close] are allowed but ignored. | |
| 132 */ | |
| 133 void close(); | |
| 134 | |
| 135 /** | |
| 136 * Creates a new send port that sends to this receive port. It is legal to | |
| 137 * create several [SendPort]s from the same [ReceivePort]. | |
| 138 */ | |
| 139 SendPort toSendPort(); | |
| 140 | |
| 141 } | |
| 142 | |
| 143 // TODO(kasperl): Make this hashable and document it. | |
| 144 interface SendPortSync { | |
| 145 | |
| 146 callSync(var message); | |
| 147 | |
| 148 } | |
| OLD | NEW |