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:
|
2017-09-20 13:53:39 +00:00
|
|
|
using SkBaseMutex::SkBaseMutex;
|
|
|
|
~SkMutex() { fSemaphore.cleanup(); }
|
2015-09-24 14:34:49 +00:00
|
|
|
};
|
2015-01-21 21:13:31 +00:00
|
|
|
|
2016-06-10 20:56:35 +00:00
|
|
|
class SkAutoMutexAcquire {
|
2015-02-02 20:22:07 +00:00
|
|
|
public:
|
2016-06-10 20:56:35 +00:00
|
|
|
template <typename T>
|
|
|
|
SkAutoMutexAcquire(T* mutex) : fMutex(mutex) {
|
2015-02-02 20:22:07 +00:00
|
|
|
if (mutex) {
|
|
|
|
mutex->acquire();
|
|
|
|
}
|
2016-06-10 20:56:35 +00:00
|
|
|
fRelease = [](void* mutex) { ((T*)mutex)->release(); };
|
2015-02-02 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 20:56:35 +00:00
|
|
|
template <typename T>
|
|
|
|
SkAutoMutexAcquire(T& mutex) : SkAutoMutexAcquire(&mutex) {}
|
|
|
|
|
|
|
|
~SkAutoMutexAcquire() { this->release(); }
|
2015-02-02 20:22:07 +00:00
|
|
|
|
|
|
|
void release() {
|
|
|
|
if (fMutex) {
|
2016-06-10 20:56:35 +00:00
|
|
|
fRelease(fMutex);
|
2015-02-02 20:22:07 +00:00
|
|
|
}
|
2016-06-10 20:56:35 +00:00
|
|
|
fMutex = nullptr;
|
2015-02-02 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-06-10 20:56:35 +00:00
|
|
|
void* fMutex;
|
|
|
|
void (*fRelease)(void*);
|
2015-02-02 20:22:07 +00:00
|
|
|
};
|
2016-06-10 20:56:35 +00:00
|
|
|
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
|
2015-07-09 20:44:32 +00:00
|
|
|
|
2016-06-10 20:56:35 +00:00
|
|
|
// SkAutoExclusive is a lighter weight version of SkAutoMutexAcquire.
|
|
|
|
// It assumes that there is a valid mutex, obviating the null check.
|
|
|
|
class SkAutoExclusive {
|
2015-10-22 02:11:13 +00:00
|
|
|
public:
|
2016-06-10 20:56:35 +00:00
|
|
|
template <typename T>
|
|
|
|
SkAutoExclusive(T& mutex) : fMutex(&mutex) {
|
|
|
|
mutex.acquire();
|
2015-10-22 02:11:13 +00:00
|
|
|
|
2016-06-10 20:56:35 +00:00
|
|
|
fRelease = [](void* mutex) { ((T*)mutex)->release(); };
|
|
|
|
}
|
|
|
|
~SkAutoExclusive() { fRelease(fMutex); }
|
2015-02-02 20:22:07 +00:00
|
|
|
|
2016-06-10 20:56:35 +00:00
|
|
|
private:
|
|
|
|
void* fMutex;
|
|
|
|
void (*fRelease)(void*);
|
|
|
|
};
|
|
|
|
#define SkAutoExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoExclusive)
|
2015-10-22 02:11:13 +00:00
|
|
|
|
2015-01-21 21:13:31 +00:00
|
|
|
#endif//SkMutex_DEFINED
|