OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
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 SkThread_DEFINED | 8 #ifndef SkThread_DEFINED |
9 #define SkThread_DEFINED | 9 #define SkThread_DEFINED |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 * This must act as a release (SL/S) memory barrier and as a compiler barrier. | 26 * This must act as a release (SL/S) memory barrier and as a compiler barrier. |
27 */ | 27 */ |
28 static int32_t sk_atomic_dec(int32_t* addr); | 28 static int32_t sk_atomic_dec(int32_t* addr); |
29 | 29 |
30 /** Atomically adds one to the int referenced by addr iff the referenced int was
not 0 | 30 /** Atomically adds one to the int referenced by addr iff the referenced int was
not 0 |
31 * and returns the previous value. | 31 * and returns the previous value. |
32 * No additional memory barrier is required; this must act as a compiler barrie
r. | 32 * No additional memory barrier is required; this must act as a compiler barrie
r. |
33 */ | 33 */ |
34 static int32_t sk_atomic_conditional_inc(int32_t* addr); | 34 static int32_t sk_atomic_conditional_inc(int32_t* addr); |
35 | 35 |
| 36 /** Atomic compare and set. |
| 37 * If *addr == before, set *addr to after and return true, otherwise return fal
se. |
| 38 * This must act as a release (SL/S) memory barrier and as a compiler barrier. |
| 39 */ |
| 40 static bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after); |
| 41 |
36 /** If sk_atomic_dec does not act as an acquire (L/SL) barrier, | 42 /** If sk_atomic_dec does not act as an acquire (L/SL) barrier, |
37 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. | 43 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. |
38 */ | 44 */ |
39 static void sk_membar_acquire__after_atomic_dec(); | 45 static void sk_membar_acquire__after_atomic_dec(); |
40 | 46 |
41 /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier, | 47 /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier, |
42 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. | 48 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. |
43 */ | 49 */ |
44 static void sk_membar_acquire__after_atomic_conditional_inc(); | 50 static void sk_membar_acquire__after_atomic_conditional_inc(); |
45 | 51 |
46 #include SK_ATOMICS_PLATFORM_H | 52 #include SK_ATOMICS_PLATFORM_H |
47 | 53 |
| 54 // This is POD and must be zero-initialized. |
| 55 struct SkSpinlock { |
| 56 void acquire() { |
| 57 SkASSERT(shouldBeZero == 0); |
| 58 // No memory barrier needed, but sk_atomic_cas gives us at least release
anyway. |
| 59 while (!sk_atomic_cas(&thisIsPrivate, 0, 1)) { |
| 60 // spin |
| 61 } |
| 62 } |
| 63 |
| 64 void release() { |
| 65 SkASSERT(shouldBeZero == 0); |
| 66 // This requires a release memory barrier before storing, which sk_atomi
c_cas guarantees. |
| 67 SkAssertResult(sk_atomic_cas(&thisIsPrivate, 1, 0)); |
| 68 } |
| 69 |
| 70 int32_t thisIsPrivate; |
| 71 SkDEBUGCODE(int32_t shouldBeZero;) |
| 72 }; |
| 73 |
| 74 class SkAutoSpinlock : SkNoncopyable { |
| 75 public: |
| 76 explicit SkAutoSpinlock(SkSpinlock* lock) : fLock(lock) { fLock->acquire();
} |
| 77 ~SkAutoSpinlock() { fLock->release(); } |
| 78 private: |
| 79 SkSpinlock* fLock; |
| 80 }; |
| 81 #define SkAutoSpinlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoSpinlock) |
48 | 82 |
49 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. | 83 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. |
50 | 84 |
51 class SkBaseMutex { | 85 class SkBaseMutex { |
52 public: | 86 public: |
53 void acquire(); | 87 void acquire(); |
54 void release(); | 88 void release(); |
55 }; | 89 }; |
56 | 90 |
57 class SkMutex : SkBaseMutex { | 91 class SkMutex : SkBaseMutex { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 fMutex = NULL; | 128 fMutex = NULL; |
95 } | 129 } |
96 } | 130 } |
97 | 131 |
98 private: | 132 private: |
99 SkBaseMutex* fMutex; | 133 SkBaseMutex* fMutex; |
100 }; | 134 }; |
101 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 135 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
102 | 136 |
103 #endif | 137 #endif |
OLD | NEW |