Chromium Code Reviews| Index: third_party/WebKit/Source/wtf/SpinLock.h |
| diff --git a/third_party/WebKit/Source/wtf/SpinLock.h b/third_party/WebKit/Source/wtf/SpinLock.h |
| index 08724a370abb4bb6e4d861dfa267c3ea7ab89f91..12ad666b26a07cbb073b28aab4fc8bd6b1dbca74 100644 |
| --- a/third_party/WebKit/Source/wtf/SpinLock.h |
| +++ b/third_party/WebKit/Source/wtf/SpinLock.h |
| @@ -31,36 +31,48 @@ |
| #ifndef WTF_SpinLock_h |
| #define WTF_SpinLock_h |
| -// DESCRIPTION |
| -// spinLockLock() and spinLockUnlock() are simple spinlock primitives based on |
| -// the standard CPU primitive of atomic increment and decrement of an int at |
| -// a given memory address. These are intended only for very short duration locks |
| -// and assume a system with multiple cores. For any potentially longer wait you |
| -// should be using a real lock. |
| +#include "wtf/Compiler.h" |
| +#include "wtf/WTFExport.h" |
| +#include <atomic> |
| +#include <memory> |
| +#include <mutex> |
| -#include "wtf/Atomics.h" |
| +// DESCRIPTION |
| +// Spinlock is a simple spinlock class based on the standard CPU primitive of |
| +// atomic increment and decrement of an int at a given memory address. These are |
| +// intended only for very short duration locks and assume a system with multiple |
| +// cores. For any potentially longer wait you should be using a real lock. |
| namespace WTF { |
| -// This is called if the initial attempt to acquire the lock fails. It's a bit |
| -// slower, but has a much better scheduling and power consumption behavior. |
| -WTF_EXPORT void slowSpinLockLock(int volatile* lock); |
| +class SpinLock { |
| +public: |
| + using Guard = std::lock_guard<SpinLock>; |
| + |
| + ALWAYS_INLINE void lock() |
| + { |
| + static_assert(sizeof(m_lock) == sizeof(int), "int and m_lock are different sizes"); |
| + if (LIKELY(!m_lock.exchange(true))) |
|
Alexander Potapenko
2015/12/04 12:17:34
I suppose it's enough to have memory_order_acq_rel
jschuh
2015/12/04 20:45:13
Done.
|
| + return; |
| + SlowLock(); |
| + } |
| + |
| + ALWAYS_INLINE void unlock() |
| + { |
| + m_lock.store(false); |
|
Alexander Potapenko
2015/12/04 12:17:34
Should be enough to store with memory_order_releas
jschuh
2015/12/04 20:45:13
Done.
|
| + } |
| + |
| +private: |
| + // This is called if the initial attempt to acquire the lock fails. It's |
| + // slower, but has a much better scheduling and power consumption behavior. |
| + WTF_EXPORT void SlowLock(); |
|
Yuta Kitamura
2015/12/04 03:36:28
Follow Blink's naming convention.
jschuh
2015/12/04 20:45:13
Done.
|
| -ALWAYS_INLINE void spinLockLock(int volatile* lock) |
| -{ |
| - if (LIKELY(!atomicTestAndSetToOne(lock))) |
| - return; |
| - slowSpinLockLock(lock); |
| -} |
| + std::atomic<int> m_lock; |
| +}; |
| -ALWAYS_INLINE void spinLockUnlock(int volatile* lock) |
| -{ |
| - atomicSetOneToZero(lock); |
| -} |
| } // namespace WTF |
| -using WTF::spinLockLock; |
| -using WTF::spinLockUnlock; |
| +using WTF::SpinLock; |
| #endif // WTF_SpinLock_h |