2015-03-30 15:13:33 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2015-09-28 18:24:13 +00:00
|
|
|
#include "../private/SkAtomics.h"
|
2015-03-30 15:13:33 +00:00
|
|
|
|
|
|
|
#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).
|
2015-07-10 15:32:23 +00:00
|
|
|
class SK_API SkPODSpinlock {
|
2015-07-09 17:51:36 +00:00
|
|
|
public:
|
2015-03-30 15:13:33 +00:00
|
|
|
void acquire() {
|
2015-07-09 17:51:36 +00:00
|
|
|
// 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)) {
|
|
|
|
// Lock was contended. Fall back to an out-of-line spin loop.
|
|
|
|
this->contendedAcquire();
|
|
|
|
}
|
2015-03-30 15:13:33 +00:00
|
|
|
}
|
2015-07-09 17:51:36 +00:00
|
|
|
|
2015-03-30 15:13:33 +00:00
|
|
|
void release() {
|
|
|
|
// To act as a mutex, we need a release barrier.
|
|
|
|
sk_atomic_store(&fLocked, false, sk_memory_order_release);
|
|
|
|
}
|
|
|
|
|
2015-07-09 17:51:36 +00:00
|
|
|
private:
|
|
|
|
void contendedAcquire();
|
2015-03-30 15:13:33 +00:00
|
|
|
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
|