| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// A multiplexing [RawReceivePort]. | 5 /// A multiplexing [RawReceivePort]. |
| 6 /// | 6 /// |
| 7 /// Allows creating a number of [RawReceivePort] implementations that all send | 7 /// Allows creating a number of [RawReceivePort] implementations that all send |
| 8 /// messages through the same real `RawReceivePort`. | 8 /// messages through the same real `RawReceivePort`. |
| 9 /// | 9 /// |
| 10 /// This allows reducing the number of receive ports created, but adds an | 10 /// This allows reducing the number of receive ports created, but adds an |
| 11 /// overhead to each message. | 11 /// overhead to each message. |
| 12 /// If a library creates many short-lived receive ports, multiplexing might be | 12 /// If a library creates many short-lived receive ports, multiplexing might be |
| 13 /// faster. | 13 /// faster. |
| 14 /// | 14 /// |
| 15 /// To use multiplexing receive ports, create and store a | 15 /// To use multiplexing receive ports, create and store a |
| 16 /// [RawReceivePortMultiplexer], and create receive ports by calling | 16 /// [RawReceivePortMultiplexer], and create receive ports by calling |
| 17 /// `multiplexer.createRawReceivePort(handler)` where you would otherwise | 17 /// `multiplexer.createRawReceivePort(handler)` where you would otherwise |
| 18 /// write `new RawReceivePort(handler)`. | 18 /// write `new RawReceivePort(handler)`. |
| 19 /// | 19 /// |
| 20 /// Remember to [close] the multiplexer when it is no longer needed. | 20 /// Remember to [close] the multiplexer when it is no longer needed. |
| 21 /// ` | 21 /// |
| 22 /// (TODO: Check if it really is faster - creating a receive port requires a | 22 /// (TODO: Check if it really is faster - creating a receive port requires a |
| 23 /// global mutex, so it may be a bottleneck, but it's not clear how slow it is). | 23 /// global mutex, so it may be a bottleneck, but it's not clear how slow it is). |
| 24 library pkg.isolate.multiplexreceiveport; | 24 library isolate.raw_receive_port_multiplexer; |
| 25 | 25 |
| 26 import "dart:isolate"; | 26 import 'dart:collection'; |
| 27 import "dart:collection"; | 27 import 'dart:isolate'; |
| 28 import "lists.dart"; | 28 |
| 29 import 'lists.dart'; |
| 29 | 30 |
| 30 class _MultiplexRawReceivePort implements RawReceivePort { | 31 class _MultiplexRawReceivePort implements RawReceivePort { |
| 31 final RawReceivePortMultiplexer _multiplexer; | 32 final RawReceivePortMultiplexer _multiplexer; |
| 32 final int _id; | 33 final int _id; |
| 33 Function _handler; | 34 Function _handler; |
| 34 | 35 |
| 35 _MultiplexRawReceivePort(this._multiplexer, this._id, this._handler); | 36 _MultiplexRawReceivePort(this._multiplexer, this._id, this._handler); |
| 36 | 37 |
| 37 void set handler(void handler(response)) { | 38 void set handler(void handler(response)) { |
| 38 this._handler = handler; | 39 this._handler = handler; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 93 } |
| 93 | 94 |
| 94 SendPort _createSendPort(int id) { | 95 SendPort _createSendPort(int id) { |
| 95 return new _MultiplexSendPort(id, _port.sendPort); | 96 return new _MultiplexSendPort(id, _port.sendPort); |
| 96 } | 97 } |
| 97 | 98 |
| 98 void _closePort(int id) { | 99 void _closePort(int id) { |
| 99 _map.remove(id); | 100 _map.remove(id); |
| 100 } | 101 } |
| 101 } | 102 } |
| OLD | NEW |