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
|
|
|
|
|
|
|
|
// This file is not part of the public Skia API.
|
2015-09-24 14:34:49 +00:00
|
|
|
#include "../private/SkSemaphore.h"
|
2015-01-21 21:13:31 +00:00
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2015-09-29 21:38:01 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
#include "../private/SkThreadID.h"
|
|
|
|
#endif
|
|
|
|
|
2015-09-24 14:34:49 +00:00
|
|
|
#define SK_MUTEX_SEMAPHORE_INIT {1, {0}}
|
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
#define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0}
|
2015-01-21 21:13:31 +00:00
|
|
|
#else
|
2015-09-24 14:34:49 +00:00
|
|
|
#define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Using POD-style initialization prevents the generation of a static initializer.
|
|
|
|
//
|
|
|
|
// Without magic statics there are no thread safety guarantees on initialization
|
|
|
|
// of local statics (even POD). As a result, it is illegal to use
|
|
|
|
// SK_DECLARE_STATIC_MUTEX in a function.
|
|
|
|
//
|
|
|
|
// Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be
|
|
|
|
// initialized in a class with this macro.
|
|
|
|
#define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_BASE_MUTEX_INIT;
|
|
|
|
|
|
|
|
struct SkBaseMutex {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
SkBaseSemaphore fSemaphore;
|
|
|
|
SkDEBUGCODE(SkThreadID fOwner;)
|
|
|
|
};
|
|
|
|
|
|
|
|
// This needs to use subclassing instead of encapsulation to make SkAutoMutexAcquire to work.
|
|
|
|
class SkMutex : public SkBaseMutex {
|
|
|
|
public:
|
|
|
|
SkMutex () {
|
|
|
|
fSemaphore = SK_MUTEX_SEMAPHORE_INIT;
|
2015-09-29 21:38:01 +00:00
|
|
|
SkDEBUGCODE(fOwner = kIllegalThreadID);
|
2015-09-24 14:34:49 +00:00
|
|
|
}
|
|
|
|
~SkMutex () { fSemaphore.deleteSemaphore(); }
|
|
|
|
SkMutex(const SkMutex&) = delete;
|
|
|
|
SkMutex& operator=(const SkMutex&) = delete;
|
|
|
|
};
|
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;
|
|
|
|
};
|
|
|
|
|
2015-07-09 20:44:32 +00:00
|
|
|
typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
|
2015-02-02 20:22:07 +00:00
|
|
|
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
|
|
|
|
|
2015-10-23 17:48:26 +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
|