OLD | NEW |
| (Empty) |
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 "base/synchronization/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-s
leep-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) || 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 base { | |
58 namespace subtle { | |
59 | |
60 void SpinLock::LockSlow() { | |
61 // The value of |kYieldProcessorTries| is cargo culted from TCMalloc, Windows | |
62 // critical section defaults, and various other recommendations. | |
63 // TODO(jschuh): Further tuning may be warranted. | |
64 static const int kYieldProcessorTries = 1000; | |
65 do { | |
66 do { | |
67 for (int count = 0; count < kYieldProcessorTries; ++count) { | |
68 // Let the processor know we're spinning. | |
69 YIELD_PROCESSOR; | |
70 if (!lock_.load(std::memory_order_relaxed) && | |
71 LIKELY(!lock_.exchange(true, std::memory_order_acquire))) | |
72 return; | |
73 } | |
74 | |
75 // Give the OS a chance to schedule something on this core. | |
76 YIELD_THREAD; | |
77 } while (lock_.load(std::memory_order_relaxed)); | |
78 } while (UNLIKELY(lock_.exchange(true, std::memory_order_acquire))); | |
79 } | |
80 | |
81 } // namespace subtle | |
82 } // namespace base | |
OLD | NEW |