OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 /* |
2 // Use of this source code is governed by a BSD-style license that can be | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 // found in the LICENSE file. | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
4 | 30 |
5 #ifndef WTF_SpinLock_h | 31 #ifndef WTF_SpinLock_h |
6 #define WTF_SpinLock_h | 32 #define WTF_SpinLock_h |
7 | 33 |
8 #include "base/macros.h" | 34 #include "wtf/Compiler.h" |
9 #include "base/synchronization/spin_lock.h" | 35 #include "wtf/WTFExport.h" |
| 36 #include <atomic> |
| 37 #include <memory> |
| 38 #include <mutex> |
| 39 |
| 40 // DESCRIPTION |
| 41 // Spinlock is a simple spinlock class based on the standard CPU primitive of |
| 42 // atomic increment and decrement of an int at a given memory address. These are |
| 43 // intended only for very short duration locks and assume a system with multiple |
| 44 // cores. For any potentially longer wait you should be using a real lock. |
10 | 45 |
11 namespace WTF { | 46 namespace WTF { |
12 | 47 |
13 // WTF::SpinLock is base::SpinLock. See base/synchronization/spin_lock.h for | 48 class SpinLock { |
14 // documentation. | 49 public: |
15 using SpinLock = base::subtle::SpinLock; | 50 using Guard = std::lock_guard<SpinLock>; |
| 51 |
| 52 ALWAYS_INLINE void lock() { |
| 53 static_assert(sizeof(m_lock) == sizeof(int), |
| 54 "int and m_lock are different sizes"); |
| 55 if (LIKELY(!m_lock.exchange(true, std::memory_order_acquire))) |
| 56 return; |
| 57 lockSlow(); |
| 58 } |
| 59 |
| 60 ALWAYS_INLINE void unlock() { |
| 61 m_lock.store(false, std::memory_order_release); |
| 62 } |
| 63 |
| 64 private: |
| 65 // This is called if the initial attempt to acquire the lock fails. It's |
| 66 // slower, but has a much better scheduling and power consumption behavior. |
| 67 WTF_EXPORT void lockSlow(); |
| 68 |
| 69 std::atomic_int m_lock; |
| 70 }; |
16 | 71 |
17 } // namespace WTF | 72 } // namespace WTF |
18 | 73 |
19 using WTF::SpinLock; | 74 using WTF::SpinLock; |
20 | 75 |
21 #endif // WTF_SpinLock_h | 76 #endif // WTF_SpinLock_h |
OLD | NEW |