2015-07-09 20:44:32 +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.
|
|
|
|
*/
|
|
|
|
|
2015-01-21 21:13:31 +00:00
|
|
|
#ifndef SkMutex_DEFINED
|
|
|
|
#define SkMutex_DEFINED
|
|
|
|
|
2015-09-24 14:34:49 +00:00
|
|
|
#include "../private/SkSemaphore.h"
|
2016-05-05 17:57:37 +00:00
|
|
|
#include "../private/SkThreadID.h"
|
2015-01-21 21:13:31 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2016-05-05 17:57:37 +00:00
|
|
|
#define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name;
|
2016-05-05 01:23:30 +00:00
|
|
|
|
2016-05-05 17:57:37 +00:00
|
|
|
class SkBaseMutex {
|
|
|
|
public:
|
|
|
|
constexpr SkBaseMutex() = default;
|
2016-05-05 01:23:30 +00:00
|
|
|
|
2015-09-24 14:34:49 +00:00
|
|
|
void acquire() {
|
|
|
|
fSemaphore.wait();
|
2015-09-29 21:38:01 +00:00
|
|
|
SkDEBUGCODE(fOwner = SkGetThreadID();)
|
2015-09-24 14:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void release() {
|
|
|
|
this->assertHeld();
|
2015-09-29 21:38:01 +00:00
|
|
|
SkDEBUGCODE(fOwner = kIllegalThreadID;)
|
2015-09-24 14:34:49 +00:00
|
|
|
fSemaphore.signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void assertHeld() {
|
2015-09-29 21:38:01 +00:00
|
|
|
SkASSERT(fOwner == SkGetThreadID());
|
2015-09-24 14:34:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 17:57:37 +00:00
|
|
|
protected:
|
|
|
|
SkBaseSemaphore fSemaphore{1};
|
|
|
|
SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};)
|
2016-05-05 01:23:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkMutex : public SkBaseMutex {
|
|
|
|
public:
|
2016-05-05 17:57:37 +00:00
|
|
|
using SkBaseMutex::SkBaseMutex;
|
|
|
|
~SkMutex() { fSemaphore.cleanup(); }
|
2015-09-24 14:34:49 +00:00
|
|
|
};
|
2015-01-21 21:13:31 +00:00
|
|
|
|
2015-07-09 20:44:32 +00:00
|
|
|
template <typename Lock>
|
|
|
|
class SkAutoTAcquire : SkNoncopyable {
|
2015-02-02 20:22:07 +00:00
|
|
|
public:
|
2015-07-09 20:44:32 +00:00
|
|
|
explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) {
|
2015-09-29 21:38:01 +00:00
|
|
|
SkASSERT(fMutex != nullptr);
|
2015-02-02 20:22:07 +00:00
|
|
|
mutex.acquire();
|
|
|
|
}
|
|
|
|
|
2015-07-09 20:44:32 +00:00
|
|
|
explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) {
|
2015-02-02 20:22:07 +00:00
|
|
|
if (mutex) {
|
|
|
|
mutex->acquire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If the mutex has not been released, release it now. */
|
2015-07-09 20:44:32 +00:00
|
|
|
~SkAutoTAcquire() {
|
2015-02-02 20:22:07 +00:00
|
|
|
if (fMutex) {
|
|
|
|
fMutex->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If the mutex has not been released, release it now. */
|
|
|
|
void release() {
|
|
|
|
if (fMutex) {
|
|
|
|
fMutex->release();
|
2015-09-29 21:38:01 +00:00
|
|
|
fMutex = nullptr;
|
2015-02-02 20:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assert that we're holding the mutex. */
|
|
|
|
void assertHeld() {
|
|
|
|
SkASSERT(fMutex);
|
|
|
|
fMutex->assertHeld();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-07-09 20:44:32 +00:00
|
|
|
Lock* fMutex;
|
2015-02-02 20:22:07 +00:00
|
|
|
};
|
2015-07-09 20:44:32 +00:00
|
|
|
|
2015-10-22 02:11:13 +00:00
|
|
|
// SkAutoTExclusive is a lighter weight version of SkAutoTAcquire. It assumes that there is a valid
|
|
|
|
// mutex, thus removing the check for the null pointer.
|
|
|
|
template <typename Lock>
|
|
|
|
class SkAutoTExclusive {
|
|
|
|
public:
|
|
|
|
SkAutoTExclusive(Lock& lock) : fLock(lock) { lock.acquire(); }
|
|
|
|
~SkAutoTExclusive() { fLock.release(); }
|
|
|
|
private:
|
|
|
|
Lock &fLock;
|
|
|
|
};
|
|
|
|
|
2016-05-05 01:23:30 +00:00
|
|
|
typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
|
2015-02-02 20:22:07 +00:00
|
|
|
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
|
|
|
|
|
2016-05-05 01:23:30 +00:00
|
|
|
typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive;
|
2015-10-22 02:11:13 +00:00
|
|
|
#define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive)
|
|
|
|
|
2015-01-21 21:13:31 +00:00
|
|
|
#endif//SkMutex_DEFINED
|