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

Unified Diff: third_party/WebKit/Source/wtf/SpinLock.h

Issue 1463683002: Switch wtf/SpinLock to std::atomic (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: asan iwyu Created 5 years 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 side-by-side diff with in-line comments
Download patch
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..b8d6b9c65ed1188e8abae37f2fed0e69d833bf8f 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, std::memory_order_acq_rel)))
+ return;
+ lockSlow();
+ }
+
+ ALWAYS_INLINE void unlock()
+ {
+ m_lock.store(false, std::memory_order_release);
+ }
+
+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 lockSlow();
-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

Powered by Google App Engine
This is Rietveld 408576698