OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import "dart:isolate"; | 5 import "dart:isolate"; |
6 import "dart:async"; | 6 import "dart:async"; |
7 import "package:async_helper/async_helper.dart"; | 7 import "package:async_helper/async_helper.dart"; |
8 | 8 |
9 isomain1(replyPort) { | 9 isomain1(replyPort) { |
10 RawReceivePort port = new RawReceivePort(); | 10 RawReceivePort port = new RawReceivePort(); |
11 port.handler = (v) { | 11 port.handler = (v) { |
12 replyPort.send(v); | 12 replyPort.send(v); |
13 port.close(); | 13 port.close(); |
14 }; | 14 }; |
15 replyPort.send(port.sendPort); | 15 replyPort.send(port.sendPort); |
16 } | 16 } |
17 | 17 |
18 main(){ | 18 main() { |
19 asyncStart(); | 19 asyncStart(); |
20 RawReceivePort reply = new RawReceivePort(); | 20 RawReceivePort reply = new RawReceivePort(); |
21 Isolate isolate; | 21 Isolate isolate; |
22 Capability resume; | 22 Capability resume; |
23 var completer = new Completer(); // Completed by first reply from isolate. | 23 var completer = new Completer(); // Completed by first reply from isolate. |
24 reply.handler = completer.complete; | 24 reply.handler = completer.complete; |
25 Isolate.spawn(isomain1, reply.sendPort).then((Isolate iso) { | 25 Isolate.spawn(isomain1, reply.sendPort).then((Isolate iso) { |
26 isolate = iso; | 26 isolate = iso; |
27 return completer.future; | 27 return completer.future; |
28 }).then((echoPort) { | 28 }).then((echoPort) { |
29 // Isolate has been created, and first response has been returned. | 29 // Isolate has been created, and first response has been returned. |
30 resume = isolate.pause(); | 30 resume = isolate.pause(); |
31 echoPort.send(24); | 31 echoPort.send(24); |
32 reply.handler = (v) { | 32 reply.handler = (v) { |
33 throw "RESPONSE WHILE PAUSED?!?"; | 33 throw "RESPONSE WHILE PAUSED?!?"; |
34 }; | 34 }; |
35 return new Future.delayed(const Duration(milliseconds: 250)); | 35 return new Future.delayed(const Duration(milliseconds: 250)); |
36 }).then((_) { | 36 }).then((_) { |
37 reply.handler = (v) { | 37 reply.handler = (v) { |
38 if (v != 24) throw "WRONG ANSWER!"; | 38 if (v != 24) throw "WRONG ANSWER!"; |
39 reply.close(); | 39 reply.close(); |
40 asyncEnd(); | 40 asyncEnd(); |
41 }; | 41 }; |
42 isolate.resume(resume); | 42 isolate.resume(resume); |
43 }); | 43 }); |
44 } | 44 } |
OLD | NEW |