OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #ifndef WTF_StdLibExtras_h | 26 #ifndef WTF_StdLibExtras_h |
27 #define WTF_StdLibExtras_h | 27 #define WTF_StdLibExtras_h |
28 | 28 |
29 #include <cstddef> | |
29 #include "base/numerics/safe_conversions.h" | 30 #include "base/numerics/safe_conversions.h" |
30 #include "wtf/Assertions.h" | 31 #include "wtf/Assertions.h" |
31 #include "wtf/CPU.h" | 32 #include "wtf/CPU.h" |
32 #include "wtf/LeakAnnotations.h" | 33 #include "wtf/LeakAnnotations.h" |
34 #include "wtf/Noncopyable.h" | |
33 #include "wtf/TypeTraits.h" | 35 #include "wtf/TypeTraits.h" |
34 #include <cstddef> | |
35 | 36 |
36 #if DCHECK_IS_ON() | 37 #if DCHECK_IS_ON() |
37 #include "wtf/Noncopyable.h" | |
38 #include "wtf/Threading.h" | 38 #include "wtf/Threading.h" |
39 #endif | |
39 | 40 |
40 class WTF_EXPORT StaticLocalVerifier { | 41 // Use |DEFINE_STATIC_LOCAL()| to declare and define a static local variable |
41 WTF_MAKE_NONCOPYABLE(StaticLocalVerifier); | 42 // (|static T;|) so that it is leaked and its destructors are not called at |
43 // exit. T may also be a Blink garbage collected object, in which case it is | |
44 // wrapped up by an off-heap |Persistent<T>| reference to the object, keeping | |
45 // it alive across GCs. | |
46 // | |
47 #define DEFINE_STATIC_LOCAL(Type, Name, Arguments) \ | |
48 static WTF::StaticSingleton<Type> s_##Name( \ | |
49 new WTF::StaticSingleton<Type>::WrapperType Arguments); \ | |
50 Type& Name = s_##Name.get() | |
51 | |
52 namespace blink { | |
53 template <typename T> | |
54 class Persistent; | |
55 | |
56 } // namespace blink | |
57 | |
58 namespace WTF { | |
59 | |
60 template <typename Type> | |
61 class StaticSingleton final { | |
62 WTF_MAKE_NONCOPYABLE(StaticSingleton); | |
42 | 63 |
43 public: | 64 public: |
44 StaticLocalVerifier() | 65 template <typename T, |
45 : m_safelyInitialized(WTF::isBeforeThreadCreated()), | 66 bool = WTF::IsGarbageCollectedType<T>::value && |
46 m_thread(WTF::internal::currentThreadSyscall()) {} | 67 !WTF::IsPersistentReferenceType<T>::value> |
68 struct Wrapper { | |
69 using type = T; | |
47 | 70 |
48 bool isNotRacy() { | 71 static T& unwrap(T* singleton) { return *singleton; } |
72 }; | |
73 | |
74 template <typename T> | |
75 struct Wrapper<T, true> { | |
76 using type = blink::Persistent<T>; | |
77 | |
78 static T& unwrap(blink::Persistent<T>* singleton) { | |
79 DCHECK(singleton); | |
80 // If this assert triggers, you're supplying an empty ("()") 'Arguments' | |
81 // argument to DEFINE_STATIC_LOCAL() - it must be the heap object you wish | |
82 // to create as a static singleton and wrapped up with a Persistent | |
83 // reference. | |
84 DCHECK(*singleton); | |
85 return **singleton; | |
86 } | |
87 }; | |
88 | |
89 using WrapperType = typename Wrapper<Type>::type; | |
90 | |
91 // To cooperate with leak detection(LSan) for Blink garbage collected objects, | |
92 // the objects owned by persistent local statics will in some cases have to be | |
93 // finalized prior to leak checking. This only applies to static references to | |
94 // Blink heap objects and what they transitively hold on to. Hence the | |
95 // LEAK_SANITIZER_REGISTER_STATIC_LOCAL() use, it taking care of the grungy | |
96 // details. | |
97 | |
98 explicit StaticSingleton(WrapperType* instance) | |
99 : m_instance(LEAK_SANITIZER_REGISTER_STATIC_LOCAL(WrapperType, instance)) | |
100 #if DCHECK_IS_ON() | |
101 , | |
102 m_safelyInitialized(WTF::isBeforeThreadCreated()), | |
103 m_thread(WTF::internal::currentThreadSyscall()) | |
104 #endif | |
105 { | |
106 } | |
107 | |
108 Type& get() const { | |
109 #if DCHECK_IS_ON() | |
110 DCHECK(isNotRacy()); | |
111 #endif | |
112 return Wrapper<Type>::unwrap(m_instance); | |
113 } | |
114 | |
115 operator Type&() { return get(); } | |
116 | |
117 private: | |
118 #if DCHECK_IS_ON() | |
119 | |
120 bool isNotRacy() const { | |
49 // Make sure that this 1) is safely initialized, 2) keeps being called | 121 // Make sure that this 1) is safely initialized, 2) keeps being called |
50 // on the same thread, or 3) is called within | 122 // on the same thread, or 3) is called within |
51 // AtomicallyInitializedStatic (i.e. with a lock held). | 123 // AtomicallyInitializedStatic (i.e. with a lock held). |
52 return m_safelyInitialized || | 124 return m_safelyInitialized || |
53 m_thread == WTF::internal::currentThreadSyscall() || | 125 m_thread == WTF::internal::currentThreadSyscall() || |
54 WTF::isAtomicallyInitializedStaticMutexLockHeld(); | 126 WTF::isAtomicallyInitializedStaticMutexLockHeld(); |
dcheng
2017/02/15 08:29:56
Since we have threadsafe function-local statics by
sof
2017/02/15 08:44:57
https://codereview.chromium.org/874203002#msg11 co
| |
55 } | 127 } |
128 #endif | |
56 | 129 |
57 private: | 130 WrapperType* m_instance; |
131 #if DCHECK_IS_ON() | |
58 bool m_safelyInitialized; | 132 bool m_safelyInitialized; |
59 ThreadIdentifier m_thread; | 133 ThreadIdentifier m_thread; |
60 }; | |
61 #endif | 134 #endif |
62 | |
63 namespace blink { | |
64 template <typename T> | |
65 class Persistent; | |
66 }; | 135 }; |
67 | 136 |
68 template <typename T, | 137 } // namespace WTF |
69 bool = WTF::IsGarbageCollectedType<T>::value && | |
70 !WTF::IsPersistentReferenceType<T>::value> | |
71 class StaticLocalWrapper { | |
72 public: | |
73 using WrapType = T; | |
74 | |
75 static T& unwrap(T* singleton) { return *singleton; } | |
76 }; | |
77 | |
78 template <typename T> | |
79 class StaticLocalWrapper<T, true> { | |
80 public: | |
81 using WrapType = blink::Persistent<T>; | |
82 | |
83 static T& unwrap(blink::Persistent<T>* singleton) { | |
84 DCHECK(singleton); | |
85 // If this assert triggers, you're supplying an empty ("()") 'Arguments' | |
86 // argument to DEFINE_STATIC_LOCAL() - it must be the heap object you wish | |
87 // to create as a static singleton and wrapped up with a Persistent | |
88 // reference. | |
89 DCHECK(*singleton); | |
90 return **singleton; | |
91 } | |
92 }; | |
93 | |
94 #if DCHECK_IS_ON() | |
95 #define DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name) \ | |
96 static StaticLocalVerifier Name##StaticLocalVerifier; \ | |
97 DCHECK(Name##StaticLocalVerifier.isNotRacy()) | |
98 #else | |
99 #define DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name) | |
100 #endif | |
101 | |
102 // Use DEFINE_STATIC_LOCAL() to declare and define a static local variable | |
103 // (static T;) so that it is leaked and its destructors are not called at exit. | |
104 // T may also be a Blink garbage collected object, in which case it is | |
105 // wrapped up by an off-heap Persistent<T> reference to the object, keeping | |
106 // it alive across GCs. | |
107 // | |
108 // To cooperate with leak detection(LSan) for Blink garbage collected objects, | |
109 // the objects owned by persistent local statics will in some cases have to be | |
110 // finalized prior to leak checking. This only applies to static references to | |
111 // Blink heap objects and what they transitively hold on to. Hence the | |
112 // LEAK_SANITIZER_REGISTER_STATIC_LOCAL() use, it taking care of the grungy | |
113 // details. | |
114 // | |
115 #define DEFINE_STATIC_LOCAL(Type, Name, Arguments) \ | |
116 DEFINE_STATIC_LOCAL_CHECK_THREADSAFE_ACCESS(Name); \ | |
117 using WrappedTypeFor##Name = StaticLocalWrapper<Type>::WrapType; \ | |
118 static WrappedTypeFor##Name* WrappedInstanceFor##Name = \ | |
119 LEAK_SANITIZER_REGISTER_STATIC_LOCAL( \ | |
120 WrappedTypeFor##Name, new WrappedTypeFor##Name Arguments); \ | |
121 Type& Name = StaticLocalWrapper<Type>::unwrap(WrappedInstanceFor##Name); | |
122 | 138 |
123 // Use this to declare and define a static local pointer to a ref-counted object | 139 // Use this to declare and define a static local pointer to a ref-counted object |
124 // so that it is leaked so that the object's destructors are not called at | 140 // so that it is leaked so that the object's destructors are not called at |
125 // exit. This macro should be used with ref-counted objects rather than | 141 // exit. This macro should be used with ref-counted objects rather than |
126 // DEFINE_STATIC_LOCAL macro, as this macro does not lead to an extra memory | 142 // DEFINE_STATIC_LOCAL macro, as this macro does not lead to an extra memory |
127 // allocation. | 143 // allocation. |
128 #define DEFINE_STATIC_REF(type, name, arguments) \ | 144 #define DEFINE_STATIC_REF(type, name, arguments) \ |
129 static type* name = PassRefPtr<type>(arguments).leakRef(); | 145 static type* name = PassRefPtr<type>(arguments).leakRef(); |
130 | 146 |
131 /* | 147 /* |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 enum NotNullTag { NotNull }; | 255 enum NotNullTag { NotNull }; |
240 inline void* operator new(size_t, NotNullTag, void* location) { | 256 inline void* operator new(size_t, NotNullTag, void* location) { |
241 DCHECK(location); | 257 DCHECK(location); |
242 return location; | 258 return location; |
243 } | 259 } |
244 | 260 |
245 using WTF::bitwiseCast; | 261 using WTF::bitwiseCast; |
246 using WTF::safeCast; | 262 using WTF::safeCast; |
247 | 263 |
248 #endif // WTF_StdLibExtras_h | 264 #endif // WTF_StdLibExtras_h |
OLD | NEW |