Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: lib/src/multiplexport.dart

Issue 928663003: Add IsolateRunner as a helper around Isolate. (Closed) Base URL: https://github.com/dart-lang/isolate.git@master
Patch Set: Add .status. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/lists.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 /**
6 * A multiplexing [RawReceivePort].
7 *
8 * Allows creating a number of [RawReceivePort] implementations that all send
9 * messages through the same real `RawReceivePort`.
10 *
11 * This allows reducing the number of receive ports created, but adds an
12 * overhead to each message.
13 * If a library creates many short-lived receive ports, multiplexing might be
14 * faster.
15 *
16 * To use multiplexing receive ports, create and store a
17 * [RawReceivePortMultiplexer], and create receive ports by calling
18 * `multiplexer.createRawReceivePort(handler)` where you would otherwise
19 * write `new RawReceivePort(handler)`.
20 *
21 * Remember to [close] the multiplexer when it is no longer needed.
22 * `
23 * (TODO: Check if it really is faster - creating a receive port requires a
24 * global mutex, so it may be a bottleneck, but it's not clear how slow it is).
25 */
26 library pkg.isolate.multiplexreceiveport;
27
28 import "dart:isolate";
29 import "dart:collection";
30 import "lists.dart";
31
32 class _MultiplexRawReceivePort implements RawReceivePort {
33 final RawReceivePortMultiplexer _multiplexer;
34 final int _id;
35 Function _handler;
36
37 _MultiplexRawReceivePort(this._multiplexer, this._id, this._handler);
38
39 void set handler(void handler(response)) {
40 this._handler = handler;
41 }
42
43 void close() {
44 _multiplexer._closePort(_id);
45 }
46
47 SendPort get sendPort => _multiplexer._createSendPort(_id);
48
49 void _invokeHandler(message) { _handler(message); }
50 }
51
52 class _MultiplexSendPort implements SendPort {
53 final SendPort _sendPort;
54 final int _id;
55 _MultiplexSendPort(this._id, this._sendPort);
56
57 void send(message) {
58 _sendPort.send(list2(_id, message));
59 }
60 }
61
62 /**
63 * A shared [RawReceivePort] that distributes messages to
64 * [RawReceivePort] instances that it manages.
65 */
66 class RawReceivePortMultiplexer {
67 final RawReceivePort _port = new RawReceivePort();
68 final Map<int, _MultiplexRawReceivePort> _map = new HashMap();
69 int _nextId = 0;
70
71 RawReceivePortMultiplexer() {
72 _port.handler = _multiplexResponse;
73 }
74
75 RawReceivePort createRawReceivePort([void handler(value)]) {
76 int id = _nextId++;
77 var result = new _MultiplexRawReceivePort(this, id, handler);
78 _map[id] = result;
79 return result;
80 }
81
82 void close() {
83 _port.close();
84 }
85
86 void _multiplexResponse(list) {
87 int id = list[0];
88 var message = list[1];
89 _MultiplexRawReceivePort receivePort = _map[id];
90 // If the receive port is closed, messages are dropped, just as for
91 // the normal ReceivePort.
92 if (receivePort == null) return; // Port closed.
93 receivePort._invokeHandler(message);
94 }
95
96 SendPort _createSendPort(int id) {
97 return new _MultiplexSendPort(id, _port.sendPort);
98 }
99
100 void _closePort(int id) {
101 _map.remove(id);
102 }
103 }
OLDNEW
« no previous file with comments | « lib/src/lists.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698