OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkDynamicAnnotations_DEFINED | 8 #ifndef SkDynamicAnnotations_DEFINED |
9 #define SkDynamicAnnotations_DEFINED | 9 #define SkDynamicAnnotations_DEFINED |
10 | 10 |
11 // This file contains macros used to send out-of-band signals to dynamic instrum
entation systems, | 11 // This file contains macros used to send out-of-band signals to dynamic instrum
entation systems, |
12 // namely thread sanitizer. This is a cut-down version of the full dynamic_anno
tations library with | 12 // namely thread sanitizer. This is a cut-down version of the full dynamic_anno
tations library with |
13 // only the features used by Skia. | 13 // only the features used by Skia. |
14 | 14 |
15 // We check the same define to know to enable the annotations, but prefix all ou
r macros with SK_. | 15 #if SK_DYNAMIC_ANNOTATIONS_ENABLED |
16 #if DYNAMIC_ANNOTATIONS_ENABLED | |
17 | 16 |
18 extern "C" { | 17 extern "C" { |
19 // TSAN provides these hooks. | 18 // TSAN provides these hooks. |
20 void AnnotateIgnoreReadsBegin(const char* file, int line); | 19 void AnnotateIgnoreReadsBegin(const char* file, int line); |
21 void AnnotateIgnoreReadsEnd(const char* file, int line); | 20 void AnnotateIgnoreReadsEnd(const char* file, int line); |
| 21 void AnnotateBenignRaceSized(const char* file, int line, |
| 22 const volatile void* addr, long size, const char* d
esc); |
22 } // extern "C" | 23 } // extern "C" |
23 | 24 |
24 // SK_ANNOTATE_UNPROTECTED_READ can wrap any variable read to tell TSAN to ignor
e that it appears to | 25 // SK_ANNOTATE_UNPROTECTED_READ can wrap any variable read to tell TSAN to ignor
e that it appears to |
25 // be a racy read. This should be used only when we can make an external guaran
tee that though this | 26 // be a racy read. This should be used only when we can make an external guaran
tee that though this |
26 // particular read is racy, it is being used as part of a mechanism which is thr
ead safe. Examples: | 27 // particular read is racy, it is being used as part of a mechanism which is thr
ead safe. Examples: |
27 // - the first check in double-checked locking; | 28 // - the first check in double-checked locking; |
28 // - checking if a ref count is equal to 1. | 29 // - checking if a ref count is equal to 1. |
29 // Note that in both these cases, we must still add terrifyingly subtle memory b
arriers to provide | 30 // Note that in both these cases, we must still add terrifyingly subtle memory b
arriers to provide |
30 // that overall thread safety guarantee. Using this macro to shut TSAN up witho
ut providing such an | 31 // that overall thread safety guarantee. Using this macro to shut TSAN up witho
ut providing such an |
31 // external guarantee is pretty much never correct. | 32 // external guarantee is pretty much never correct. |
32 template <typename T> | 33 template <typename T> |
33 inline T SK_ANNOTATE_UNPROTECTED_READ(const volatile T& x) { | 34 inline T SK_ANNOTATE_UNPROTECTED_READ(const volatile T& x) { |
34 AnnotateIgnoreReadsBegin(__FILE__, __LINE__); | 35 AnnotateIgnoreReadsBegin(__FILE__, __LINE__); |
35 T read = x; | 36 T read = x; |
36 AnnotateIgnoreReadsEnd(__FILE__, __LINE__); | 37 AnnotateIgnoreReadsEnd(__FILE__, __LINE__); |
37 return read; | 38 return read; |
38 } | 39 } |
39 | 40 |
40 #else // !DYNAMIC_ANNOTATIONS_ENABLED | 41 // Ignore racy reads and racy writes to this pointer, indefinitely. |
| 42 // If at all possible, use the more precise SK_ANNOTATE_UNPROTECTED_READ. |
| 43 template <typename T> |
| 44 void SK_ANNOTATE_BENIGN_RACE(T* ptr) { |
| 45 AnnotateBenignRaceSized(__FILE__, __LINE__, ptr, sizeof(*ptr), "SK_ANNOTATE_
BENIGN_RACE"); |
| 46 } |
| 47 |
| 48 #else // !SK_DYNAMIC_ANNOTATIONS_ENABLED |
41 | 49 |
42 #define SK_ANNOTATE_UNPROTECTED_READ(x) (x) | 50 #define SK_ANNOTATE_UNPROTECTED_READ(x) (x) |
| 51 #define SK_ANNOTATE_BENIGN_RACE(ptr) |
43 | 52 |
44 #endif | 53 #endif |
45 | 54 |
| 55 // Can be used to wrap values that are intentionally racy, usually small mutable
cached values, e.g. |
| 56 // - SkMatrix type mask |
| 57 // - SkPathRef bounds |
| 58 // - SkPixelRef genIDs |
| 59 template <typename T> |
| 60 class SkTRacy { |
| 61 public: |
| 62 SkTRacy() { SK_ANNOTATE_BENIGN_RACE(&fVal); } |
| 63 |
| 64 operator const T&() const { |
| 65 return fVal; |
| 66 } |
| 67 |
| 68 SkTRacy& operator=(const T& val) { |
| 69 fVal = val; |
| 70 return *this; |
| 71 } |
| 72 |
| 73 const T* get() const { return &fVal; } |
| 74 T* get() { return &fVal; } |
| 75 |
| 76 const T* operator->() const { return &fVal; } |
| 77 T* operator->() { return &fVal; } |
| 78 |
| 79 private: |
| 80 T fVal; |
| 81 }; |
| 82 |
46 #endif//SkDynamicAnnotations_DEFINED | 83 #endif//SkDynamicAnnotations_DEFINED |
OLD | NEW |