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.
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2015-07-29 18:49:40 +00:00
|
|
|
// IWYU pragma: begin_exports
|
2015-01-21 21:13:31 +00:00
|
|
|
#if defined(SK_BUILD_FOR_WIN)
|
|
|
|
#include "../ports/SkMutex_win.h"
|
|
|
|
#else
|
|
|
|
#include "../ports/SkMutex_pthread.h"
|
|
|
|
#endif
|
2015-07-29 18:49:40 +00:00
|
|
|
// IWYU pragma: end_exports
|
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-02-02 20:22:07 +00:00
|
|
|
SkASSERT(fMutex != NULL);
|
|
|
|
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();
|
|
|
|
fMutex = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
|
|
|
typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
|
|
|
|
|
2015-02-02 20:22:07 +00:00
|
|
|
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
|
|
|
|
|
|
|
|
|
2015-01-21 21:13:31 +00:00
|
|
|
#endif//SkMutex_DEFINED
|