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

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

Issue 1502023002: Revert of Switch wtf/SpinLock to std::atomic (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef WTF_SpinLock_h 31 #ifndef WTF_SpinLock_h
32 #define WTF_SpinLock_h 32 #define WTF_SpinLock_h
33 33
34 #include "wtf/Compiler.h" 34 // DESCRIPTION
35 #include "wtf/WTFExport.h" 35 // spinLockLock() and spinLockUnlock() are simple spinlock primitives based on
36 #include <atomic> 36 // the standard CPU primitive of atomic increment and decrement of an int at
37 #include <memory> 37 // a given memory address. These are intended only for very short duration locks
38 #include <mutex> 38 // and assume a system with multiple cores. For any potentially longer wait you
39 // should be using a real lock.
39 40
40 // DESCRIPTION 41 #include "wtf/Atomics.h"
41 // Spinlock is a simple spinlock class based on the standard CPU primitive of
42 // atomic increment and decrement of an int at a given memory address. These are
43 // intended only for very short duration locks and assume a system with multiple
44 // cores. For any potentially longer wait you should be using a real lock.
45 42
46 namespace WTF { 43 namespace WTF {
47 44
48 class SpinLock { 45 // This is called if the initial attempt to acquire the lock fails. It's a bit
49 public: 46 // slower, but has a much better scheduling and power consumption behavior.
50 using Guard = std::lock_guard<SpinLock>; 47 WTF_EXPORT void slowSpinLockLock(int volatile* lock);
51 48
52 ALWAYS_INLINE void lock() 49 ALWAYS_INLINE void spinLockLock(int volatile* lock)
53 { 50 {
54 static_assert(sizeof(m_lock) == sizeof(int), "int and m_lock are differe nt sizes"); 51 if (LIKELY(!atomicTestAndSetToOne(lock)))
55 if (LIKELY(!m_lock.exchange(true, std::memory_order_acq_rel))) 52 return;
56 return; 53 slowSpinLockLock(lock);
57 lockSlow(); 54 }
58 }
59 55
60 ALWAYS_INLINE void unlock() 56 ALWAYS_INLINE void spinLockUnlock(int volatile* lock)
61 { 57 {
62 m_lock.store(false, std::memory_order_release); 58 atomicSetOneToZero(lock);
63 } 59 }
64
65 private:
66 // This is called if the initial attempt to acquire the lock fails. It's
67 // slower, but has a much better scheduling and power consumption behavior.
68 WTF_EXPORT void lockSlow();
69
70 std::atomic<int> m_lock;
71 };
72
73 60
74 } // namespace WTF 61 } // namespace WTF
75 62
76 using WTF::SpinLock; 63 using WTF::spinLockLock;
64 using WTF::spinLockUnlock;
77 65
78 #endif // WTF_SpinLock_h 66 #endif // WTF_SpinLock_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Partitions.cpp ('k') | third_party/WebKit/Source/wtf/SpinLock.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698