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

Side by Side Diff: third_party/WebKit/Source/wtf/Threading.h

Issue 2680843006: Tidy DEFINE_(THREAD_SAFE_)STATIC_LOCAL() implementations. (Closed)
Patch Set: simplify DEFINE_THREAD_SAFE_STATIC_LOCAL() Created 3 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 17 matching lines...) Expand all
28 */ 28 */
29 29
30 #ifndef Threading_h 30 #ifndef Threading_h
31 #define Threading_h 31 #define Threading_h
32 32
33 #include "wtf/Atomics.h" 33 #include "wtf/Atomics.h"
34 #include "wtf/TypeTraits.h" 34 #include "wtf/TypeTraits.h"
35 #include "wtf/WTFExport.h" 35 #include "wtf/WTFExport.h"
36 #include <stdint.h> 36 #include <stdint.h>
37 37
38 // For portability, we do not make use of C++11 thread-safe statics, as
39 // supported by some toolchains. Make use of double-checked locking to reduce
40 // overhead. Note that this uses system-wide default lock, and cannot be used
41 // before WTF::initializeThreading() is called.
42 #define DEFINE_THREAD_SAFE_STATIC_LOCAL(T, name, initializer) \
43 static_assert(!WTF::IsGarbageCollectedType<T>::value, \
44 "Garbage collected types should not be a static local!"); \
45 /* Init to nullptr is thread-safe on all implementations. */ \
46 static void* name##Pointer = nullptr; \
47 if (!WTF::acquireLoad(&name##Pointer)) { \
48 WTF::lockAtomicallyInitializedStaticMutex(); \
49 if (!WTF::acquireLoad(&name##Pointer)) { \
50 std::remove_const<T>::type* initializerResult = initializer; \
51 WTF::releaseStore(&name##Pointer, initializerResult); \
52 } \
53 WTF::unlockAtomicallyInitializedStaticMutex(); \
54 } \
55 T& name = *static_cast<T*>(name##Pointer)
56
57 namespace WTF { 38 namespace WTF {
58 39
59 #if OS(WIN) 40 #if OS(WIN)
60 typedef uint32_t ThreadIdentifier; 41 typedef uint32_t ThreadIdentifier;
61 #else 42 #else
62 typedef intptr_t ThreadIdentifier; 43 typedef intptr_t ThreadIdentifier;
63 #endif 44 #endif
64 45
65 namespace internal { 46 namespace internal {
66 WTF_EXPORT ThreadIdentifier currentThreadSyscall(); 47 WTF_EXPORT ThreadIdentifier currentThreadSyscall();
67 } // namespace internal 48 } // namespace internal
68 49
69 WTF_EXPORT ThreadIdentifier currentThread(); 50 WTF_EXPORT ThreadIdentifier currentThread();
70 51
71 WTF_EXPORT void lockAtomicallyInitializedStaticMutex();
72 WTF_EXPORT void unlockAtomicallyInitializedStaticMutex();
73
74 #if DCHECK_IS_ON() 52 #if DCHECK_IS_ON()
75 WTF_EXPORT bool isAtomicallyInitializedStaticMutexLockHeld();
76 WTF_EXPORT bool isBeforeThreadCreated(); 53 WTF_EXPORT bool isBeforeThreadCreated();
77 WTF_EXPORT void willCreateThread(); 54 WTF_EXPORT void willCreateThread();
78 #endif 55 #endif
79 56
80 } // namespace WTF 57 } // namespace WTF
81 58
82 using WTF::ThreadIdentifier; 59 using WTF::ThreadIdentifier;
83 using WTF::currentThread; 60 using WTF::currentThread;
84 61
85 #endif // Threading_h 62 #endif // Threading_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698