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