2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkDynamicAnnotations_DEFINED
9 #define SkDynamicAnnotations_DEFINED
11 // This file contains macros used to send out-of-band signals to dynamic instrumentation systems,
12 // namely thread sanitizer. This is a cut-down version of the full dynamic_annotations library with
13 // only the features used by Skia.
15 #if SK_DYNAMIC_ANNOTATIONS_ENABLED
18 // TSAN provides these hooks.
19 void AnnotateIgnoreReadsBegin(const char* file, int line);
20 void AnnotateIgnoreReadsEnd(const char* file, int line);
21 void AnnotateIgnoreWritesBegin(const char* file, int line);
22 void AnnotateIgnoreWritesEnd(const char* file, int line);
23 void AnnotateBenignRaceSized(const char* file, int line,
24 const volatile void* addr, long size, const char* desc);
27 // SK_ANNOTATE_UNPROTECTED_READ can wrap any variable read to tell TSAN to ignore that it appears to
28 // be a racy read. This should be used only when we can make an external guarantee that though this
29 // particular read is racy, it is being used as part of a mechanism which is thread safe. Examples:
30 // - the first check in double-checked locking;
31 // - checking if a ref count is equal to 1.
32 // Note that in both these cases, we must still add terrifyingly subtle memory barriers to provide
33 // that overall thread safety guarantee. Using this macro to shut TSAN up without providing such an
34 // external guarantee is pretty much never correct.
36 inline T SK_ANNOTATE_UNPROTECTED_READ(const volatile T& x) {
37 AnnotateIgnoreReadsBegin(__FILE__, __LINE__);
39 AnnotateIgnoreReadsEnd(__FILE__, __LINE__);
43 // Like SK_ANNOTATE_UNPROTECTED_READ, but for writes.
45 inline void SK_ANNOTATE_UNPROTECTED_WRITE(T* ptr, const T& val) {
46 AnnotateIgnoreWritesBegin(__FILE__, __LINE__);
48 AnnotateIgnoreWritesEnd(__FILE__, __LINE__);
51 // Ignore racy reads and racy writes to this pointer, indefinitely.
52 // If at all possible, use the more precise SK_ANNOTATE_UNPROTECTED_READ.
54 void SK_ANNOTATE_BENIGN_RACE(T* ptr) {
55 AnnotateBenignRaceSized(__FILE__, __LINE__, ptr, sizeof(*ptr), "SK_ANNOTATE_BENIGN_RACE");
58 #else // !SK_DYNAMIC_ANNOTATIONS_ENABLED
60 #define SK_ANNOTATE_UNPROTECTED_READ(x) (x)
61 #define SK_ANNOTATE_UNPROTECTED_WRITE(ptr, val) *(ptr) = (val)
62 #define SK_ANNOTATE_BENIGN_RACE(ptr)
66 // Can be used to wrap values that are intentionally racy, usually small mutable cached values, e.g.
67 // - SkMatrix type mask
68 // - SkPixelRef genIDs
72 operator const T() const {
73 return SK_ANNOTATE_UNPROTECTED_READ(fVal);
76 SkTRacy& operator=(const T& val) {
77 SK_ANNOTATE_UNPROTECTED_WRITE(&fVal, val);
85 // This is like SkTRacy, but allows you to return the value by reference.
86 // TSAN is better at suppressing SkTRacy than SkTRacyReffable, so use SkTRacy when possible.
88 // We use this for SkPathRef bounds, which is an SkRect we pass around by reference publically.
90 class SkTRacyReffable {
92 SkTRacyReffable() { SK_ANNOTATE_BENIGN_RACE(&fVal); }
94 operator const T&() const {
98 SkTRacyReffable& operator=(const T& val) {
103 const T* get() const { return &fVal; }
104 T* get() { return &fVal; }
106 const T* operator->() const { return &fVal; }
107 T* operator->() { return &fVal; }
113 #endif//SkDynamicAnnotations_DEFINED