| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 // DESCRIPTION | 40 // DESCRIPTION |
| 41 // Spinlock is a simple spinlock class based on the standard CPU primitive of | 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 | 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 | 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. | 44 // cores. For any potentially longer wait you should be using a real lock. |
| 45 | 45 |
| 46 namespace WTF { | 46 namespace WTF { |
| 47 | 47 |
| 48 class SpinLock { | 48 class SpinLock { |
| 49 public: | 49 public: |
| 50 using Guard = std::lock_guard<SpinLock>; | 50 using Guard = std::lock_guard<SpinLock>; |
| 51 | 51 |
| 52 ALWAYS_INLINE void lock() | 52 ALWAYS_INLINE void lock() { |
| 53 { | 53 static_assert(sizeof(m_lock) == sizeof(int), |
| 54 static_assert(sizeof(m_lock) == sizeof(int), "int and m_lock are differe
nt sizes"); | 54 "int and m_lock are different sizes"); |
| 55 if (LIKELY(!m_lock.exchange(true, std::memory_order_acq_rel))) | 55 if (LIKELY(!m_lock.exchange(true, std::memory_order_acq_rel))) |
| 56 return; | 56 return; |
| 57 lockSlow(); | 57 lockSlow(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 ALWAYS_INLINE void unlock() | 60 ALWAYS_INLINE void unlock() { |
| 61 { | 61 m_lock.store(false, std::memory_order_release); |
| 62 m_lock.store(false, std::memory_order_release); | 62 } |
| 63 } | |
| 64 | 63 |
| 65 private: | 64 private: |
| 66 // This is called if the initial attempt to acquire the lock fails. It's | 65 // This is called if the initial attempt to acquire the lock fails. It's |
| 67 // slower, but has a much better scheduling and power consumption behavior. | 66 // slower, but has a much better scheduling and power consumption behavior. |
| 68 WTF_EXPORT void lockSlow(); | 67 WTF_EXPORT void lockSlow(); |
| 69 | 68 |
| 70 std::atomic_int m_lock; | 69 std::atomic_int m_lock; |
| 71 }; | 70 }; |
| 72 | 71 |
| 73 | 72 } // namespace WTF |
| 74 } // namespace WTF | |
| 75 | 73 |
| 76 using WTF::SpinLock; | 74 using WTF::SpinLock; |
| 77 | 75 |
| 78 #endif // WTF_SpinLock_h | 76 #endif // WTF_SpinLock_h |
| OLD | NEW |