| 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 "wtf/SpinLock.h" |
| 6 | 6 |
| 7 #include "wtf/Atomics.h" | 7 #include "wtf/Atomics.h" |
| 8 #include "wtf/CPU.h" | 8 #include "wtf/CPU.h" |
| 9 #include "wtf/Compiler.h" | 9 #include "wtf/Compiler.h" |
| 10 | 10 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 #if OS(POSIX) | 52 #if OS(POSIX) |
| 53 #define YIELD_THREAD sched_yield() | 53 #define YIELD_THREAD sched_yield() |
| 54 #else | 54 #else |
| 55 #warning "Thread yield not supported on this OS." | 55 #warning "Thread yield not supported on this OS." |
| 56 #define YIELD_THREAD ((void)0) | 56 #define YIELD_THREAD ((void)0) |
| 57 #endif | 57 #endif |
| 58 #endif | 58 #endif |
| 59 | 59 |
| 60 namespace WTF { | 60 namespace WTF { |
| 61 | 61 |
| 62 void SpinLock::lockSlow() | 62 void SpinLock::lockSlow() { |
| 63 { | 63 // The value of kYieldProcessorTries is cargo culted from TCMalloc, Windows |
| 64 // The value of kYieldProcessorTries is cargo culted from TCMalloc, Windows | 64 // critical section defaults, and various other recommendations. |
| 65 // critical section defaults, and various other recommendations. | 65 // TODO(jschuh): Further tuning may be warranted. |
| 66 // TODO(jschuh): Further tuning may be warranted. | 66 static const int kYieldProcessorTries = 1000; |
| 67 static const int kYieldProcessorTries = 1000; | 67 do { |
| 68 do { | 68 do { |
| 69 do { | 69 for (int count = 0; count < kYieldProcessorTries; ++count) { |
| 70 for (int count = 0; count < kYieldProcessorTries; ++count) { | 70 // Let the Processor know we're spinning. |
| 71 // Let the Processor know we're spinning. | 71 YIELD_PROCESSOR; |
| 72 YIELD_PROCESSOR; | 72 if (!m_lock.load(std::memory_order_relaxed) && |
| 73 if (!m_lock.load(std::memory_order_relaxed) && LIKELY(!m_lock.ex
change(true, std::memory_order_acq_rel))) | 73 LIKELY(!m_lock.exchange(true, std::memory_order_acq_rel))) |
| 74 return; | 74 return; |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Give the OS a chance to schedule something on this core. | 77 // Give the OS a chance to schedule something on this core. |
| 78 YIELD_THREAD; | 78 YIELD_THREAD; |
| 79 } while (m_lock.load(std::memory_order_relaxed)); | 79 } while (m_lock.load(std::memory_order_relaxed)); |
| 80 } while (UNLIKELY(m_lock.exchange(true, std::memory_order_acq_rel))); | 80 } while (UNLIKELY(m_lock.exchange(true, std::memory_order_acq_rel))); |
| 81 } | 81 } |
| 82 | 82 |
| 83 } // namespace WTF | 83 } // namespace WTF |
| OLD | NEW |