| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 patch class _IOService { | 5 patch class _IOService { |
| 6 // Lazy initialize service ports, 32 per isolate. | 6 // Lazy initialize service ports, 32 per isolate. |
| 7 static const int _SERVICE_PORT_COUNT = 32; | 7 static const int _SERVICE_PORT_COUNT = 32; |
| 8 static List<SendPort> _servicePort = new List(_SERVICE_PORT_COUNT); | 8 static List<SendPort> _servicePort = new List(_SERVICE_PORT_COUNT); |
| 9 static RawReceivePort _receivePort; | 9 static RawReceivePort _receivePort; |
| 10 static SendPort _replyToPort; | 10 static SendPort _replyToPort; |
| 11 static Map<int, Completer> _messageMap = {}; | 11 static Map<int, Completer> _messageMap = {}; |
| 12 static int _id = 0; | 12 static int _id = 0; |
| 13 | 13 |
| 14 /* patch */ static Future dispatch(int request, List data) { | 14 /* patch */ static Future dispatch(int request, List data) { |
| 15 int id; | 15 int id; |
| 16 do { | 16 do { |
| 17 id = _getNextId(); | 17 id = _getNextId(); |
| 18 } while (_messageMap.containsKey(id)); | 18 } while (_messageMap.containsKey(id)); |
| 19 int index = id % _SERVICE_PORT_COUNT; | 19 int index = id % _SERVICE_PORT_COUNT; |
| 20 _initialize(index); | 20 _initialize(index); |
| 21 var completer = new Completer(); | 21 var completer = new Completer(); |
| 22 _messageMap[id] = completer; | 22 _messageMap[id] = completer; |
| 23 _servicePort[index].send([id, _replyToPort, request, data]); | 23 try { |
| 24 _servicePort[index].send([id, _replyToPort, request, data]); |
| 25 } catch (error) { |
| 26 _messageMap.remove(id).complete(error); |
| 27 if (_messageMap.length == 0) { |
| 28 _finalize(); |
| 29 } |
| 30 } |
| 24 return completer.future; | 31 return completer.future; |
| 25 } | 32 } |
| 26 | 33 |
| 27 static void _initialize(int index) { | 34 static void _initialize(int index) { |
| 28 if (_servicePort[index] == null) { | 35 if (_servicePort[index] == null) { |
| 29 _servicePort[index] = _newServicePort(); | 36 _servicePort[index] = _newServicePort(); |
| 30 } | 37 } |
| 31 if (_receivePort == null) { | 38 if (_receivePort == null) { |
| 32 _receivePort = new RawReceivePort(); | 39 _receivePort = new RawReceivePort(); |
| 33 _replyToPort = _receivePort.sendPort; | 40 _replyToPort = _receivePort.sendPort; |
| 34 _receivePort.handler = (data) { | 41 _receivePort.handler = (data) { |
| 35 assert(data is List && data.length == 2); | 42 assert(data is List && data.length == 2); |
| 36 _messageMap.remove(data[0]).complete(data[1]); | 43 _messageMap.remove(data[0]).complete(data[1]); |
| 37 if (_messageMap.length == 0) { | 44 if (_messageMap.length == 0) { |
| 38 _id = 0; | 45 _finalize(); |
| 39 _receivePort.close(); | |
| 40 _receivePort = null; | |
| 41 } | 46 } |
| 42 }; | 47 }; |
| 43 } | 48 } |
| 44 } | 49 } |
| 45 | 50 |
| 51 static void _finalize() { |
| 52 _id = 0; |
| 53 _receivePort.close(); |
| 54 _receivePort = null; |
| 55 } |
| 56 |
| 46 static int _getNextId() { | 57 static int _getNextId() { |
| 47 if (_id == 0x7FFFFFFF) _id = 0; | 58 if (_id == 0x7FFFFFFF) _id = 0; |
| 48 return _id++; | 59 return _id++; |
| 49 } | 60 } |
| 50 | 61 |
| 51 static SendPort _newServicePort() native "IOService_NewServicePort"; | 62 static SendPort _newServicePort() native "IOService_NewServicePort"; |
| 52 } | 63 } |
| OLD | NEW |