skia2/include/private/SkSpinlock.h
halcanary 7b0b2ca435 Make Cmake work with debug build
Before this CL, the following failed to link:

    cd .../skia
    git fetch
    git checkout origin/master
    git clean -ffdx
    SKIA="$PWD"
    cd $(mktemp -d);
    cmake "${SKIA}/cmake" -DCMAKE_BUILD_TYPE=Debug -G Ninja
    ninja
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1757993006

Review URL: https://codereview.chromium.org/1757993006
2016-03-04 08:30:05 -08:00

36 lines
848 B
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:
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();
}
}
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