| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// A multiplexing [RawReceivePort]. |
| 6 /// |
| 7 /// Allows creating a number of [RawReceivePort] implementations that all send |
| 8 /// messages through the same real `RawReceivePort`. |
| 9 /// |
| 10 /// This allows reducing the number of receive ports created, but adds an |
| 11 /// overhead to each message. |
| 12 /// If a library creates many short-lived receive ports, multiplexing might be |
| 13 /// faster. |
| 14 /// |
| 15 /// To use multiplexing receive ports, create and store a |
| 16 /// [RawReceivePortMultiplexer], and create receive ports by calling |
| 17 /// `multiplexer.createRawReceivePort(handler)` where you would otherwise |
| 18 /// write `new RawReceivePort(handler)`. |
| 19 /// |
| 20 /// Remember to [close] the multiplexer when it is no longer needed. |
| 21 /// |
| 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). |
| 24 library isolate.raw_receive_port_multiplexer; |
| 25 |
| 26 import 'dart:collection'; |
| 27 import 'dart:isolate'; |
| 28 |
| 29 import 'lists.dart'; |
| 30 |
| 31 class _MultiplexRawReceivePort implements RawReceivePort { |
| 32 final RawReceivePortMultiplexer _multiplexer; |
| 33 final int _id; |
| 34 Function _handler; |
| 35 |
| 36 _MultiplexRawReceivePort(this._multiplexer, this._id, this._handler); |
| 37 |
| 38 void set handler(Function handler) { |
| 39 this._handler = handler; |
| 40 } |
| 41 |
| 42 void close() { |
| 43 _multiplexer._closePort(_id); |
| 44 } |
| 45 |
| 46 SendPort get sendPort => _multiplexer._createSendPort(_id); |
| 47 |
| 48 void _invokeHandler(message) { |
| 49 _handler(message); |
| 50 } |
| 51 } |
| 52 |
| 53 class _MultiplexSendPort implements SendPort { |
| 54 final SendPort _sendPort; |
| 55 final int _id; |
| 56 _MultiplexSendPort(this._id, this._sendPort); |
| 57 |
| 58 void send(message) { |
| 59 _sendPort.send(list2(_id, message)); |
| 60 } |
| 61 } |
| 62 |
| 63 /// A shared [RawReceivePort] that distributes messages to |
| 64 /// [RawReceivePort] instances that it manages. |
| 65 class RawReceivePortMultiplexer { |
| 66 final RawReceivePort _port = new RawReceivePort(); |
| 67 final Map<int, _MultiplexRawReceivePort> _map = new HashMap(); |
| 68 int _nextId = 0; |
| 69 |
| 70 RawReceivePortMultiplexer() { |
| 71 _port.handler = _multiplexResponse; |
| 72 } |
| 73 |
| 74 RawReceivePort createRawReceivePort([void handler(value)]) { |
| 75 int id = _nextId++; |
| 76 var result = new _MultiplexRawReceivePort(this, id, handler); |
| 77 _map[id] = result; |
| 78 return result; |
| 79 } |
| 80 |
| 81 void close() { |
| 82 _port.close(); |
| 83 } |
| 84 |
| 85 void _multiplexResponse(list) { |
| 86 int id = list[0]; |
| 87 var message = list[1]; |
| 88 _MultiplexRawReceivePort receivePort = _map[id]; |
| 89 // If the receive port is closed, messages are dropped, just as for |
| 90 // the normal ReceivePort. |
| 91 if (receivePort == null) return; // Port closed. |
| 92 receivePort._invokeHandler(message); |
| 93 } |
| 94 |
| 95 SendPort _createSendPort(int id) { |
| 96 return new _MultiplexSendPort(id, _port.sendPort); |
| 97 } |
| 98 |
| 99 void _closePort(int id) { |
| 100 _map.remove(id); |
| 101 } |
| 102 } |
| OLD | NEW |