skia2/include/private/SkMutex.h
Mike Klein d9187c085b Revert "SkMath takes some functions from from SkTypes"
This reverts commit 3b347232bc.

Reason for revert: tree done gone red.

Original change's description:
> SkMath takes some functions from from SkTypes
> 
> Moved to include/core/SkMath.h: Sk{Is|}Align{2|4|8|Ptr}, SkLeftShift,
> SkAbs{32|}, SkM{ax|in}32 SkTM{in|ax}, SkTClamp, SkFastMin32, SkTPin.
> 
> Change-Id: Ibcc07be0fc3677731048e7cc86006e7aa493cb92
> Reviewed-on: https://skia-review.googlesource.com/133381
> Auto-Submit: Hal Canary <halcanary@google.com>
> Reviewed-by: Ben Wagner <bungeman@google.com>
> Commit-Queue: Hal Canary <halcanary@google.com>

TBR=mtklein@google.com,halcanary@google.com,bungeman@google.com,reed@google.com

Change-Id: I44073cf397e2a3a6a941a90f0aa63c6396d4c742
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://skia-review.googlesource.com/152587
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
2018-09-07 17:32:54 +00:00

95 lines
2.1 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 SkMutex_DEFINED
#define SkMutex_DEFINED
#include "../private/SkMacros.h"
#include "../private/SkSemaphore.h"
#include "../private/SkThreadID.h"
#include "SkTypes.h"
#define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name;
class SkBaseMutex {
public:
constexpr SkBaseMutex() = default;
void acquire() {
fSemaphore.wait();
SkDEBUGCODE(fOwner = SkGetThreadID();)
}
void release() {
this->assertHeld();
SkDEBUGCODE(fOwner = kIllegalThreadID;)
fSemaphore.signal();
}
void assertHeld() {
SkASSERT(fOwner == SkGetThreadID());
}
protected:
SkBaseSemaphore fSemaphore{1};
SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};)
};
class SkMutex : public SkBaseMutex {
public:
using SkBaseMutex::SkBaseMutex;
~SkMutex() { fSemaphore.cleanup(); }
};
class SkAutoMutexAcquire {
public:
template <typename T>
SkAutoMutexAcquire(T* mutex) : fMutex(mutex) {
if (mutex) {
mutex->acquire();
}
fRelease = [](void* mutex) { ((T*)mutex)->release(); };
}
template <typename T>
SkAutoMutexAcquire(T& mutex) : SkAutoMutexAcquire(&mutex) {}
~SkAutoMutexAcquire() { this->release(); }
void release() {
if (fMutex) {
fRelease(fMutex);
}
fMutex = nullptr;
}
private:
void* fMutex;
void (*fRelease)(void*);
};
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
// SkAutoExclusive is a lighter weight version of SkAutoMutexAcquire.
// It assumes that there is a valid mutex, obviating the null check.
class SkAutoExclusive {
public:
template <typename T>
SkAutoExclusive(T& mutex) : fMutex(&mutex) {
mutex.acquire();
fRelease = [](void* mutex) { ((T*)mutex)->release(); };
}
~SkAutoExclusive() { fRelease(fMutex); }
private:
void* fMutex;
void (*fRelease)(void*);
};
#define SkAutoExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoExclusive)
#endif//SkMutex_DEFINED