aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkSpinlock.h
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2015-03-30 08:13:33 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-03-30 08:13:34 -0700
commit50ffd9921e6dc82cae8ef143e8cecfd8bcf900fb (patch)
tree56876ee2f54e95478ad143b177abc8f34843e3dd /include/core/SkSpinlock.h
parentff420217394e6e35e831b11e98b46202e37de23f (diff)
Extract the spinlock from SkOnce as SkSpinlock.
This uses slightly newer APIs from SkAtomics.h to make it a bit more efficient. No public API changes. TBR=reed@google.com BUG=skia: Review URL: https://codereview.chromium.org/1039323002
Diffstat (limited to 'include/core/SkSpinlock.h')
-rw-r--r--include/core/SkSpinlock.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/include/core/SkSpinlock.h b/include/core/SkSpinlock.h
new file mode 100644
index 0000000000..68a6ff7b12
--- /dev/null
+++ b/include/core/SkSpinlock.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+// This file is not part of the public Skia API.
+
+#ifndef SkSpinlock_DEFINED
+#define SkSpinlock_DEFINED
+
+#include "SkAtomics.h"
+
+#define SK_DECLARE_STATIC_SPINLOCK(name) namespace {} static SkPODSpinlock name
+
+// This class has no constructor and must be zero-initialized (the macro above does this).
+struct SkPODSpinlock {
+ void acquire() {
+ // To act as a mutex, we need an acquire barrier.
+ while(sk_atomic_exchange(&fLocked, true, sk_memory_order_acquire)) { /*spin*/ }
+ }
+ void release() {
+ // To act as a mutex, we need a release barrier.
+ sk_atomic_store(&fLocked, false, sk_memory_order_release);
+ }
+
+ bool fLocked;
+};
+
+// For non-global-static use cases, this is normally what you want.
+class SkSpinlock : public SkPODSpinlock {
+public:
+ SkSpinlock() { this->release(); }
+};
+
+#endif//SkSpinlock_DEFINED