skia2/include/private/SkSpinlock.h
Brian Osman 7c2114fc2f Cache GrColorSpaceXforms
Even with a modest cache, we're going to get nearly 100% hit rate
for typical usage scenarios. I'm hoping to avoid the special case
caching of sRGB -> destination, and just rely on the more general
mechanism.

Yes, this is yet-another cache class. I wanted to use one of many
that are laying around, but couldn't find a good fit. On the plus
side, it's not much code.

BUG=skia:

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=3726

Change-Id: I943be5c99f0d691a87ffe8c5bc3067a8eb491fc2
Reviewed-on: https://skia-review.googlesource.com/3726
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
2016-10-20 20:47:33 +00:00

48 lines
1.3 KiB
C++

/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkSpinlock_DEFINED
#define SkSpinlock_DEFINED
#include "SkTypes.h"
#include <atomic>
class SkSpinlock {
public:
constexpr SkSpinlock() = default;
void 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();
}
}
// Acquire the lock or fail (quickly). Lets the caller decide to do something other than wait.
bool tryAcquire() {
// 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. Let the caller decide what to do.
return false;
}
return true;
}
void release() {
// To act as a mutex, we need a release barrier when we release the lock.
fLocked.store(false, std::memory_order_release);
}
private:
SK_API void contendedAcquire();
std::atomic<bool> fLocked{false};
};
#endif//SkSpinlock_DEFINED