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 19 matching lines...) Expand all Loading... |
30 MandelbrotState() { | 30 MandelbrotState() { |
31 _result = new List<List<int>>(N); | 31 _result = new List<List<int>>(N); |
32 _lineProcessedBy = new List<LineProcessorClient>(N); | 32 _lineProcessedBy = new List<LineProcessorClient>(N); |
33 _sent = 0; | 33 _sent = 0; |
34 _missing = N; | 34 _missing = N; |
35 _validated = new Completer<bool>(); | 35 _validated = new Completer<bool>(); |
36 } | 36 } |
37 | 37 |
38 void startClient(int id) { | 38 void startClient(int id) { |
39 assert(_sent < N); | 39 assert(_sent < N); |
40 final client = new LineProcessorClient(this, id); | 40 int line = _sent++; |
41 client.processLine(_sent++); | 41 LineProcessorClient.create(this, id).then((final client) { |
| 42 client.processLine(line); |
| 43 }); |
42 } | 44 } |
43 | 45 |
44 void notifyProcessedLine(LineProcessorClient client, int y, List<int> line) { | 46 void notifyProcessedLine(LineProcessorClient client, int y, List<int> line) { |
45 assert(_result[y] == null); | 47 assert(_result[y] == null); |
46 _result[y] = line; | 48 _result[y] = line; |
47 _lineProcessedBy[y] = client; | 49 _lineProcessedBy[y] = client; |
48 | 50 |
49 if (_sent != N) { | 51 if (_sent != N) { |
50 client.processLine(_sent++); | 52 client.processLine(_sent++); |
51 } else { | 53 } else { |
(...skipping 27 matching lines...) Expand all Loading... |
79 | 81 |
80 List<List<int>> _result; | 82 List<List<int>> _result; |
81 List<LineProcessorClient> _lineProcessedBy; | 83 List<LineProcessorClient> _lineProcessedBy; |
82 int _sent; | 84 int _sent; |
83 int _missing; | 85 int _missing; |
84 Completer<bool> _validated; | 86 Completer<bool> _validated; |
85 } | 87 } |
86 | 88 |
87 | 89 |
88 class LineProcessorClient { | 90 class LineProcessorClient { |
| 91 MandelbrotState _state; |
| 92 int _id; |
| 93 SendPort _port; |
89 | 94 |
90 LineProcessorClient(MandelbrotState this._state, int this._id) { | 95 LineProcessorClient(this._state, this._id, this._port); |
91 _port = spawnFunction(processLines); | 96 |
| 97 static Future<LineProcessorClient> create(MandelbrotState state, int id) { |
| 98 ReceivePort reply = new ReceivePort(); |
| 99 return Isolate.spawn(processLines, reply.sendPort).then((_) { |
| 100 return reply.first.then((port) { |
| 101 return new LineProcessorClient(state, id, port); |
| 102 }); |
| 103 }); |
92 } | 104 } |
93 | 105 |
94 void processLine(int y) { | 106 void processLine(int y) { |
95 _port.call(y).then((List<int> message) { | 107 ReceivePort reply = new ReceivePort(); |
| 108 _port.send([y, reply.sendPort]); |
| 109 reply.first.then((List<int> message) { |
96 _state.notifyProcessedLine(this, y, message); | 110 _state.notifyProcessedLine(this, y, message); |
97 }); | 111 }); |
98 } | 112 } |
99 | 113 |
100 void shutdown() { | 114 void shutdown() { |
101 _port.send(TERMINATION_MESSAGE, null); | 115 _port.send(TERMINATION_MESSAGE); |
102 } | 116 } |
103 | |
104 MandelbrotState _state; | |
105 int _id; | |
106 SendPort _port; | |
107 } | 117 } |
108 | 118 |
109 List<int> processLine(int y) { | 119 List<int> processLine(int y) { |
110 double inverseN = 2.0 / N; | 120 double inverseN = 2.0 / N; |
111 double Civ = y * inverseN - 1.0; | 121 double Civ = y * inverseN - 1.0; |
112 List<int> result = new List<int>(N); | 122 List<int> result = new List<int>(N); |
113 for (int x = 0; x < N; x++) { | 123 for (int x = 0; x < N; x++) { |
114 double Crv = x * inverseN - 1.5; | 124 double Crv = x * inverseN - 1.5; |
115 | 125 |
116 double Zrv = Crv; | 126 double Zrv = Crv; |
117 double Ziv = Civ; | 127 double Ziv = Civ; |
118 | 128 |
119 double Trv = Crv * Crv; | 129 double Trv = Crv * Crv; |
120 double Tiv = Civ * Civ; | 130 double Tiv = Civ * Civ; |
121 | 131 |
122 int i = 49; | 132 int i = 49; |
123 do { | 133 do { |
124 Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ; | 134 Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ; |
125 Zrv = Trv - Tiv + Crv; | 135 Zrv = Trv - Tiv + Crv; |
126 | 136 |
127 Trv = Zrv * Zrv; | 137 Trv = Zrv * Zrv; |
128 Tiv = Ziv * Ziv; | 138 Tiv = Ziv * Ziv; |
129 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); | 139 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); |
130 | 140 |
131 result[x] = i; | 141 result[x] = i; |
132 } | 142 } |
133 return result; | 143 return result; |
134 } | 144 } |
135 | 145 |
136 void processLines() { | 146 void processLines(SendPort replyPort) { |
137 port.receive((message, SendPort replyTo) { | 147 ReceivePort port = new ReceivePort(); |
138 if (message == TERMINATION_MESSAGE) { | 148 port.listen((message) { |
139 assert(replyTo == null); | 149 if (message != TERMINATION_MESSAGE) { |
| 150 int line = message[0]; |
| 151 SendPort replyTo = message[1]; |
| 152 replyTo.send(processLine(line)); |
| 153 } else { |
140 port.close(); | 154 port.close(); |
141 } else { | |
142 replyTo.send(processLine(message), null); | |
143 } | 155 } |
144 }); | 156 }); |
| 157 replyPort.send(port.sendPort); |
145 } | 158 } |
OLD | NEW |