Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
|
kinuko
2016/11/08 05:15:42
drive-by. Should these files be maybe put under b
| |
| 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 |
| 11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. | 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
| 17 * | 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef WTF_SpinLock_h | 31 #ifndef BASE_OPTIONAL_H |
| 32 #define WTF_SpinLock_h | 32 #define BASE_OPTIONAL_H |
| 33 | 33 |
| 34 #include "wtf/Compiler.h" | |
| 35 #include "wtf/WTFExport.h" | |
| 36 #include <atomic> | 34 #include <atomic> |
| 37 #include <memory> | 35 #include <memory> |
| 38 #include <mutex> | 36 #include <mutex> |
| 39 | 37 |
| 40 // DESCRIPTION | 38 #include "base/base_export.h" |
| 39 #include "base/compiler_specific.h" | |
| 40 | |
| 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 base { |
| 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 SpinLock(); | |
| 53 | |
| 52 ALWAYS_INLINE void lock() { | 54 ALWAYS_INLINE void lock() { |
| 53 static_assert(sizeof(m_lock) == sizeof(int), | 55 static_assert(sizeof(lock_) == sizeof(int), |
| 54 "int and m_lock are different sizes"); | 56 "int and lock_ are different sizes"); |
| 55 if (LIKELY(!m_lock.exchange(true, std::memory_order_acquire))) | 57 if (LIKELY(!lock_.exchange(true, std::memory_order_acquire))) |
| 56 return; | 58 return; |
| 57 lockSlow(); | 59 LockSlow(); |
| 58 } | 60 } |
| 59 | 61 |
| 60 ALWAYS_INLINE void unlock() { | 62 ALWAYS_INLINE void unlock() { lock_.store(false, std::memory_order_release); } |
| 61 m_lock.store(false, std::memory_order_release); | |
| 62 } | |
| 63 | 63 |
| 64 private: | 64 private: |
| 65 // 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 |
| 66 // slower, but has a much better scheduling and power consumption behavior. | 66 // slower, but has a much better scheduling and power consumption behavior. |
| 67 WTF_EXPORT void lockSlow(); | 67 BASE_EXPORT void LockSlow(); |
| 68 | 68 |
| 69 std::atomic_int m_lock; | 69 std::atomic_int lock_; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 } // namespace WTF | 72 } // namespace base |
| 73 | 73 |
| 74 using WTF::SpinLock; | 74 #endif // BASE_OPTIONAL_H |
| 75 | |
| 76 #endif // WTF_SpinLock_h | |
| OLD | NEW |