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

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: addressed eric's comments 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
« no previous file with comments | « Source/wtf/MainThread.cpp ('k') | Source/wtf/Threading.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/Threading.h"
35
36 class WTF_EXPORT StaticLocalVerifier {
37 public:
jochen (gone - plz use gerrit) 2015/02/04 12:41:05 add WTF_MAKE_NONCOPYABLE?
kinuko 2015/02/05 08:58:25 Done.
38 StaticLocalVerifier()
39 : m_safelyInitialized(WTF::isBeforeThreadCreated())
40 , m_thread(WTF::currentThread())
41 {
42 }
43
44 bool isNotRacy()
45 {
46 // Make sure that this 1) is safely initialized, 2) keeps being called
47 // on the same thread, or 3) is called within
48 // AtomicallyInitializedStatic (i.e. with a lock held).
49 return m_safelyInitialized || m_thread == WTF::currentThread() || WTF::i sAtomicallyInitializedStaticMutexLockHeld();
50 }
51
52 private:
53 bool m_safelyInitialized;
54 ThreadIdentifier m_thread;
55 };
56 #endif
57
33 // Use this to declare and define a static local variable (static T;) so that 58 // 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. 59 // it is leaked so that its destructors are not called at exit.
35 #ifndef DEFINE_STATIC_LOCAL 60 #ifndef DEFINE_STATIC_LOCAL
61
62 #if ENABLE(ASSERT)
63 #define DEFINE_STATIC_LOCAL(type, name, arguments) \
64 static StaticLocalVerifier name##StaticLocalVerifier; \
65 ASSERT(name##StaticLocalVerifier.isNotRacy()); \
66 static type& name = *new type arguments
67 #else
36 #define DEFINE_STATIC_LOCAL(type, name, arguments) \ 68 #define DEFINE_STATIC_LOCAL(type, name, arguments) \
37 static type& name = *new type arguments 69 static type& name = *new type arguments
38 #endif 70 #endif
39 71
72 // Does the same as DEFINE_STATIC_LOCAL but without assertions.
73 // Use this when you are absolutely sure that it is safe but above
74 // assertions fail (e.g. called on multiple thread with a local lock).
75 #define DEFINE_STATIC_LOCAL_NOASSERT(type, name, arguments) \
76 static type& name = *new type arguments
77 #endif
78
79
40 // Use this to declare and define a static local pointer to a ref-counted object so that 80 // 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. 81 // 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, 82 // 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. 83 // as this macro does not lead to an extra memory allocation.
44 #ifndef DEFINE_STATIC_REF 84 #ifndef DEFINE_STATIC_REF
45 #define DEFINE_STATIC_REF(type, name, arguments) \ 85 #define DEFINE_STATIC_REF(type, name, arguments) \
46 static type* name = PassRefPtr<type>(arguments).leakRef(); 86 static type* name = PassRefPtr<type>(arguments).leakRef();
47 #endif 87 #endif
48 88
49 // Use this macro to declare and define a debug-only global variable that may ha ve a 89 // 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) 183 inline void* operator new(size_t, NotNullTag, void* location)
144 { 184 {
145 ASSERT(location); 185 ASSERT(location);
146 return location; 186 return location;
147 } 187 }
148 188
149 using WTF::bitwise_cast; 189 using WTF::bitwise_cast;
150 using WTF::safeCast; 190 using WTF::safeCast;
151 191
152 #endif // WTF_StdLibExtras_h 192 #endif // WTF_StdLibExtras_h
OLDNEW
« no previous file with comments | « Source/wtf/MainThread.cpp ('k') | Source/wtf/Threading.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698