| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkSpinlock_DEFINED | 8 #ifndef SkSpinlock_DEFINED |
| 9 #define SkSpinlock_DEFINED | 9 #define SkSpinlock_DEFINED |
| 10 | 10 |
| 11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
| 12 #include <atomic> | 12 #include <atomic> |
| 13 | 13 |
| 14 class SkSpinlock { | 14 class SkSpinlock { |
| 15 public: | 15 public: |
| 16 constexpr SkSpinlock() = default; |
| 17 |
| 16 void acquire() { | 18 void acquire() { |
| 17 // To act as a mutex, we need an acquire barrier when we acquire the loc
k. | 19 // To act as a mutex, we need an acquire barrier when we acquire the loc
k. |
| 18 if (fLocked.exchange(true, std::memory_order_acquire)) { | 20 if (fLocked.exchange(true, std::memory_order_acquire)) { |
| 19 // Lock was contended. Fall back to an out-of-line spin loop. | 21 // Lock was contended. Fall back to an out-of-line spin loop. |
| 20 this->contendedAcquire(); | 22 this->contendedAcquire(); |
| 21 } | 23 } |
| 22 } | 24 } |
| 23 | 25 |
| 24 void release() { | 26 void release() { |
| 25 // To act as a mutex, we need a release barrier when we release the lock
. | 27 // To act as a mutex, we need a release barrier when we release the lock
. |
| 26 fLocked.store(false, std::memory_order_release); | 28 fLocked.store(false, std::memory_order_release); |
| 27 } | 29 } |
| 28 | 30 |
| 29 private: | 31 private: |
| 30 SK_API void contendedAcquire(); | 32 SK_API void contendedAcquire(); |
| 31 | 33 |
| 32 std::atomic<bool> fLocked{false}; | 34 std::atomic<bool> fLocked{false}; |
| 33 }; | 35 }; |
| 34 | 36 |
| 35 #endif//SkSpinlock_DEFINED | 37 #endif//SkSpinlock_DEFINED |
| OLD | NEW |