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 while (!sk_atomic_cas(&thisIsPrivate, 0, 1)) { | |
bungeman-skia
2014/01/08 18:35:44
I'd still like a comment here that this doesn't re
| |
59 // spin | |
60 } | |
61 } | |
62 | |
63 void release() { | |
64 SkASSERT(shouldBeZero == 0); | |
65 SkAssertResult(sk_atomic_cas(&thisIsPrivate, 1, 0)); | |
bungeman-skia
2014/01/08 18:35:44
I'd still like a comment here that this requires a
| |
66 } | |
67 | |
68 int32_t thisIsPrivate; | |
69 SkDEBUGCODE(int32_t shouldBeZero;) | |
70 }; | |
71 | |
72 class SkAutoSpinlock : SkNoncopyable { | |
73 public: | |
74 explicit SkAutoSpinlock(SkSpinlock* lock) : fLock(lock) { fLock->acquire(); } | |
75 ~SkAutoSpinlock() { fLock->release(); } | |
76 private: | |
77 SkSpinlock* fLock; | |
78 }; | |
79 #define SkAutoSpinlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoSpinlock) | |
48 | 80 |
49 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. | 81 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. |
50 | 82 |
51 class SkBaseMutex { | 83 class SkBaseMutex { |
52 public: | 84 public: |
53 void acquire(); | 85 void acquire(); |
54 void release(); | 86 void release(); |
55 }; | 87 }; |
56 | 88 |
57 class SkMutex : SkBaseMutex { | 89 class SkMutex : SkBaseMutex { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 fMutex = NULL; | 126 fMutex = NULL; |
95 } | 127 } |
96 } | 128 } |
97 | 129 |
98 private: | 130 private: |
99 SkBaseMutex* fMutex; | 131 SkBaseMutex* fMutex; |
100 }; | 132 }; |
101 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 133 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
102 | 134 |
103 #endif | 135 #endif |
OLD | NEW |