f8869a24e7
The ClangTidy check `bugprone-unused-raii` has been enabled at review.skia.org/306838; this check provides equivalent protection. Change-Id: I9f3858bfd2bede107d509a5a206a08293d5f914c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/306953 Reviewed-by: Mike Klein <mtklein@google.com> Commit-Queue: Mike Klein <mtklein@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
/*
|
|
* Copyright 2015 Google Inc.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#ifndef SkMutex_DEFINED
|
|
#define SkMutex_DEFINED
|
|
|
|
#include "include/core/SkTypes.h"
|
|
#include "include/private/SkMacros.h"
|
|
#include "include/private/SkSemaphore.h"
|
|
#include "include/private/SkThreadAnnotations.h"
|
|
#include "include/private/SkThreadID.h"
|
|
|
|
class SK_CAPABILITY("mutex") SkMutex {
|
|
public:
|
|
constexpr SkMutex() = default;
|
|
|
|
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());
|
|
}
|
|
|
|
private:
|
|
SkSemaphore fSemaphore{1};
|
|
SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};)
|
|
};
|
|
|
|
class SK_SCOPED_CAPABILITY SkAutoMutexExclusive {
|
|
public:
|
|
SkAutoMutexExclusive(SkMutex& mutex) SK_ACQUIRE(mutex) : fMutex(mutex) { fMutex.acquire(); }
|
|
~SkAutoMutexExclusive() SK_RELEASE_CAPABILITY() { fMutex.release(); }
|
|
|
|
private:
|
|
SkMutex& fMutex;
|
|
};
|
|
|
|
#endif // SkMutex_DEFINED
|