Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 library dart2js.common.tasks; | 5 library dart2js.common.tasks; |
| 6 | 6 |
| 7 import 'dart:async' | 7 import 'dart:async' |
| 8 show Future, Zone, ZoneDelegate, ZoneSpecification, runZoned; | 8 show Future, Zone, ZoneDelegate, ZoneSpecification, runZoned; |
| 9 | 9 |
| 10 /// Used to measure where time is spent in the compiler. | 10 /// Used to measure where time is spent in the compiler. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 // Using zones, we're able to track asynchronous operations correctly, as | 90 // Using zones, we're able to track asynchronous operations correctly, as |
| 91 // our zone will be asked to invoke `then` blocks. Then blocks (the closure | 91 // our zone will be asked to invoke `then` blocks. Then blocks (the closure |
| 92 // passed to runZoned, and other closures) are run via the `run` functions | 92 // passed to runZoned, and other closures) are run via the `run` functions |
| 93 // below. | 93 // below. |
| 94 | 94 |
| 95 assert(_watch != null); | 95 assert(_watch != null); |
| 96 | 96 |
| 97 // The current zone is already measuring `this` task. | 97 // The current zone is already measuring `this` task. |
| 98 if (Zone.current[measurer] == this) return action(); | 98 if (Zone.current[measurer] == this) return action(); |
| 99 | 99 |
| 100 /// Run [f] in [zone]. Running must be delegated to [parent] to ensure that | 100 return runZoned(action, |
| 101 /// various state is set up correctly (in particular that `Zone.current` | 101 zoneValues: _zoneValues ??= {measurer: this}, |
| 102 /// has the right value). Since [_measureZoned] can be called recursively | 102 zoneSpecification: _zoneSpecification ??= new ZoneSpecification( |
| 103 /// (synchronously), some of the measuring zones we create will be parents | 103 run: _run, runUnary: _runUnary, runBinary: _runBinary)); |
| 104 /// of other measuring zones, but we still need to call through the parent | 104 } |
| 105 /// chain. Consequently, we use a zone value keyed by [measurer] to see if | 105 Map _zoneValues; |
| 106 /// we should measure or not when delegating. | 106 ZoneSpecification _zoneSpecification; |
|
ahe
2016/08/04 17:17:33
I would move the fields up with the other fields o
| |
| 107 run(Zone self, ZoneDelegate parent, Zone zone, f()) { | 107 |
| 108 if (zone[measurer] != this) return parent.run(zone, f); | 108 /// Run [f] in [zone]. Running must be delegated to [parent] to ensure that |
| 109 CompilerTask previous = _start(); | 109 /// various state is set up correctly (in particular that `Zone.current` |
| 110 try { | 110 /// has the right value). Since [_measureZoned] can be called recursively |
| 111 return parent.run(zone, f); | 111 /// (synchronously), some of the measuring zones we create will be parents |
| 112 } finally { | 112 /// of other measuring zones, but we still need to call through the parent |
| 113 _stop(previous); | 113 /// chain. Consequently, we use a zone value keyed by [measurer] to see if |
| 114 } | 114 /// we should measure or not when delegating. |
| 115 _run(Zone self, ZoneDelegate parent, Zone zone, f()) { | |
| 116 if (zone[measurer] != this) return parent.run(zone, f); | |
| 117 CompilerTask previous = _start(); | |
| 118 try { | |
| 119 return parent.run(zone, f); | |
| 120 } finally { | |
| 121 _stop(previous); | |
| 115 } | 122 } |
| 123 } | |
| 116 | 124 |
| 117 /// Same as [run] except that [f] takes one argument, [arg]. | 125 /// Same as [run] except that [f] takes one argument, [arg]. |
| 118 runUnary(Zone self, ZoneDelegate parent, Zone zone, f(arg), arg) { | 126 _runUnary(Zone self, ZoneDelegate parent, Zone zone, f(arg), arg) { |
| 119 if (zone[measurer] != this) return parent.runUnary(zone, f, arg); | 127 if (zone[measurer] != this) return parent.runUnary(zone, f, arg); |
| 120 CompilerTask previous = _start(); | 128 CompilerTask previous = _start(); |
| 121 try { | 129 try { |
| 122 return parent.runUnary(zone, f, arg); | 130 return parent.runUnary(zone, f, arg); |
| 123 } finally { | 131 } finally { |
| 124 _stop(previous); | 132 _stop(previous); |
| 125 } | |
| 126 } | 133 } |
| 134 } | |
| 127 | 135 |
| 128 /// Same as [run] except that [f] takes two arguments ([a1] and [a2]). | 136 /// Same as [run] except that [f] takes two arguments ([a1] and [a2]). |
| 129 runBinary(Zone self, ZoneDelegate parent, Zone zone, f(a1, a2), a1, a2) { | 137 _runBinary(Zone self, ZoneDelegate parent, Zone zone, f(a1, a2), a1, a2) { |
| 130 if (zone[measurer] != this) return parent.runBinary(zone, f, a1, a2); | 138 if (zone[measurer] != this) return parent.runBinary(zone, f, a1, a2); |
| 131 CompilerTask previous = _start(); | 139 CompilerTask previous = _start(); |
| 132 try { | 140 try { |
| 133 return parent.runBinary(zone, f, a1, a2); | 141 return parent.runBinary(zone, f, a1, a2); |
| 134 } finally { | 142 } finally { |
| 135 _stop(previous); | 143 _stop(previous); |
| 136 } | |
| 137 } | 144 } |
| 138 | |
| 139 return runZoned(action, | |
| 140 zoneValues: {measurer: this}, | |
| 141 zoneSpecification: new ZoneSpecification( | |
| 142 run: run, runUnary: runUnary, runBinary: runBinary)); | |
| 143 } | 145 } |
| 144 | 146 |
| 145 /// Asynchronous version of [measure]. Use this when action returns a future | 147 /// Asynchronous version of [measure]. Use this when action returns a future |
| 146 /// that's truly asynchronous, such I/O. Only one task can use this method | 148 /// that's truly asynchronous, such I/O. Only one task can use this method |
| 147 /// concurrently. | 149 /// concurrently. |
| 148 /// | 150 /// |
| 149 /// Note: we assume that this method is used only by the compiler input | 151 /// Note: we assume that this method is used only by the compiler input |
| 150 /// provider, but it could be used by other tasks as long as the input | 152 /// provider, but it could be used by other tasks as long as the input |
| 151 /// provider will not be called by those tasks. | 153 /// provider will not be called by those tasks. |
| 152 measureIo(Future action()) { | 154 measureIo(Future action()) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 } | 242 } |
| 241 | 243 |
| 242 /// Call this when the eventloop returns control to us. | 244 /// Call this when the eventloop returns control to us. |
| 243 void stopAsyncWallClock() { | 245 void stopAsyncWallClock() { |
| 244 if (currentAsyncTask != null) { | 246 if (currentAsyncTask != null) { |
| 245 currentAsyncTask._watch.stop(); | 247 currentAsyncTask._watch.stop(); |
| 246 } | 248 } |
| 247 asyncWallClock.stop(); | 249 asyncWallClock.stop(); |
| 248 } | 250 } |
| 249 } | 251 } |
| OLD | NEW |