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 <atomic> | 12 #include <atomic> |
12 | 13 |
13 class SkSpinlock { | 14 class SkSpinlock { |
14 public: | 15 public: |
15 void acquire() { | 16 void acquire() { |
16 // To act as a mutex, we need an acquire barrier when we acquire the loc
k. | 17 // To act as a mutex, we need an acquire barrier when we acquire the loc
k. |
17 if (fLocked.exchange(true, std::memory_order_acquire)) { | 18 if (fLocked.exchange(true, std::memory_order_acquire)) { |
18 // Lock was contended. Fall back to an out-of-line spin loop. | 19 // Lock was contended. Fall back to an out-of-line spin loop. |
19 this->contendedAcquire(); | 20 this->contendedAcquire(); |
20 } | 21 } |
21 } | 22 } |
22 | 23 |
23 void release() { | 24 void release() { |
24 // To act as a mutex, we need a release barrier when we release the lock
. | 25 // To act as a mutex, we need a release barrier when we release the lock
. |
25 fLocked.store(false, std::memory_order_release); | 26 fLocked.store(false, std::memory_order_release); |
26 } | 27 } |
27 | 28 |
28 private: | 29 private: |
29 void contendedAcquire(); | 30 SK_API void contendedAcquire(); |
30 | 31 |
31 std::atomic<bool> fLocked{false}; | 32 std::atomic<bool> fLocked{false}; |
32 }; | 33 }; |
33 | 34 |
34 #endif//SkSpinlock_DEFINED | 35 #endif//SkSpinlock_DEFINED |
OLD | NEW |