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

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
« tests/html/async_test.dart ('K') | « tests/html/async_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 19 matching lines...) Expand all
30 }; 30 };
31 31
32 const _NEW_TIMER = 'NEW_TIMER'; 32 const _NEW_TIMER = 'NEW_TIMER';
33 const _CANCEL_TIMER = 'CANCEL_TIMER'; 33 const _CANCEL_TIMER = 'CANCEL_TIMER';
34 const _TIMER_PING = 'TIMER_PING'; 34 const _TIMER_PING = 'TIMER_PING';
35 35
36 class _PureIsolateTimer implements Timer { 36 class _PureIsolateTimer implements Timer {
37 final ReceivePort _port = new ReceivePort(); 37 final ReceivePort _port = new ReceivePort();
38 SendPort _sendPort; // Effectively final. 38 SendPort _sendPort; // Effectively final.
39 39
40 static SendPort _SEND_PORT;
41
40 _PureIsolateTimer(int milliSeconds, callback, repeating) { 42 _PureIsolateTimer(int milliSeconds, callback, repeating) {
41 _sendPort = _port.toSendPort(); 43 _sendPort = _port.toSendPort();
42 _port.receive((msg, replyTo) { 44 _port.receive((msg, replyTo) {
43 assert(msg == _TIMER_PING); 45 assert(msg == _TIMER_PING);
44 assert(replyTo == _HELPER_ISOLATE_PORT); 46 assert(replyTo == _HELPER_ISOLATE_PORT);
45 callback(this); 47 callback(this);
46 if (!repeating) _cancel(); 48 if (!repeating) _cancel();
47 }); 49 });
48 _HELPER_ISOLATE_PORT.then((port) { 50
49 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); 51 registerTimer() {
50 }); 52 _SEND_PORT.send([_NEW_TIMER, milliSeconds, repeating], _sendPort);
53 }
54
55 // Tricky part: Future.then is always delayed might prevent
56 // new timer registration if many events come to this isolate.
57 // Therefore, let's resort to then only if really necessary.
vsm 2013/03/29 14:37:39 I'm confused by this. What was the problem you we
Anton Muhin 2013/03/29 16:09:15 (whatever outcome of our discuss be, I need to mod
vsm 2013/03/29 17:34:36 I think I'm starting to get this - was the old cod
Anton Muhin 2013/04/01 14:52:57 I've updated the comment. May you, please, tell m
58 if (_SEND_PORT != null) {
59 registerTimer();
60 } else {
61 _HELPER_ISOLATE_PORT.then((port) {
62 _SEND_PORT = port;
63 registerTimer();
64 });
65 }
51 } 66 }
52 67
53 void cancel() { 68 void cancel() {
54 _cancel(); 69 _cancel();
55 _HELPER_ISOLATE_PORT.then((port) { 70 _HELPER_ISOLATE_PORT.then((port) {
56 port.send([_CANCEL_TIMER], _sendPort); 71 port.send([_CANCEL_TIMER], _sendPort);
57 }); 72 });
58 } 73 }
59 74
60 void _cancel() { 75 void _cancel() {
61 _port.close(); 76 _port.close();
62 } 77 }
63 78
64 static final Future<SendPort> _HELPER_ISOLATE_PORT = 79 static final Future<SendPort> _HELPER_ISOLATE_PORT =
65 spawnDomFunction(_helperIsolateMain); 80 spawnDomFunction(_helperIsolateMain);
66 } 81 }
67 82
68 final _TIMER_REGISTRY = new Map<SendPort, Timer>(); 83 final _TIMER_REGISTRY = new Map<SendPort, Timer>();
69 84
70 _helperIsolateMain() { 85 _helperIsolateMain() {
71 port.receive((msg, replyTo) { 86 port.receive((msg, replyTo) {
72 final cmd = msg[0]; 87 final cmd = msg[0];
73 if (cmd == _NEW_TIMER) { 88 if (cmd == _NEW_TIMER) {
74 final duration = new Duration(milliseconds: msg[1]); 89 final duration = new Duration(milliseconds: msg[1]);
75 bool periodic = msg[2]; 90 bool periodic = msg[2];
76 final callback = () { replyTo.send(_TIMER_PING); }; 91 ping() { replyTo.send(_TIMER_PING); }
77 _TIMER_REGISTRY[replyTo] = periodic ? 92 _TIMER_REGISTRY[replyTo] = periodic ?
78 new Timer.periodic(duration, callback) : 93 new Timer.periodic(duration, (_) { ping(); }) :
79 new Timer(duration, callback); 94 new Timer(duration, ping);
80 } else if (cmd == _CANCEL_TIMER) { 95 } else if (cmd == _CANCEL_TIMER) {
81 _TIMER_REGISTRY.remove(replyTo).cancel(); 96 Timer timer = _TIMER_REGISTRY.remove(replyTo);
97 if (timer != null) timer.cancel();
82 } 98 }
83 }); 99 });
84 } 100 }
85 101
86 get _pureIsolateTimerFactoryClosure => 102 get _pureIsolateTimerFactoryClosure =>
87 ((int milliSeconds, void callback(Timer time), bool repeating) => 103 ((int milliSeconds, void callback(Timer time), bool repeating) =>
88 new _PureIsolateTimer(milliSeconds, callback, repeating)); 104 new _PureIsolateTimer(milliSeconds, callback, repeating));
OLDNEW
« tests/html/async_test.dart ('K') | « tests/html/async_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698