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 |
11 #include "SkTypes.h" | 11 // TODO: delete this file, or maybe move the definition of SkThread here. |
12 | |
13 // SkAtomics.h must provide inline implementations for the following declaration
s. | |
14 | |
15 /** Atomically adds one to the int referenced by addr and returns the previous v
alue. | |
16 * No additional memory barrier is required; this must act as a compiler barrie
r. | |
17 */ | |
18 static int32_t sk_atomic_inc(int32_t* addr); | |
19 static int64_t sk_atomic_inc(int64_t* addr); | |
20 | |
21 /** Atomically adds inc to the int referenced by addr and returns the previous v
alue. | |
22 * No additional memory barrier is required; this must act as a compiler barrie
r. | |
23 */ | |
24 static int32_t sk_atomic_add(int32_t* addr, int32_t inc); | |
25 | |
26 /** Atomically subtracts one from the int referenced by addr and returns the pre
vious value. | |
27 * This must act as a release (SL/S) memory barrier and as a compiler barrier. | |
28 */ | |
29 static int32_t sk_atomic_dec(int32_t* addr); | |
30 | |
31 /** Atomic compare and set. | |
32 * If *addr == before, set *addr to after and return true, otherwise return fal
se. | |
33 * This must act as a release (SL/S) memory barrier and as a compiler barrier. | |
34 */ | |
35 static bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after); | |
36 | |
37 /** If sk_atomic_dec does not act as an acquire (L/SL) barrier, | |
38 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. | |
39 */ | |
40 static void sk_membar_acquire__after_atomic_dec(); | |
41 | |
42 /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier, | |
43 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. | |
44 */ | |
45 static void sk_membar_acquire__after_atomic_conditional_inc(); | |
46 | 12 |
47 #include "SkAtomics.h" | 13 #include "SkAtomics.h" |
48 | |
49 /** Atomically adds one to the int referenced by addr iff the referenced int was
not 0 | |
50 * and returns the previous value. | |
51 * No additional memory barrier is required; this must act as a compiler barrie
r. | |
52 */ | |
53 template<typename INT_TYPE> static inline INT_TYPE sk_atomic_conditional_inc(INT
_TYPE* addr) { | |
54 INT_TYPE prev; | |
55 do { | |
56 prev = *addr; | |
57 if (0 == prev) { | |
58 break; | |
59 } | |
60 } while (!sk_atomic_cas(addr, prev, prev+1)); | |
61 return prev; | |
62 } | |
63 | |
64 // SkBarriers.h must provide implementations for the following declarations: | |
65 | |
66 /** Prevent the compiler from reordering across this barrier. */ | |
67 static void sk_compiler_barrier(); | |
68 | |
69 /** Read T*, with at least an acquire barrier. | |
70 * | |
71 * Only needs to be implemented for T which can be atomically read. | |
72 */ | |
73 template <typename T> T sk_acquire_load(T*); | |
74 | |
75 /** Write T*, with at least a release barrier. | |
76 * | |
77 * Only needs to be implemented for T which can be atomically written. | |
78 */ | |
79 template <typename T> void sk_release_store(T*, T); | |
80 | |
81 #include "SkBarriers.h" | |
82 | |
83 /** SkMutex.h must provide the following (or equivalent) declarations. | |
84 | |
85 class SkBaseMutex { | |
86 public: | |
87 void acquire(); // Block until this thread owns the mutex. | |
88 void release(); // Assuming this thread owns the mutex, release it. | |
89 void assertHeld(); // If SK_DEBUG, assert this thread owns the mutex. | |
90 }; | |
91 | |
92 class SkMutex : SkBaseMutex { | |
93 public: | |
94 SkMutex(); | |
95 ~SkMutex(); | |
96 }; | |
97 | |
98 #define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = ... | |
99 */ | |
100 | |
101 #include "SkMutex.h" | 14 #include "SkMutex.h" |
102 | 15 |
103 class SkAutoMutexAcquire : SkNoncopyable { | |
104 public: | |
105 explicit SkAutoMutexAcquire(SkBaseMutex& mutex) : fMutex(&mutex) { | |
106 SkASSERT(fMutex != NULL); | |
107 mutex.acquire(); | |
108 } | |
109 | |
110 explicit SkAutoMutexAcquire(SkBaseMutex* mutex) : fMutex(mutex) { | |
111 if (mutex) { | |
112 mutex->acquire(); | |
113 } | |
114 } | |
115 | |
116 /** If the mutex has not been released, release it now. */ | |
117 ~SkAutoMutexAcquire() { | |
118 if (fMutex) { | |
119 fMutex->release(); | |
120 } | |
121 } | |
122 | |
123 /** If the mutex has not been released, release it now. */ | |
124 void release() { | |
125 if (fMutex) { | |
126 fMutex->release(); | |
127 fMutex = NULL; | |
128 } | |
129 } | |
130 | |
131 /** Assert that we're holding the mutex. */ | |
132 void assertHeld() { | |
133 SkASSERT(fMutex); | |
134 fMutex->assertHeld(); | |
135 } | |
136 | |
137 private: | |
138 SkBaseMutex* fMutex; | |
139 }; | |
140 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | |
141 | |
142 #endif | 16 #endif |
OLD | NEW |