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 "wtf/SpinLock.h" | |
6 | |
7 #include "wtf/Atomics.h" | |
8 #include "wtf/CPU.h" | |
9 #include "wtf/Compiler.h" | |
10 | |
11 #if OS(WIN) | |
12 #include <windows.h> | |
13 #elif OS(POSIX) | |
14 #include <sched.h> | |
15 #endif | |
16 | |
17 // 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 | |
19 // 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: | |
21 // https://software.intel.com/en-us/articles/benefitting-power-and-performance-s
leep-loops | |
22 // | |
23 // 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 | |
25 // you really should be using proper lock rather than these spinlocks. | |
26 #if OS(WIN) | |
27 #define YIELD_PROCESSOR YieldProcessor() | |
28 #define YIELD_THREAD SwitchToThread() | |
29 #elif COMPILER(GCC) || COMPILER(CLANG) | |
30 #if CPU(X86_64) || CPU(X86) | |
31 #define YIELD_PROCESSOR __asm__ __volatile__("pause") | |
32 #elif CPU(ARM) || CPU(ARM64) | |
33 #define YIELD_PROCESSOR __asm__ __volatile__("yield") | |
34 #elif CPU(MIPS) | |
35 // 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 | |
37 // targeting pre-r2, we must encode the instruction manually. | |
38 #define YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140") | |
39 #elif CPU(MIPS64) && __mips_isa_rev >= 2 | |
40 // Don't bother doing using .word here since r2 is the lowest supported mips64 | |
41 // that Chromium supports. | |
42 #define YIELD_PROCESSOR __asm__ __volatile__("pause") | |
43 #endif | |
44 #endif | |
45 | |
46 #ifndef YIELD_PROCESSOR | |
47 #warning "Processor yield not supported on this architecture." | |
48 #define YIELD_PROCESSOR ((void)0) | |
49 #endif | |
50 | |
51 #ifndef YIELD_THREAD | |
52 #if OS(POSIX) | |
53 #define YIELD_THREAD sched_yield() | |
54 #else | |
55 #warning "Thread yield not supported on this OS." | |
56 #define YIELD_THREAD ((void)0) | |
57 #endif | |
58 #endif | |
59 | |
60 namespace WTF { | |
61 | |
62 void SpinLock::lockSlow() { | |
63 // The value of kYieldProcessorTries is cargo culted from TCMalloc, Windows | |
64 // critical section defaults, and various other recommendations. | |
65 // TODO(jschuh): Further tuning may be warranted. | |
66 static const int kYieldProcessorTries = 1000; | |
67 do { | |
68 do { | |
69 for (int count = 0; count < kYieldProcessorTries; ++count) { | |
70 // Let the Processor know we're spinning. | |
71 YIELD_PROCESSOR; | |
72 if (!m_lock.load(std::memory_order_relaxed) && | |
73 LIKELY(!m_lock.exchange(true, std::memory_order_acquire))) | |
74 return; | |
75 } | |
76 | |
77 // Give the OS a chance to schedule something on this core. | |
78 YIELD_THREAD; | |
79 } while (m_lock.load(std::memory_order_relaxed)); | |
80 } while (UNLIKELY(m_lock.exchange(true, std::memory_order_acquire))); | |
81 } | |
82 | |
83 } // namespace WTF | |
OLD | NEW |