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 3 // found in the LICENSE file. 4 5 #include "third_party/base/allocator/partition_allocator/spin_lock.h" 6 7 #if defined(OS_WIN) 8 #include <windows.h> 9 #elif defined(OS_POSIX) 10 #include <sched.h> 11 #endif 12 13 // The YIELD_PROCESSOR macro wraps an architecture specific-instruction that 14 // informs the processor we're in a busy wait, so it can handle the branch more 15 // intelligently and e.g. reduce power to our core or give more resources to the 16 // other hyper-thread on this core. See the following for context: 17 // https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops 18 // 19 // The YIELD_THREAD macro tells the OS to relinquish our quantum. This is 20 // basically a worst-case fallback, and if you're hitting it with any frequency 21 // you really should be using a proper lock (such as |base::Lock|)rather than 22 // these spinlocks. 23 #if defined(OS_WIN) 24 #define YIELD_PROCESSOR YieldProcessor() 25 #define YIELD_THREAD SwitchToThread() 26 #elif defined(COMPILER_GCC) || defined(__clang__) 27 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_X86) 28 #define YIELD_PROCESSOR __asm__ __volatile__("pause") 29 #elif (defined(ARCH_CPU_ARMEL) && __ARM_ARCH >= 6) || defined(ARCH_CPU_ARM64) 30 #define YIELD_PROCESSOR __asm__ __volatile__("yield") 31 #elif defined(ARCH_CPU_MIPSEL) 32 // The MIPS32 docs state that the PAUSE instruction is a no-op on older 33 // architectures (first added in MIPS32r2). To avoid assembler errors when 34 // targeting pre-r2, we must encode the instruction manually. 35 #define YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140") 36 #elif defined(ARCH_CPU_MIPS64EL) && __mips_isa_rev >= 2 37 // Don't bother doing using .word here since r2 is the lowest supported mips64 38 // that Chromium supports. 39 #define YIELD_PROCESSOR __asm__ __volatile__("pause") 40 #endif 41 #endif 42 43 #ifndef YIELD_PROCESSOR 44 #warning "Processor yield not supported on this architecture." 45 #define YIELD_PROCESSOR ((void)0) 46 #endif 47 48 #ifndef YIELD_THREAD 49 #if defined(OS_POSIX) 50 #define YIELD_THREAD sched_yield() 51 #else 52 #warning "Thread yield not supported on this OS." 53 #define YIELD_THREAD ((void)0) 54 #endif 55 #endif 56 57 namespace pdfium { 58 namespace base { 59 namespace subtle { 60 LockSlow()61void SpinLock::LockSlow() { 62 // The value of |kYieldProcessorTries| is cargo culted from TCMalloc, Windows 63 // critical section defaults, and various other recommendations. 64 // TODO(jschuh): Further tuning may be warranted. 65 static const int kYieldProcessorTries = 1000; 66 do { 67 do { 68 for (int count = 0; count < kYieldProcessorTries; ++count) { 69 // Let the processor know we're spinning. 70 YIELD_PROCESSOR; 71 if (!lock_.load(std::memory_order_relaxed) && 72 LIKELY(!lock_.exchange(true, std::memory_order_acquire))) 73 return; 74 } 75 76 // Give the OS a chance to schedule something on this core. 77 YIELD_THREAD; 78 } while (lock_.load(std::memory_order_relaxed)); 79 } while (UNLIKELY(lock_.exchange(true, std::memory_order_acquire))); 80 } 81 82 } // namespace subtle 83 } // namespace base 84 } // namespace pdfium 85