OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library dart.profiler; |
| 6 |
| 7 /// A UserTag can be used to group samples in the Observatory profiler. |
| 8 abstract class UserTag { |
| 9 /// The maximum number of UserTag instances that can be created by a program. |
| 10 static const MAX_USER_TAGS = 64; |
| 11 |
| 12 factory UserTag(String label) => new _FakeUserTag(label); |
| 13 |
| 14 /// Label of [this]. |
| 15 String get label; |
| 16 |
| 17 /// Make [this] the current tag for the isolate. |
| 18 makeCurrent(); |
| 19 } |
| 20 |
| 21 // This is a fake implementation of UserTag so that code can compile and run |
| 22 // in dart2js. |
| 23 class _FakeUserTag implements UserTag { |
| 24 static List _instances = []; |
| 25 |
| 26 _FakeUserTag.real(this.label); |
| 27 |
| 28 factory _FakeUserTag(String label) { |
| 29 // Canonicalize by name. |
| 30 for (var tag in _instances) { |
| 31 if (tag.label == label) { |
| 32 return tag; |
| 33 } |
| 34 } |
| 35 // Throw an exception if we've reached the maximum number of user tags. |
| 36 if (_instances.length == UserTag.MAX_USER_TAGS) { |
| 37 throw new UnsupportedError( |
| 38 'UserTag instance limit (${UserTag.MAX_USER_TAGS}) reached.'); |
| 39 } |
| 40 // Create a new instance and add it to the instance list. |
| 41 var instance = new _FakeUserTag.real(label); |
| 42 _instances.add(instance); |
| 43 return instance; |
| 44 } |
| 45 |
| 46 final String label; |
| 47 |
| 48 makeCurrent() { |
| 49 _currentTag = this; |
| 50 } |
| 51 } |
| 52 |
| 53 var _currentTag = null; |
| 54 |
| 55 /// Returns the current [UserTag] for the isolate. |
| 56 UserTag getCurrentTag() { |
| 57 return _currentTag; |
| 58 } |
| 59 |
| 60 /// Sets the current [UserTag] for the isolate to null. Returns current tag |
| 61 /// before clearing. |
| 62 UserTag clearCurrentTag() { |
| 63 var old = _currentTag; |
| 64 _currentTag = null; |
| 65 return old; |
| 66 } |
OLD | NEW |