Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Unified Diff: include/core/SkSpinlock.h

Issue 1039323002: Extract the spinlock from SkOnce as SkSpinlock. (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: quotes Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: include/core/SkSpinlock.h
diff --git a/include/core/SkSpinlock.h b/include/core/SkSpinlock.h
new file mode 100644
index 0000000000000000000000000000000000000000..68a6ff7b1246bcd216327a683a36d75148f2845b
--- /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

Powered by Google App Engine
This is Rietveld 408576698