| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 test.util.compiler_pool; | 5 library test.util.compiler_pool; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:async/async.dart'; |
| 11 import 'package:path/path.dart' as p; | 12 import 'package:path/path.dart' as p; |
| 12 import 'package:pool/pool.dart'; | 13 import 'package:pool/pool.dart'; |
| 13 | 14 |
| 14 import '../../util/async_thunk.dart'; | |
| 15 import '../../util/io.dart'; | 15 import '../../util/io.dart'; |
| 16 import '../load_exception.dart'; | 16 import '../load_exception.dart'; |
| 17 | 17 |
| 18 /// A pool of `dart2js` instances. | 18 /// A pool of `dart2js` instances. |
| 19 /// | 19 /// |
| 20 /// This limits the number of compiler instances running concurrently. | 20 /// This limits the number of compiler instances running concurrently. |
| 21 class CompilerPool { | 21 class CompilerPool { |
| 22 /// The internal pool that controls the number of process running at once. | 22 /// The internal pool that controls the number of process running at once. |
| 23 final Pool _pool; | 23 final Pool _pool; |
| 24 | 24 |
| 25 /// Whether to enable colors on dart2js. | 25 /// Whether to enable colors on dart2js. |
| 26 final bool _color; | 26 final bool _color; |
| 27 | 27 |
| 28 /// The currently-active dart2js processes. | 28 /// The currently-active dart2js processes. |
| 29 final _processes = new Set<Process>(); | 29 final _processes = new Set<Process>(); |
| 30 | 30 |
| 31 /// Whether [close] has been called. | 31 /// Whether [close] has been called. |
| 32 bool get _closed => _closeThunk.hasRun; | 32 bool get _closed => _closeMemo.hasRun; |
| 33 | 33 |
| 34 /// The thunk for running [close] exactly once. | 34 /// The memoizer for running [close] exactly once. |
| 35 final _closeThunk = new AsyncThunk(); | 35 final _closeMemo = new AsyncMemoizer(); |
| 36 | 36 |
| 37 /// Creates a compiler pool that runs up to [concurrency] instances of | 37 /// Creates a compiler pool that runs up to [concurrency] instances of |
| 38 /// `dart2js` at once. | 38 /// `dart2js` at once. |
| 39 /// | 39 /// |
| 40 /// If [concurrency] isn't provided, it defaults to 4. | 40 /// If [concurrency] isn't provided, it defaults to 4. |
| 41 /// | 41 /// |
| 42 /// If [color] is true, `dart2js` will be run with colors enabled. | 42 /// If [color] is true, `dart2js` will be run with colors enabled. |
| 43 CompilerPool({int concurrency, bool color: false}) | 43 CompilerPool({int concurrency, bool color: false}) |
| 44 : _pool = new Pool(concurrency == null ? 4 : concurrency), | 44 : _pool = new Pool(concurrency == null ? 4 : concurrency), |
| 45 _color = color; | 45 _color = color; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 return sanitizeForWindows(stream) | 118 return sanitizeForWindows(stream) |
| 119 .listen((data) => buffer.write(UTF8.decode(data))).asFuture(); | 119 .listen((data) => buffer.write(UTF8.decode(data))).asFuture(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 /// Closes the compiler pool. | 122 /// Closes the compiler pool. |
| 123 /// | 123 /// |
| 124 /// This kills all currently-running compilers and ensures that no more will | 124 /// This kills all currently-running compilers and ensures that no more will |
| 125 /// be started. It returns a [Future] that completes once all the compilers | 125 /// be started. It returns a [Future] that completes once all the compilers |
| 126 /// have been killed and all resources released. | 126 /// have been killed and all resources released. |
| 127 Future close() { | 127 Future close() { |
| 128 return _closeThunk.run(() async { | 128 return _closeMemo.runOnce(() async { |
| 129 await Future.wait(_processes.map((process) async { | 129 await Future.wait(_processes.map((process) async { |
| 130 process.kill(); | 130 process.kill(); |
| 131 await process.exitCode; | 131 await process.exitCode; |
| 132 })); | 132 })); |
| 133 }); | 133 }); |
| 134 } | 134 } |
| 135 } | 135 } |
| OLD | NEW |