Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: tools/dom/src/Timer.dart

Issue 13308002: Add a test for timers in child isolates. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 part of html; 5 part of html;
6 6
7 // TODO(antonm): support not DOM isolates too. 7 // TODO(antonm): support not DOM isolates too.
8 class _Timer implements Timer { 8 class _Timer implements Timer {
9 final canceller; 9 final canceller;
10 10
(...skipping 15 matching lines...) Expand all
26 Timer timer; 26 Timer timer;
27 final int id = maker(() { callback(timer); }, milliSeconds); 27 final int id = maker(() { callback(timer); }, milliSeconds);
28 timer = new _Timer(() { canceller(id); }); 28 timer = new _Timer(() { canceller(id); });
29 return timer; 29 return timer;
30 }; 30 };
31 31
32 class _PureIsolateTimer implements Timer { 32 class _PureIsolateTimer implements Timer {
33 final ReceivePort _port = new ReceivePort(); 33 final ReceivePort _port = new ReceivePort();
34 SendPort _sendPort; // Effectively final. 34 SendPort _sendPort; // Effectively final.
35 35
36 static SendPort _SEND_PORT;
37
36 _PureIsolateTimer(int milliSeconds, callback, repeating) { 38 _PureIsolateTimer(int milliSeconds, callback, repeating) {
37 _sendPort = _port.toSendPort(); 39 _sendPort = _port.toSendPort();
38 _port.receive((msg, replyTo) { 40 _port.receive((msg, replyTo) {
39 assert(msg == _TIMER_PING); 41 assert(msg == _TIMER_PING);
40 callback(this); 42 callback(this);
41 if (!repeating) _cancel(); 43 if (!repeating) _cancel();
42 }); 44 });
43 _HELPER_ISOLATE_PORT.then((port) { 45
44 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); 46 _send([_NEW_TIMER, milliSeconds, repeating]);
45 });
46 } 47 }
47 48
48 void cancel() { 49 void cancel() {
49 _cancel(); 50 _cancel();
50 _HELPER_ISOLATE_PORT.then((port) { 51 _send([_CANCEL_TIMER]);
51 port.send([_CANCEL_TIMER], _sendPort);
52 });
53 } 52 }
54 53
55 void _cancel() { 54 void _cancel() {
56 _port.close(); 55 _port.close();
57 } 56 }
57
58 // Tricky part.
59 // Once _HELPER_ISOLATE_PORT gets resolved, it will still delay in .then
60 // and to delay Timer.run is used. However, Timer.run will try to register
61 // another Timer and here we got stuck: event cannot be posted as then
62 // callback is not executed because it's delayed with timer.
63 // Therefore once future is resolved, it's unsafe to call .then on it
64 // in Timer code.
vsm 2013/04/01 15:27:32 Thanks, this looks good.
65 _send(msg) {
66 if (_SEND_PORT != null) {
67 _SEND_PORT.send(msg, _sendPort);
68 } else {
69 _HELPER_ISOLATE_PORT.then((port) {
70 _SEND_PORT = port;
71 _SEND_PORT.send(msg, _sendPort);
72 });
73 }
74 }
58 } 75 }
59 76
60 get _pureIsolateTimerFactoryClosure => 77 get _pureIsolateTimerFactoryClosure =>
61 ((int milliSeconds, void callback(Timer time), bool repeating) => 78 ((int milliSeconds, void callback(Timer time), bool repeating) =>
62 new _PureIsolateTimer(milliSeconds, callback, repeating)); 79 new _PureIsolateTimer(milliSeconds, callback, repeating));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698