aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/private/SkSpinlock.h
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2016-02-29 10:14:38 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2016-02-29 10:14:38 -0800
commit15923c9e475894d89028b7a6a0b38aeeb9f9e645 (patch)
tree021ee7e8ff6cb8f07301e0f9b68017c29d92b0fe /include/private/SkSpinlock.h
parent1e83a2a239a09cf5f128c18ff559916757a7c719 (diff)
Modernize SkSpinlock.
- Use std::atomic directly. - No more need for SkPODSpinlock or SK_DECLARE_STATIC_SPINLOCK. Now simple code like this works as you'd hope: static SkSpinlock gLock; That is, it starts unlocked and there's no static initializer. std::atomic_flag would make this terser and standard-guaranteed, but ATOMIC_FLAG_INIT caused not-yet-implemented errors on MSVC 2013. The generated code for this approach is identical. It appears the implicit constructor is constexpr when all the member initializers are. I'm hoping this way of producing constexpr constructors without typing "constexpr" gives us a way to eliminate more SkFoo / SkBaseFoo distinctions and SK_DECLARE_STATIC_FOO. This was certainly the easiest. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1734383002 Review URL: https://codereview.chromium.org/1734383002
Diffstat (limited to 'include/private/SkSpinlock.h')
-rw-r--r--include/private/SkSpinlock.h24
1 files changed, 7 insertions, 17 deletions
diff --git a/include/private/SkSpinlock.h b/include/private/SkSpinlock.h
index ef52ab013a..a049f1f726 100644
--- a/include/private/SkSpinlock.h
+++ b/include/private/SkSpinlock.h
@@ -5,40 +5,30 @@
* found in the LICENSE file.
*/
-// This file is not part of the public Skia API.
-
#ifndef SkSpinlock_DEFINED
#define SkSpinlock_DEFINED
-#include "../private/SkAtomics.h"
-
-#define SK_DECLARE_STATIC_SPINLOCK(name) namespace {} static SkPODSpinlock name
+#include <atomic>
-// This class has no constructor and must be zero-initialized (the macro above does this).
-class SK_API SkPODSpinlock {
+class SkSpinlock {
public:
void acquire() {
- // To act as a mutex, we need an acquire barrier if we take the lock.
- if (sk_atomic_exchange(&fLocked, true, sk_memory_order_acquire)) {
+ // To act as a mutex, we need an acquire barrier when we acquire the lock.
+ if (fLocked.exchange(true, std::memory_order_acquire)) {
// Lock was contended. Fall back to an out-of-line spin loop.
this->contendedAcquire();
}
}
void release() {
- // To act as a mutex, we need a release barrier.
- sk_atomic_store(&fLocked, false, sk_memory_order_release);
+ // To act as a mutex, we need a release barrier when we release the lock.
+ fLocked.store(false, std::memory_order_release);
}
private:
void contendedAcquire();
- bool fLocked;
-};
-// For non-global-static use cases, this is normally what you want.
-class SkSpinlock : public SkPODSpinlock {
-public:
- SkSpinlock() { this->release(); }
+ std::atomic<bool> fLocked{false};
};
#endif//SkSpinlock_DEFINED