| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 library MandelIsolateTest; | 5 library MandelIsolateTest; |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 import 'dart:math'; | 8 import 'dart:math'; |
| 9 import '../../pkg/unittest/lib/unittest.dart'; | 9 import '../../pkg/unittest/lib/unittest.dart'; |
| 10 | 10 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 List<LineProcessorClient> _lineProcessedBy; | 79 List<LineProcessorClient> _lineProcessedBy; |
| 80 int _sent; | 80 int _sent; |
| 81 int _missing; | 81 int _missing; |
| 82 Completer<bool> _validated; | 82 Completer<bool> _validated; |
| 83 } | 83 } |
| 84 | 84 |
| 85 | 85 |
| 86 class LineProcessorClient { | 86 class LineProcessorClient { |
| 87 | 87 |
| 88 LineProcessorClient(MandelbrotState this._state, int this._id) { | 88 LineProcessorClient(MandelbrotState this._state, int this._id) { |
| 89 _port = spawnFunction(processLines); | 89 _sink = streamSpawnFunction(processLines); |
| 90 _box = new MessageBox(); |
| 91 _sink.add(_box.sink); |
| 92 _box.stream.listen((List<int> message) { |
| 93 _state.notifyProcessedLine(this, _currentLine, message); |
| 94 }); |
| 90 } | 95 } |
| 91 | 96 |
| 92 void processLine(int y) { | 97 void processLine(int y) { |
| 93 _port.call(y).then((List<int> message) { | 98 _currentLine = y; |
| 94 _state.notifyProcessedLine(this, y, message); | 99 _sink.add(y); |
| 95 }); | |
| 96 } | 100 } |
| 97 | 101 |
| 98 void shutdown() { | 102 void shutdown() { |
| 99 _port.send(TERMINATION_MESSAGE, null); | 103 _sink.close(); |
| 104 _box.stream.close(); |
| 100 } | 105 } |
| 101 | 106 |
| 102 MandelbrotState _state; | 107 MandelbrotState _state; |
| 103 int _id; | 108 int _id; |
| 104 SendPort _port; | 109 IsolateSink _sink; |
| 110 int _currentLine; |
| 111 MessageBox _box; |
| 105 } | 112 } |
| 106 | 113 |
| 107 List<int> processLine(int y) { | 114 List<int> processLine(int y) { |
| 108 double inverseN = 2.0 / N; | 115 double inverseN = 2.0 / N; |
| 109 double Civ = y * inverseN - 1.0; | 116 double Civ = y * inverseN - 1.0; |
| 110 List<int> result = new List<int>(N); | 117 List<int> result = new List<int>(N); |
| 111 for (int x = 0; x < N; x++) { | 118 for (int x = 0; x < N; x++) { |
| 112 double Crv = x * inverseN - 1.5; | 119 double Crv = x * inverseN - 1.5; |
| 113 | 120 |
| 114 double Zrv = Crv; | 121 double Zrv = Crv; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 125 Trv = Zrv * Zrv; | 132 Trv = Zrv * Zrv; |
| 126 Tiv = Ziv * Ziv; | 133 Tiv = Ziv * Ziv; |
| 127 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); | 134 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); |
| 128 | 135 |
| 129 result[x] = i; | 136 result[x] = i; |
| 130 } | 137 } |
| 131 return result; | 138 return result; |
| 132 } | 139 } |
| 133 | 140 |
| 134 void processLines() { | 141 void processLines() { |
| 135 port.receive((message, SendPort replyTo) { | 142 bool isFirst = true; |
| 136 if (message == TERMINATION_MESSAGE) { | 143 IsolateSink replyTo; |
| 137 assert(replyTo == null); | 144 |
| 138 port.close(); | 145 stream.listen((message) { |
| 139 } else { | 146 if (isFirst) { |
| 140 replyTo.send(processLine(message), null); | 147 isFirst = false; |
| 148 replyTo = message; |
| 149 return; |
| 141 } | 150 } |
| 151 replyTo.add(processLine(message)); |
| 142 }); | 152 }); |
| 143 } | 153 } |
| OLD | NEW |