| 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 // This file is not part of the public Skia API. | 8 // This file is not part of the public Skia API. |
| 9 | 9 |
| 10 #ifndef SkSpinlock_DEFINED | 10 #ifndef SkSpinlock_DEFINED |
| 11 #define SkSpinlock_DEFINED | 11 #define SkSpinlock_DEFINED |
| 12 | 12 |
| 13 #include "SkAtomics.h" | 13 #include "SkAtomics.h" |
| 14 | 14 |
| 15 #define SK_DECLARE_STATIC_SPINLOCK(name) namespace {} static SkPODSpinlock name | 15 #define SK_DECLARE_STATIC_SPINLOCK(name) namespace {} static SkPODSpinlock name |
| 16 | 16 |
| 17 // This class has no constructor and must be zero-initialized (the macro above d
oes this). | 17 // This class has no constructor and must be zero-initialized (the macro above d
oes this). |
| 18 struct SkPODSpinlock { | 18 class SkPODSpinlock { |
| 19 public: |
| 19 void acquire() { | 20 void acquire() { |
| 20 // To act as a mutex, we need an acquire barrier. | 21 // To act as a mutex, we need an acquire barrier if we take the lock. |
| 21 while(sk_atomic_exchange(&fLocked, true, sk_memory_order_acquire)) { /*s
pin*/ } | 22 if (sk_atomic_exchange(&fLocked, true, sk_memory_order_acquire)) { |
| 23 // Lock was contended. Fall back to an out-of-line spin loop. |
| 24 this->contendedAcquire(); |
| 25 } |
| 22 } | 26 } |
| 27 |
| 23 void release() { | 28 void release() { |
| 24 // To act as a mutex, we need a release barrier. | 29 // To act as a mutex, we need a release barrier. |
| 25 sk_atomic_store(&fLocked, false, sk_memory_order_release); | 30 sk_atomic_store(&fLocked, false, sk_memory_order_release); |
| 26 } | 31 } |
| 27 | 32 |
| 33 private: |
| 34 void contendedAcquire(); |
| 28 bool fLocked; | 35 bool fLocked; |
| 29 }; | 36 }; |
| 30 | 37 |
| 31 // For non-global-static use cases, this is normally what you want. | 38 // For non-global-static use cases, this is normally what you want. |
| 32 class SkSpinlock : public SkPODSpinlock { | 39 class SkSpinlock : public SkPODSpinlock { |
| 33 public: | 40 public: |
| 34 SkSpinlock() { this->release(); } | 41 SkSpinlock() { this->release(); } |
| 35 }; | 42 }; |
| 36 | 43 |
| 37 #endif//SkSpinlock_DEFINED | 44 #endif//SkSpinlock_DEFINED |
| OLD | NEW |