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

Side by Side Diff: base/synchronization/spin_lock.cc

Issue 2484803003: Move WTF::SpinLock to base::SpinLock. (Closed)
Patch Set: base::SpinLock → base::subtle::SpinLock, plus a warning comment. Created 4 years, 1 month 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
« no previous file with comments | « base/synchronization/spin_lock.h ('k') | third_party/WebKit/Source/wtf/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "wtf/SpinLock.h" 5 #include "base/synchronization/spin_lock.h"
6 6
7 #include "wtf/Atomics.h" 7 #if defined(OS_WIN)
8 #include "wtf/CPU.h"
9 #include "wtf/Compiler.h"
10
11 #if OS(WIN)
12 #include <windows.h> 8 #include <windows.h>
13 #elif OS(POSIX) 9 #elif defined(OS_POSIX)
14 #include <sched.h> 10 #include <sched.h>
15 #endif 11 #endif
16 12
17 // The YIELD_PROCESSOR macro wraps an architecture specific-instruction that 13 // The YIELD_PROCESSOR macro wraps an architecture specific-instruction that
18 // informs the processor we're in a busy wait, so it can handle the branch more 14 // informs the processor we're in a busy wait, so it can handle the branch more
19 // intelligently and e.g. reduce power to our core or give more resources to the 15 // intelligently and e.g. reduce power to our core or give more resources to the
20 // other hyper-thread on this core. See the following for context: 16 // other hyper-thread on this core. See the following for context:
21 // https://software.intel.com/en-us/articles/benefitting-power-and-performance-s leep-loops 17 // https://software.intel.com/en-us/articles/benefitting-power-and-performance-s leep-loops
22 // 18 //
23 // The YIELD_THREAD macro tells the OS to relinquish our quanta. This is 19 // The YIELD_THREAD macro tells the OS to relinquish our quantum. This is
24 // basically a worst-case fallback, and if you're hitting it with any frequency 20 // basically a worst-case fallback, and if you're hitting it with any frequency
25 // you really should be using proper lock rather than these spinlocks. 21 // you really should be using a proper lock (such as |base::Lock|)rather than
26 #if OS(WIN) 22 // these spinlocks.
23 #if defined(OS_WIN)
27 #define YIELD_PROCESSOR YieldProcessor() 24 #define YIELD_PROCESSOR YieldProcessor()
28 #define YIELD_THREAD SwitchToThread() 25 #define YIELD_THREAD SwitchToThread()
29 #elif COMPILER(GCC) || COMPILER(CLANG) 26 #elif defined(COMPILER_GCC) || defined(__clang__)
30 #if CPU(X86_64) || CPU(X86) 27 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_X86)
31 #define YIELD_PROCESSOR __asm__ __volatile__("pause") 28 #define YIELD_PROCESSOR __asm__ __volatile__("pause")
32 #elif CPU(ARM) || CPU(ARM64) 29 #elif defined(ARCH_CPU_ARMEL) || defined(ARCH_CPU_ARM64)
33 #define YIELD_PROCESSOR __asm__ __volatile__("yield") 30 #define YIELD_PROCESSOR __asm__ __volatile__("yield")
34 #elif CPU(MIPS) 31 #elif defined(ARCH_CPU_MIPSEL)
35 // The MIPS32 docs state that the PAUSE instruction is a no-op on older 32 // The MIPS32 docs state that the PAUSE instruction is a no-op on older
36 // architectures (first added in MIPS32r2). To avoid assembler errors when 33 // architectures (first added in MIPS32r2). To avoid assembler errors when
37 // targeting pre-r2, we must encode the instruction manually. 34 // targeting pre-r2, we must encode the instruction manually.
38 #define YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140") 35 #define YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140")
39 #elif CPU(MIPS64) && __mips_isa_rev >= 2 36 #elif defined(ARCH_CPU_MIPS64EL) && __mips_isa_rev >= 2
40 // Don't bother doing using .word here since r2 is the lowest supported mips64 37 // Don't bother doing using .word here since r2 is the lowest supported mips64
41 // that Chromium supports. 38 // that Chromium supports.
42 #define YIELD_PROCESSOR __asm__ __volatile__("pause") 39 #define YIELD_PROCESSOR __asm__ __volatile__("pause")
43 #endif 40 #endif
44 #endif 41 #endif
45 42
46 #ifndef YIELD_PROCESSOR 43 #ifndef YIELD_PROCESSOR
47 #warning "Processor yield not supported on this architecture." 44 #warning "Processor yield not supported on this architecture."
48 #define YIELD_PROCESSOR ((void)0) 45 #define YIELD_PROCESSOR ((void)0)
49 #endif 46 #endif
50 47
51 #ifndef YIELD_THREAD 48 #ifndef YIELD_THREAD
52 #if OS(POSIX) 49 #if defined(OS_POSIX)
53 #define YIELD_THREAD sched_yield() 50 #define YIELD_THREAD sched_yield()
54 #else 51 #else
55 #warning "Thread yield not supported on this OS." 52 #warning "Thread yield not supported on this OS."
56 #define YIELD_THREAD ((void)0) 53 #define YIELD_THREAD ((void)0)
57 #endif 54 #endif
58 #endif 55 #endif
59 56
60 namespace WTF { 57 namespace base {
58 namespace subtle {
61 59
62 void SpinLock::lockSlow() { 60 void SpinLock::LockSlow() {
63 // The value of kYieldProcessorTries is cargo culted from TCMalloc, Windows 61 // The value of |kYieldProcessorTries| is cargo culted from TCMalloc, Windows
64 // critical section defaults, and various other recommendations. 62 // critical section defaults, and various other recommendations.
65 // TODO(jschuh): Further tuning may be warranted. 63 // TODO(jschuh): Further tuning may be warranted.
66 static const int kYieldProcessorTries = 1000; 64 static const int kYieldProcessorTries = 1000;
67 do { 65 do {
68 do { 66 do {
69 for (int count = 0; count < kYieldProcessorTries; ++count) { 67 for (int count = 0; count < kYieldProcessorTries; ++count) {
70 // Let the Processor know we're spinning. 68 // Let the processor know we're spinning.
71 YIELD_PROCESSOR; 69 YIELD_PROCESSOR;
72 if (!m_lock.load(std::memory_order_relaxed) && 70 if (!lock_.load(std::memory_order_relaxed) &&
73 LIKELY(!m_lock.exchange(true, std::memory_order_acquire))) 71 LIKELY(!lock_.exchange(true, std::memory_order_acquire)))
74 return; 72 return;
75 } 73 }
76 74
77 // Give the OS a chance to schedule something on this core. 75 // Give the OS a chance to schedule something on this core.
78 YIELD_THREAD; 76 YIELD_THREAD;
79 } while (m_lock.load(std::memory_order_relaxed)); 77 } while (lock_.load(std::memory_order_relaxed));
80 } while (UNLIKELY(m_lock.exchange(true, std::memory_order_acquire))); 78 } while (UNLIKELY(lock_.exchange(true, std::memory_order_acquire)));
81 } 79 }
82 80
83 } // namespace WTF 81 } // namespace subtle
82 } // namespace base
OLDNEW
« no previous file with comments | « base/synchronization/spin_lock.h ('k') | third_party/WebKit/Source/wtf/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698