Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(810)

Side by Side Diff: Source/wtf/StdLibExtras.h

Issue 874203002: Add a thread-safety assertion in DEFINE_STATIC_LOCAL (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: added noncopyable Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
(...skipping 12 matching lines...) Expand all
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 "wtf/Assertions.h" 29 #include "wtf/Assertions.h"
30 #include "wtf/CPU.h" 30 #include "wtf/CPU.h"
31 #include "wtf/CheckedArithmetic.h" 31 #include "wtf/CheckedArithmetic.h"
32 32
33 #if ENABLE(ASSERT)
34 #include "wtf/Noncopyable.h"
35 #include "wtf/Threading.h"
36
37 class WTF_EXPORT StaticLocalVerifier {
38 WTF_MAKE_NONCOPYABLE(StaticLocalVerifier);
39 public:
40 StaticLocalVerifier()
41 : m_safelyInitialized(WTF::isBeforeThreadCreated())
42 , m_thread(WTF::currentThread())
43 {
44 }
45
46 bool isNotRacy()
47 {
48 // Make sure that this 1) is safely initialized, 2) keeps being called
49 // on the same thread, or 3) is called within
50 // AtomicallyInitializedStatic (i.e. with a lock held).
51 return m_safelyInitialized || m_thread == WTF::currentThread() || WTF::i sAtomicallyInitializedStaticMutexLockHeld();
52 }
53
54 private:
55 bool m_safelyInitialized;
56 ThreadIdentifier m_thread;
57 };
58 #endif
59
33 // Use this to declare and define a static local variable (static T;) so that 60 // Use this to declare and define a static local variable (static T;) so that
34 // it is leaked so that its destructors are not called at exit. 61 // it is leaked so that its destructors are not called at exit.
35 #ifndef DEFINE_STATIC_LOCAL 62 #ifndef DEFINE_STATIC_LOCAL
63
64 #if ENABLE(ASSERT)
65 #define DEFINE_STATIC_LOCAL(type, name, arguments) \
66 static StaticLocalVerifier name##StaticLocalVerifier; \
67 ASSERT(name##StaticLocalVerifier.isNotRacy()); \
68 static type& name = *new type arguments
69 #else
36 #define DEFINE_STATIC_LOCAL(type, name, arguments) \ 70 #define DEFINE_STATIC_LOCAL(type, name, arguments) \
37 static type& name = *new type arguments 71 static type& name = *new type arguments
38 #endif 72 #endif
39 73
74 // Does the same as DEFINE_STATIC_LOCAL but without assertions.
75 // Use this when you are absolutely sure that it is safe but above
76 // assertions fail (e.g. called on multiple thread with a local lock).
77 #define DEFINE_STATIC_LOCAL_NOASSERT(type, name, arguments) \
78 static type& name = *new type arguments
79 #endif
80
81
40 // Use this to declare and define a static local pointer to a ref-counted object so that 82 // Use this to declare and define a static local pointer to a ref-counted object so that
41 // it is leaked so that the object's destructors are not called at exit. 83 // it is leaked so that the object's destructors are not called at exit.
42 // This macro should be used with ref-counted objects rather than DEFINE_STATIC_ LOCAL macro, 84 // This macro should be used with ref-counted objects rather than DEFINE_STATIC_ LOCAL macro,
43 // as this macro does not lead to an extra memory allocation. 85 // as this macro does not lead to an extra memory allocation.
44 #ifndef DEFINE_STATIC_REF 86 #ifndef DEFINE_STATIC_REF
45 #define DEFINE_STATIC_REF(type, name, arguments) \ 87 #define DEFINE_STATIC_REF(type, name, arguments) \
46 static type* name = PassRefPtr<type>(arguments).leakRef(); 88 static type* name = PassRefPtr<type>(arguments).leakRef();
47 #endif 89 #endif
48 90
49 // Use this macro to declare and define a debug-only global variable that may ha ve a 91 // Use this macro to declare and define a debug-only global variable that may ha ve a
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 inline void* operator new(size_t, NotNullTag, void* location) 185 inline void* operator new(size_t, NotNullTag, void* location)
144 { 186 {
145 ASSERT(location); 187 ASSERT(location);
146 return location; 188 return location;
147 } 189 }
148 190
149 using WTF::bitwise_cast; 191 using WTF::bitwise_cast;
150 using WTF::safeCast; 192 using WTF::safeCast;
151 193
152 #endif // WTF_StdLibExtras_h 194 #endif // WTF_StdLibExtras_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698