| OLD | NEW |
| 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 20 matching lines...) Expand all Loading... |
| 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 supporte
d | 38 // For portability, we do not make use of C++11 thread-safe statics, as supporte
d |
| 39 // by some toolchains. Make use of double-checked locking to reduce overhead. | 39 // by some toolchains. Make use of double-checked locking to reduce overhead. |
| 40 #define AtomicallyInitializedStaticReferenceInternal(T, name, initializer, LOCK,
UNLOCK) \ | 40 #define AtomicallyInitializedStaticReferenceInternal(T, name, initializer, LOCK,
UNLOCK) \ |
| 41 /* Init to nullptr is thread-safe on all implementations. */ \ | 41 /* Init to nullptr is thread-safe on all implementations. */
\ |
| 42 static void* name##Pointer = nullptr; \ | 42 static void* name##Pointer = nullptr;
\ |
| 43 if (!WTF::acquireLoad(&name##Pointer)) { \ | 43 if (!WTF::acquireLoad(&name##Pointer)) {
\ |
| 44 LOCK; \ | 44 LOCK;
\ |
| 45 if (!WTF::acquireLoad(&name##Pointer)) { \ | 45 if (!WTF::acquireLoad(&name##Pointer)) {
\ |
| 46 WTF::RemoveConst<T>::Type* initializerResult = initializer; \ | 46 WTF::RemoveConst<T>::Type* initializerResult = initializer;
\ |
| 47 WTF::releaseStore(&name##Pointer, initializerResult); \ | 47 WTF::releaseStore(&name##Pointer, initializerResult);
\ |
| 48 } \ | 48 }
\ |
| 49 UNLOCK; \ | 49 UNLOCK;
\ |
| 50 } \ | 50 }
\ |
| 51 T& name = *static_cast<T*>(name##Pointer) | 51 T& name = *static_cast<T*>(name##Pointer) |
| 52 | 52 |
| 53 // Uses system-wide default lock. This version cannot be used before | 53 // Uses system-wide default lock. This version cannot be used before |
| 54 // WTF::initializeThreading() is called. | 54 // WTF::initializeThreading() is called. |
| 55 #define AtomicallyInitializedStaticReference(T, name, initializer) \ | 55 #define AtomicallyInitializedStaticReference(T, name, initializer) \ |
| 56 AtomicallyInitializedStaticReferenceInternal( \ | 56 AtomicallyInitializedStaticReferenceInternal( \ |
| 57 T, name, initializer, \ | 57 T, name, initializer, \ |
| 58 WTF::lockAtomicallyInitializedStaticMutex(), \ | 58 WTF::lockAtomicallyInitializedStaticMutex(), \ |
| 59 WTF::unlockAtomicallyInitializedStaticMutex()) | 59 WTF::unlockAtomicallyInitializedStaticMutex()) |
| 60 | 60 |
| 61 // Same as above but uses a given lock. | 61 // Same as above but uses a given lock. |
| 62 #define AtomicallyInitializedStaticReferenceWithLock(T, name, initializer, locka
ble) \ | 62 #define AtomicallyInitializedStaticReferenceWithLock(T, name, initializer, locka
ble) \ |
| 63 AtomicallyInitializedStaticReferenceInternal( \ | 63 AtomicallyInitializedStaticReferenceInternal(
\ |
| 64 T, name, initializer, lockable.lock(), lockable.unlock()); | 64 T, name, initializer, lockable.lock(), lockable.unlock()); |
| 65 | 65 |
| 66 namespace WTF { | 66 namespace WTF { |
| 67 | 67 |
| 68 #if OS(WIN) | 68 #if OS(WIN) |
| 69 typedef uint32_t ThreadIdentifier; | 69 typedef uint32_t ThreadIdentifier; |
| 70 #else | 70 #else |
| 71 typedef intptr_t ThreadIdentifier; | 71 typedef intptr_t ThreadIdentifier; |
| 72 #endif | 72 #endif |
| 73 | 73 |
| 74 WTF_EXPORT ThreadIdentifier currentThread(); | 74 WTF_EXPORT ThreadIdentifier currentThread(); |
| 75 | 75 |
| 76 WTF_EXPORT void lockAtomicallyInitializedStaticMutex(); | 76 WTF_EXPORT void lockAtomicallyInitializedStaticMutex(); |
| 77 WTF_EXPORT void unlockAtomicallyInitializedStaticMutex(); | 77 WTF_EXPORT void unlockAtomicallyInitializedStaticMutex(); |
| 78 | 78 |
| 79 #if ENABLE(ASSERT) | 79 #if ENABLE(ASSERT) |
| 80 WTF_EXPORT bool isAtomicallyInitializedStaticMutexLockHeld(); | 80 WTF_EXPORT bool isAtomicallyInitializedStaticMutexLockHeld(); |
| 81 WTF_EXPORT bool isBeforeThreadCreated(); | 81 WTF_EXPORT bool isBeforeThreadCreated(); |
| 82 WTF_EXPORT void willCreateThread(); | 82 WTF_EXPORT void willCreateThread(); |
| 83 #endif | 83 #endif |
| 84 | 84 |
| 85 } // namespace WTF | 85 } // namespace WTF |
| 86 | 86 |
| 87 using WTF::ThreadIdentifier; | 87 using WTF::ThreadIdentifier; |
| 88 using WTF::currentThread; | 88 using WTF::currentThread; |
| 89 | 89 |
| 90 #endif // Threading_h | 90 #endif // Threading_h |
| OLD | NEW |