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

Side by Side 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: sync again 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"
35 #include "wtf/WTFExport.h"
36 #include <atomic>
37 #include <memory>
38 #include <mutex>
39
34 // DESCRIPTION 40 // DESCRIPTION
35 // spinLockLock() and spinLockUnlock() are simple spinlock primitives based on 41 // Spinlock is a simple spinlock class based on the standard CPU primitive of
36 // the standard CPU primitive of atomic increment and decrement of an int at 42 // atomic increment and decrement of an int at a given memory address. These are
37 // a given memory address. These are intended only for very short duration locks 43 // intended only for very short duration locks and assume a system with multiple
38 // and assume a system with multiple cores. For any potentially longer wait you 44 // cores. For any potentially longer wait you should be using a real lock.
39 // should be using a real lock.
40
41 #include "wtf/Atomics.h"
42 45
43 namespace WTF { 46 namespace WTF {
44 47
45 // This is called if the initial attempt to acquire the lock fails. It's a bit 48 class SpinLock {
46 // slower, but has a much better scheduling and power consumption behavior. 49 public:
47 WTF_EXPORT void slowSpinLockLock(int volatile* lock); 50 using Guard = std::lock_guard<SpinLock>;
48 51
49 ALWAYS_INLINE void spinLockLock(int volatile* lock) 52 ALWAYS_INLINE void lock()
50 { 53 {
51 if (LIKELY(!atomicTestAndSetToOne(lock))) 54 static_assert(sizeof(m_lock) == sizeof(int), "int and m_lock are differe nt sizes");
52 return; 55 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.
53 slowSpinLockLock(lock); 56 return;
54 } 57 SlowLock();
58 }
55 59
56 ALWAYS_INLINE void spinLockUnlock(int volatile* lock) 60 ALWAYS_INLINE void unlock()
57 { 61 {
58 atomicSetOneToZero(lock); 62 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.
59 } 63 }
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 SlowLock();
Yuta Kitamura 2015/12/04 03:36:28 Follow Blink's naming convention.
jschuh 2015/12/04 20:45:13 Done.
69
70 std::atomic<int> m_lock;
71 };
72
60 73
61 } // namespace WTF 74 } // namespace WTF
62 75
63 using WTF::spinLockLock; 76 using WTF::SpinLock;
64 using WTF::spinLockUnlock;
65 77
66 #endif // WTF_SpinLock_h 78 #endif // WTF_SpinLock_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698