OLD | NEW |
1 // Copyright (c) 2011, 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:isolate'); | 6 #import('dart:isolate'); |
7 #import('../../lib/unittest/unittest.dart'); | 7 #import('../../lib/unittest/unittest.dart'); |
8 | 8 |
9 final TERMINATION_MESSAGE = -1; | 9 final TERMINATION_MESSAGE = -1; |
10 final N = 100; | 10 final N = 100; |
11 final ISOLATES = 20; | 11 final ISOLATES = 20; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 List<LineProcessorClient> _lineProcessedBy; | 77 List<LineProcessorClient> _lineProcessedBy; |
78 int _sent; | 78 int _sent; |
79 int _missing; | 79 int _missing; |
80 Completer<bool> _validated; | 80 Completer<bool> _validated; |
81 } | 81 } |
82 | 82 |
83 | 83 |
84 class LineProcessorClient { | 84 class LineProcessorClient { |
85 | 85 |
86 LineProcessorClient(MandelbrotState this._state, int this._id) { | 86 LineProcessorClient(MandelbrotState this._state, int this._id) { |
87 _out = new LineProcessor().spawn(); | 87 _port = spawnFunction(processLines); |
88 } | 88 } |
89 | 89 |
90 void processLine(int y) { | 90 void processLine(int y) { |
91 _out.then((SendPort p) { | 91 _port.call(y).then((List<int> message) { |
92 p.call(y).then((List<int> message) { | 92 _state.notifyProcessedLine(this, y, message); |
93 _state.notifyProcessedLine(this, y, message); | |
94 }); | |
95 }); | 93 }); |
96 } | 94 } |
97 | 95 |
98 void shutdown() { | 96 void shutdown() { |
99 _out.then((SendPort p) { | 97 _port.send(TERMINATION_MESSAGE, null); |
100 p.send(TERMINATION_MESSAGE, null); | |
101 }); | |
102 } | 98 } |
103 | 99 |
104 MandelbrotState _state; | 100 MandelbrotState _state; |
105 int _id; | 101 int _id; |
106 Future<SendPort> _out; | 102 SendPort _port; |
107 | |
108 } | 103 } |
109 | 104 |
| 105 List<int> processLine(int y) { |
| 106 double inverseN = 2.0 / N; |
| 107 double Civ = y * inverseN - 1.0; |
| 108 List<int> result = new List<int>(N); |
| 109 for (int x = 0; x < N; x++) { |
| 110 double Crv = x * inverseN - 1.5; |
110 | 111 |
111 class LineProcessor extends Isolate { | 112 double Zrv = Crv; |
| 113 double Ziv = Civ; |
112 | 114 |
113 LineProcessor() : super() { } | 115 double Trv = Crv * Crv; |
| 116 double Tiv = Civ * Civ; |
114 | 117 |
115 void main() { | 118 int i = 49; |
116 this.port.receive((message, SendPort replyTo) { | 119 do { |
117 if (message == TERMINATION_MESSAGE) { | 120 Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ; |
118 assert(replyTo == null); | 121 Zrv = Trv - Tiv + Crv; |
119 this.port.close(); | 122 |
120 } else { | 123 Trv = Zrv * Zrv; |
121 replyTo.send(_processLine(message), null); | 124 Tiv = Ziv * Ziv; |
122 } | 125 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); |
123 }); | 126 |
| 127 result[x] = i; |
124 } | 128 } |
| 129 return result; |
| 130 } |
125 | 131 |
126 static List<int> _processLine(int y) { | 132 void processLines() { |
127 double inverseN = 2.0 / N; | 133 port.receive((message, SendPort replyTo) { |
128 double Civ = y * inverseN - 1.0; | 134 if (message == TERMINATION_MESSAGE) { |
129 List<int> result = new List<int>(N); | 135 assert(replyTo == null); |
130 for (int x = 0; x < N; x++) { | 136 port.close(); |
131 double Crv = x * inverseN - 1.5; | 137 } else { |
132 | 138 replyTo.send(processLine(message), null); |
133 double Zrv = Crv; | |
134 double Ziv = Civ; | |
135 | |
136 double Trv = Crv * Crv; | |
137 double Tiv = Civ * Civ; | |
138 | |
139 int i = 49; | |
140 do { | |
141 Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ; | |
142 Zrv = Trv - Tiv + Crv; | |
143 | |
144 Trv = Zrv * Zrv; | |
145 Tiv = Ziv * Ziv; | |
146 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); | |
147 | |
148 result[x] = i; | |
149 } | 139 } |
150 return result; | 140 }); |
151 } | |
152 } | 141 } |
OLD | NEW |