| OLD | NEW |
| 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/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 quanta. 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 proper lock rather than these spinlocks. |
| 26 #if OS(WIN) | 22 #if defined(OS_WIN) |
| 27 #define YIELD_PROCESSOR YieldProcessor() | 23 #define YIELD_PROCESSOR YieldProcessor() |
| 28 #define YIELD_THREAD SwitchToThread() | 24 #define YIELD_THREAD SwitchToThread() |
| 29 #elif COMPILER(GCC) || COMPILER(CLANG) | 25 #elif defined(COMPILER_GCC) || defined(__clang__) |
| 30 #if CPU(X86_64) || CPU(X86) | 26 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_X86) |
| 31 #define YIELD_PROCESSOR __asm__ __volatile__("pause") | 27 #define YIELD_PROCESSOR __asm__ __volatile__("pause") |
| 32 #elif CPU(ARM) || CPU(ARM64) | 28 #elif defined(ARCH_CPU_ARMEL) || defined(ARCH_CPU_ARM64) |
| 33 #define YIELD_PROCESSOR __asm__ __volatile__("yield") | 29 #define YIELD_PROCESSOR __asm__ __volatile__("yield") |
| 34 #elif CPU(MIPS) | 30 #elif defined(ARCH_CPU_MIPSEL) |
| 35 // The MIPS32 docs state that the PAUSE instruction is a no-op on older | 31 // 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 | 32 // architectures (first added in MIPS32r2). To avoid assembler errors when |
| 37 // targeting pre-r2, we must encode the instruction manually. | 33 // targeting pre-r2, we must encode the instruction manually. |
| 38 #define YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140") | 34 #define YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140") |
| 39 #elif CPU(MIPS64) && __mips_isa_rev >= 2 | 35 #elif defined(ARCH_CPU_MIPS64EL) && __mips_isa_rev >= 2 |
| 40 // Don't bother doing using .word here since r2 is the lowest supported mips64 | 36 // Don't bother doing using .word here since r2 is the lowest supported mips64 |
| 41 // that Chromium supports. | 37 // that Chromium supports. |
| 42 #define YIELD_PROCESSOR __asm__ __volatile__("pause") | 38 #define YIELD_PROCESSOR __asm__ __volatile__("pause") |
| 43 #endif | 39 #endif |
| 44 #endif | 40 #endif |
| 45 | 41 |
| 46 #ifndef YIELD_PROCESSOR | 42 #ifndef YIELD_PROCESSOR |
| 47 #warning "Processor yield not supported on this architecture." | 43 #warning "Processor yield not supported on this architecture." |
| 48 #define YIELD_PROCESSOR ((void)0) | 44 #define YIELD_PROCESSOR ((void)0) |
| 49 #endif | 45 #endif |
| 50 | 46 |
| 51 #ifndef YIELD_THREAD | 47 #ifndef YIELD_THREAD |
| 52 #if OS(POSIX) | 48 #if defined(OS_POSIX) |
| 53 #define YIELD_THREAD sched_yield() | 49 #define YIELD_THREAD sched_yield() |
| 54 #else | 50 #else |
| 55 #warning "Thread yield not supported on this OS." | 51 #warning "Thread yield not supported on this OS." |
| 56 #define YIELD_THREAD ((void)0) | 52 #define YIELD_THREAD ((void)0) |
| 57 #endif | 53 #endif |
| 58 #endif | 54 #endif |
| 59 | 55 |
| 60 namespace WTF { | 56 namespace base { |
| 61 | 57 |
| 62 void SpinLock::lockSlow() { | 58 SpinLock::SpinLock() {} |
| 59 |
| 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 base |
| OLD | NEW |