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
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkMacros.h"
|
|
|
|
#include "include/private/SkSemaphore.h"
|
2019-05-10 16:16:17 +00:00
|
|
|
#include "include/private/SkThreadAnnotations.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkThreadID.h"
|
2015-01-21 21:13:31 +00:00
|
|
|
|
2019-05-10 16:16:17 +00:00
|
|
|
class SK_CAPABILITY("mutex") SkMutex {
|
2016-05-05 01:23:30 +00:00
|
|
|
public:
|
2019-05-10 16:16:17 +00:00
|
|
|
constexpr SkMutex() = default;
|
|
|
|
|
2022-02-11 23:17:08 +00:00
|
|
|
~SkMutex() {
|
|
|
|
this->assertNotHeld();
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:16:17 +00:00
|
|
|
void acquire() SK_ACQUIRE() {
|
|
|
|
fSemaphore.wait();
|
|
|
|
SkDEBUGCODE(fOwner = SkGetThreadID();)
|
|
|
|
}
|
|
|
|
|
|
|
|
void release() SK_RELEASE_CAPABILITY() {
|
|
|
|
this->assertHeld();
|
|
|
|
SkDEBUGCODE(fOwner = kIllegalThreadID;)
|
|
|
|
fSemaphore.signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void assertHeld() SK_ASSERT_CAPABILITY(this) {
|
|
|
|
SkASSERT(fOwner == SkGetThreadID());
|
|
|
|
}
|
|
|
|
|
2022-02-11 23:17:08 +00:00
|
|
|
void assertNotHeld() {
|
|
|
|
SkASSERT(fOwner == kIllegalThreadID);
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:16:17 +00:00
|
|
|
private:
|
|
|
|
SkSemaphore fSemaphore{1};
|
|
|
|
SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};)
|
2015-09-24 14:34:49 +00:00
|
|
|
};
|
2015-01-21 21:13:31 +00:00
|
|
|
|
2019-05-10 16:16:17 +00:00
|
|
|
class SK_SCOPED_CAPABILITY SkAutoMutexExclusive {
|
2015-10-22 02:11:13 +00:00
|
|
|
public:
|
2019-05-10 16:16:17 +00:00
|
|
|
SkAutoMutexExclusive(SkMutex& mutex) SK_ACQUIRE(mutex) : fMutex(mutex) { fMutex.acquire(); }
|
|
|
|
~SkAutoMutexExclusive() SK_RELEASE_CAPABILITY() { fMutex.release(); }
|
2015-02-02 20:22:07 +00:00
|
|
|
|
2021-02-28 15:00:29 +00:00
|
|
|
SkAutoMutexExclusive(const SkAutoMutexExclusive&) = delete;
|
|
|
|
SkAutoMutexExclusive(SkAutoMutexExclusive&&) = delete;
|
|
|
|
|
|
|
|
SkAutoMutexExclusive& operator=(const SkAutoMutexExclusive&) = delete;
|
|
|
|
SkAutoMutexExclusive& operator=(SkAutoMutexExclusive&&) = delete;
|
|
|
|
|
2016-06-10 20:56:35 +00:00
|
|
|
private:
|
2019-05-10 16:16:17 +00:00
|
|
|
SkMutex& fMutex;
|
2016-06-10 20:56:35 +00:00
|
|
|
};
|
2019-05-10 16:16:17 +00:00
|
|
|
|
2019-06-17 18:40:42 +00:00
|
|
|
#endif // SkMutex_DEFINED
|