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. | |
bungeman-skia
2014/01/07 22:05:41
This needs to state the memory barriers provided.
mtklein
2014/01/08 17:02:18
Thanks for reminding me. Now done.
I updated _an
| |
38 */ | |
39 static bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after); | |
40 | |
36 /** If sk_atomic_dec does not act as an acquire (L/SL) barrier, | 41 /** 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. | 42 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. |
38 */ | 43 */ |
39 static void sk_membar_acquire__after_atomic_dec(); | 44 static void sk_membar_acquire__after_atomic_dec(); |
40 | 45 |
41 /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier, | 46 /** 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. | 47 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. |
43 */ | 48 */ |
44 static void sk_membar_acquire__after_atomic_conditional_inc(); | 49 static void sk_membar_acquire__after_atomic_conditional_inc(); |
45 | 50 |
46 #include SK_ATOMICS_PLATFORM_H | 51 #include SK_ATOMICS_PLATFORM_H |
47 | 52 |
53 // This is POD and must be zero-initialized. | |
54 struct SkSpinlock { | |
55 void acquire() { | |
56 SkASSERT(shouldBeZero == 0); | |
57 while (!sk_atomic_cas(&thisIsPrivate, 0, 1)) { | |
bungeman-skia
2014/01/07 22:05:41
This doesn't need a barrier.
(It doesn't look lik
mtklein
2014/01/08 17:02:18
Done. Conceivably this might need a store barrier
| |
58 // spin | |
59 } | |
60 } | |
61 | |
62 void release() { | |
63 SkASSERT(shouldBeZero == 0); | |
64 SkAssertResult(sk_atomic_cas(&thisIsPrivate, 1, 0)); | |
bungeman-skia
2014/01/07 22:05:41
This needs a store:store/load:store (release) barr
mtklein
2014/01/08 17:02:18
Done.
mtklein
2014/01/08 17:02:18
Done. Now guaranteed by sk_atomic_cas.
| |
65 } | |
66 | |
67 int32_t thisIsPrivate; | |
68 SkDEBUGCODE(int32_t shouldBeZero;) | |
69 }; | |
70 | |
71 class SkAutoSpinlock : SkNoncopyable { | |
72 public: | |
73 explicit SkAutoSpinlock(SkSpinlock* lock) : fLock(lock) { fLock->acquire(); } | |
74 ~SkAutoSpinlock() { fLock->release(); } | |
75 private: | |
76 SkSpinlock* fLock; | |
77 }; | |
78 #define SkAutoSpinlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoSpinlock) | |
48 | 79 |
49 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. | 80 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. |
50 | 81 |
51 class SkBaseMutex { | 82 class SkBaseMutex { |
52 public: | 83 public: |
53 void acquire(); | 84 void acquire(); |
54 void release(); | 85 void release(); |
55 }; | 86 }; |
56 | 87 |
57 class SkMutex : SkBaseMutex { | 88 class SkMutex : SkBaseMutex { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 fMutex = NULL; | 125 fMutex = NULL; |
95 } | 126 } |
96 } | 127 } |
97 | 128 |
98 private: | 129 private: |
99 SkBaseMutex* fMutex; | 130 SkBaseMutex* fMutex; |
100 }; | 131 }; |
101 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 132 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
102 | 133 |
103 #endif | 134 #endif |
OLD | NEW |