Index: include/core/SkThread.h |
diff --git a/include/core/SkThread.h b/include/core/SkThread.h |
index 412ace31eb24f64e31c1d2e9529d24a3c004e95a..fbd7fe2ec740df5729156b3d86e57a46f3ff76c6 100644 |
--- a/include/core/SkThread.h |
+++ b/include/core/SkThread.h |
@@ -33,6 +33,11 @@ static int32_t sk_atomic_dec(int32_t* addr); |
*/ |
static int32_t sk_atomic_conditional_inc(int32_t* addr); |
+/** Atomic compare and set. |
+ * If *addr == before, set *addr to after and return true, otherwise return false. |
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
|
+ */ |
+static bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after); |
+ |
/** If sk_atomic_dec does not act as an acquire (L/SL) barrier, |
* this must act as an acquire (L/SL) memory barrier and as a compiler barrier. |
*/ |
@@ -45,6 +50,32 @@ static void sk_membar_acquire__after_atomic_conditional_inc(); |
#include SK_ATOMICS_PLATFORM_H |
+// This is POD and must be zero-initialized. |
+struct SkSpinlock { |
+ void acquire() { |
+ SkASSERT(shouldBeZero == 0); |
+ 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
|
+ // spin |
+ } |
+ } |
+ |
+ void release() { |
+ SkASSERT(shouldBeZero == 0); |
+ 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.
|
+ } |
+ |
+ int32_t thisIsPrivate; |
+ SkDEBUGCODE(int32_t shouldBeZero;) |
+}; |
+ |
+class SkAutoSpinlock : SkNoncopyable { |
+public: |
+ explicit SkAutoSpinlock(SkSpinlock* lock) : fLock(lock) { fLock->acquire(); } |
+ ~SkAutoSpinlock() { fLock->release(); } |
+private: |
+ SkSpinlock* fLock; |
+}; |
+#define SkAutoSpinlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoSpinlock) |
/** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. |