OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 dart.async; | 5 part of dart.async; |
6 | 6 |
7 typedef void _AsyncCallback(); | 7 typedef void _AsyncCallback(); |
8 | 8 |
9 class _AsyncCallbackEntry { | 9 class _AsyncCallbackEntry { |
10 final _AsyncCallback callback; | 10 final _AsyncCallback callback; |
(...skipping 14 matching lines...) Expand all Loading... |
25 */ | 25 */ |
26 _AsyncCallbackEntry _lastPriorityCallback; | 26 _AsyncCallbackEntry _lastPriorityCallback; |
27 /** | 27 /** |
28 * Whether we are currently inside the callback loop. | 28 * Whether we are currently inside the callback loop. |
29 * | 29 * |
30 * If we are inside the loop, we never need to schedule the loop, | 30 * If we are inside the loop, we never need to schedule the loop, |
31 * even if adding a first element. | 31 * even if adding a first element. |
32 */ | 32 */ |
33 bool _isInCallbackLoop = false; | 33 bool _isInCallbackLoop = false; |
34 | 34 |
35 void _asyncRunCallbackLoop() { | 35 void _microtaskLoop() { |
36 while (_nextCallback != null) { | 36 while (_nextCallback != null) { |
37 _lastPriorityCallback = null; | 37 _lastPriorityCallback = null; |
38 _AsyncCallbackEntry entry = _nextCallback; | 38 _AsyncCallbackEntry entry = _nextCallback; |
39 _nextCallback = entry.next; | 39 _nextCallback = entry.next; |
40 if (_nextCallback == null) _lastCallback = null; | 40 if (_nextCallback == null) _lastCallback = null; |
41 entry.callback(); | 41 (entry.callback)(); |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 void _asyncRunCallback() { | 45 void _startMicrotaskLoop() { |
46 _isInCallbackLoop = true; | 46 _isInCallbackLoop = true; |
47 try { | 47 try { |
48 _asyncRunCallbackLoop(); | 48 // Moved to separate function because try-finally prevents |
| 49 // good optimization. |
| 50 _microtaskLoop(); |
49 } finally { | 51 } finally { |
50 _lastPriorityCallback = null; | 52 _lastPriorityCallback = null; |
51 _isInCallbackLoop = false; | 53 _isInCallbackLoop = false; |
52 if (_nextCallback != null) _AsyncRun._scheduleImmediate(_asyncRunCallback); | 54 if (_nextCallback != null) { |
| 55 _AsyncRun._scheduleImmediate(_startMicrotaskLoop); |
| 56 } |
53 } | 57 } |
54 } | 58 } |
55 | 59 |
56 /** | 60 /** |
57 * Schedules a callback to be called as a microtask. | 61 * Schedules a callback to be called as a microtask. |
58 * | 62 * |
59 * The microtask is called after all other currently scheduled | 63 * The microtask is called after all other currently scheduled |
60 * microtasks, but as part of the current system event. | 64 * microtasks, but as part of the current system event. |
61 */ | 65 */ |
62 void _scheduleAsyncCallback(callback) { | 66 void _scheduleAsyncCallback(_AsyncCallback callback) { |
63 // Optimizing a group of Timer.run callbacks to be executed in the | 67 _AsyncCallbackEntry newEntry = new _AsyncCallbackEntry(callback); |
64 // same Timer callback. | |
65 if (_nextCallback == null) { | 68 if (_nextCallback == null) { |
66 _nextCallback = _lastCallback = new _AsyncCallbackEntry(callback); | 69 _nextCallback = _lastCallback = newEntry; |
67 if (!_isInCallbackLoop) { | 70 if (!_isInCallbackLoop) { |
68 _AsyncRun._scheduleImmediate(_asyncRunCallback); | 71 _AsyncRun._scheduleImmediate(_startMicrotaskLoop); |
69 } | 72 } |
70 } else { | 73 } else { |
71 _AsyncCallbackEntry newEntry = new _AsyncCallbackEntry(callback); | |
72 _lastCallback.next = newEntry; | 74 _lastCallback.next = newEntry; |
73 _lastCallback = newEntry; | 75 _lastCallback = newEntry; |
74 } | 76 } |
75 } | 77 } |
76 | 78 |
77 /** | 79 /** |
78 * Schedules a callback to be called before all other currently scheduled ones. | 80 * Schedules a callback to be called before all other currently scheduled ones. |
79 * | 81 * |
80 * This callback takes priority over existing scheduled callbacks. | 82 * This callback takes priority over existing scheduled callbacks. |
81 * It is only used internally to give higher priority to error reporting. | 83 * It is only used internally to give higher priority to error reporting. |
| 84 * |
| 85 * Is always run in the root zone. |
82 */ | 86 */ |
83 void _schedulePriorityAsyncCallback(callback) { | 87 void _schedulePriorityAsyncCallback(_AsyncCallback callback) { |
84 _AsyncCallbackEntry entry = new _AsyncCallbackEntry(callback); | |
85 if (_nextCallback == null) { | 88 if (_nextCallback == null) { |
86 _scheduleAsyncCallback(callback); | 89 _scheduleAsyncCallback(callback); |
87 _lastPriorityCallback = _lastCallback; | 90 _lastPriorityCallback = _lastCallback; |
88 } else if (_lastPriorityCallback == null) { | 91 return; |
| 92 } |
| 93 _AsyncCallbackEntry entry = new _AsyncCallbackEntry(callback); |
| 94 if (_lastPriorityCallback == null) { |
89 entry.next = _nextCallback; | 95 entry.next = _nextCallback; |
90 _nextCallback = _lastPriorityCallback = entry; | 96 _nextCallback = _lastPriorityCallback = entry; |
91 } else { | 97 } else { |
92 entry.next = _lastPriorityCallback.next; | 98 entry.next = _lastPriorityCallback.next; |
93 _lastPriorityCallback.next = entry; | 99 _lastPriorityCallback.next = entry; |
94 _lastPriorityCallback = entry; | 100 _lastPriorityCallback = entry; |
95 if (entry.next == null) { | 101 if (entry.next == null) { |
96 _lastCallback = entry; | 102 _lastCallback = entry; |
97 } | 103 } |
98 } | 104 } |
(...skipping 18 matching lines...) Expand all Loading... |
117 * foo(); | 123 * foo(); |
118 * } | 124 * } |
119 * | 125 * |
120 * ## Other resources | 126 * ## Other resources |
121 * | 127 * |
122 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): | 128 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): |
123 * Learn how Dart handles the event queue and microtask queue, so you can write | 129 * Learn how Dart handles the event queue and microtask queue, so you can write |
124 * better asynchronous code with fewer surprises. | 130 * better asynchronous code with fewer surprises. |
125 */ | 131 */ |
126 void scheduleMicrotask(void callback()) { | 132 void scheduleMicrotask(void callback()) { |
127 if (identical(_ROOT_ZONE, Zone.current)) { | 133 _Zone currentZone = Zone.current; |
| 134 if (identical(_ROOT_ZONE, currentZone)) { |
128 // No need to bind the callback. We know that the root's scheduleMicrotask | 135 // No need to bind the callback. We know that the root's scheduleMicrotask |
129 // will be invoked in the root zone. | 136 // will be invoked in the root zone. |
130 _rootScheduleMicrotask(null, null, _ROOT_ZONE, callback); | 137 _rootScheduleMicrotask(null, null, _ROOT_ZONE, callback); |
131 return; | 138 return; |
132 } | 139 } |
| 140 _ZoneFunction implementation = currentZone._scheduleMicrotask; |
| 141 if (identical(_ROOT_ZONE, implementation.zone) && |
| 142 _ROOT_ZONE.inSameErrorZone(currentZone)) { |
| 143 _rootScheduleMicrotask(null, null, currentZone, |
| 144 currentZone.registerCallback(callback)); |
| 145 return; |
| 146 } |
133 Zone.current.scheduleMicrotask( | 147 Zone.current.scheduleMicrotask( |
134 Zone.current.bindCallback(callback, runGuarded: true)); | 148 Zone.current.bindCallback(callback, runGuarded: true)); |
135 } | 149 } |
136 | 150 |
137 class _AsyncRun { | 151 class _AsyncRun { |
138 /** Schedule the given callback before any other event in the event-loop. */ | 152 /** Schedule the given callback before any other event in the event-loop. */ |
139 external static void _scheduleImmediate(void callback()); | 153 external static void _scheduleImmediate(void callback()); |
140 } | 154 } |
OLD | NEW |