Type-erase SkAutoMutexAcquire and SkAutoExclusive.
This is purely for convenience, to not need to write the lock type
in the guard anymore. This should all inline away.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2055023003
Review-Url: https://codereview.chromium.org/2055023003
diff --git a/dm/DM.cpp b/dm/DM.cpp
index 9ddff28..158d256 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -128,7 +128,7 @@
vlog("done %s\n", id.c_str());
int pending;
{
- SkAutoTAcquire<SkSpinlock> lock(gMutex);
+ SkAutoMutexAcquire lock(gMutex);
for (int i = 0; i < gRunning.count(); i++) {
if (gRunning[i] == id) {
gRunning.removeShuffle(i);
@@ -147,7 +147,7 @@
static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
vlog("start %s\n", id.c_str());
- SkAutoTAcquire<SkSpinlock> lock(gMutex);
+ SkAutoMutexAcquire lock(gMutex);
gRunning.push_back(id);
}
@@ -156,7 +156,7 @@
peak = sk_tools::getMaxResidentSetSizeMB();
SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
- SkAutoTAcquire<SkSpinlock> lock(gMutex);
+ SkAutoMutexAcquire lock(gMutex);
info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
for (auto& task : gRunning) {
@@ -179,7 +179,7 @@
#undef _
};
- SkAutoTAcquire<SkSpinlock> lock(gMutex);
+ SkAutoMutexAcquire lock(gMutex);
const DWORD code = e->ExceptionRecord->ExceptionCode;
info("\nCaught exception %u", code);
@@ -205,7 +205,7 @@
#include <stdlib.h>
static void crash_handler(int sig) {
- SkAutoTAcquire<SkSpinlock> lock(gMutex);
+ SkAutoMutexAcquire lock(gMutex);
info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
for (auto& task : gRunning) {
@@ -1300,7 +1300,7 @@
if (src->veto(sink->flags()) ||
is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
src.options.c_str(), src->name().c_str())) {
- SkAutoTAcquire<SkSpinlock> lock(gMutex);
+ SkAutoMutexAcquire lock(gMutex);
gPending--;
continue;
}
diff --git a/include/private/SkMutex.h b/include/private/SkMutex.h
index 3b0e1c4..7cfdb11 100644
--- a/include/private/SkMutex.h
+++ b/include/private/SkMutex.h
@@ -44,60 +44,50 @@
~SkMutex() { fSemaphore.cleanup(); }
};
-template <typename Lock>
-class SkAutoTAcquire : SkNoncopyable {
+class SkAutoMutexAcquire {
public:
- explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) {
- SkASSERT(fMutex != nullptr);
- mutex.acquire();
- }
-
- explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) {
+ template <typename T>
+ SkAutoMutexAcquire(T* mutex) : fMutex(mutex) {
if (mutex) {
mutex->acquire();
}
+ fRelease = [](void* mutex) { ((T*)mutex)->release(); };
}
- /** If the mutex has not been released, release it now. */
- ~SkAutoTAcquire() {
- if (fMutex) {
- fMutex->release();
- }
- }
+ template <typename T>
+ SkAutoMutexAcquire(T& mutex) : SkAutoMutexAcquire(&mutex) {}
- /** If the mutex has not been released, release it now. */
+ ~SkAutoMutexAcquire() { this->release(); }
+
void release() {
if (fMutex) {
- fMutex->release();
- fMutex = nullptr;
+ fRelease(fMutex);
}
- }
-
- /** Assert that we're holding the mutex. */
- void assertHeld() {
- SkASSERT(fMutex);
- fMutex->assertHeld();
+ fMutex = nullptr;
}
private:
- Lock* fMutex;
+ void* fMutex;
+ void (*fRelease)(void*);
};
-
-// 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;
-};
-
-typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
-typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive;
-#define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive)
+// SkAutoExclusive is a lighter weight version of SkAutoMutexAcquire.
+// It assumes that there is a valid mutex, obviating the null check.
+class SkAutoExclusive {
+public:
+ template <typename T>
+ SkAutoExclusive(T& mutex) : fMutex(&mutex) {
+ mutex.acquire();
+
+ fRelease = [](void* mutex) { ((T*)mutex)->release(); };
+ }
+ ~SkAutoExclusive() { fRelease(fMutex); }
+
+private:
+ void* fMutex;
+ void (*fRelease)(void*);
+};
+#define SkAutoExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoExclusive)
#endif//SkMutex_DEFINED
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index 072541d..30d00b2 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -470,15 +470,13 @@
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
-typedef SkAutoTExclusive<SkSpinlock> Exclusive;
-
size_t SkGlyphCache_Globals::setCacheSizeLimit(size_t newLimit) {
static const size_t minLimit = 256 * 1024;
if (newLimit < minLimit) {
newLimit = minLimit;
}
- Exclusive ac(fLock);
+ SkAutoExclusive ac(fLock);
size_t prevLimit = fCacheSizeLimit;
fCacheSizeLimit = newLimit;
@@ -491,7 +489,7 @@
newCount = 0;
}
- Exclusive ac(fLock);
+ SkAutoExclusive ac(fLock);
int prevCount = fCacheCountLimit;
fCacheCountLimit = newCount;
@@ -500,7 +498,7 @@
}
void SkGlyphCache_Globals::purgeAll() {
- Exclusive ac(fLock);
+ SkAutoExclusive ac(fLock);
this->internalPurge(fTotalMemoryUsed);
}
@@ -534,7 +532,7 @@
SkGlyphCache* cache;
{
- Exclusive ac(globals.fLock);
+ SkAutoExclusive ac(globals.fLock);
globals.validate();
@@ -647,7 +645,7 @@
void SkGlyphCache::VisitAll(Visitor visitor, void* context) {
SkGlyphCache_Globals& globals = get_globals();
- Exclusive ac(globals.fLock);
+ SkAutoExclusive ac(globals.fLock);
SkGlyphCache* cache;
globals.validate();
@@ -660,7 +658,7 @@
///////////////////////////////////////////////////////////////////////////////
void SkGlyphCache_Globals::attachCacheToHead(SkGlyphCache* cache) {
- Exclusive ac(fLock);
+ SkAutoExclusive ac(fLock);
this->validate();
cache->validate();
diff --git a/src/ports/SkScalerContext_win_dw.cpp b/src/ports/SkScalerContext_win_dw.cpp
index 6f1d6fe..c9ff5d8 100644
--- a/src/ports/SkScalerContext_win_dw.cpp
+++ b/src/ports/SkScalerContext_win_dw.cpp
@@ -42,7 +42,6 @@
*/
static SkSharedMutex DWriteFactoryMutex;
-typedef SkAutoTExclusive<SkSharedMutex> Exclusive;
typedef SkAutoSharedMutexShared Shared;
static bool isLCD(const SkScalerContext::Rec& rec) {
@@ -50,7 +49,7 @@
}
static bool is_hinted_without_gasp(DWriteFontTypeface* typeface) {
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
AutoTDWriteTable<SkOTTableMaximumProfile> maxp(typeface->fDWriteFontFace.get());
if (!maxp.fExists) {
return false;
@@ -120,7 +119,7 @@
}
static bool has_bitmap_strike(DWriteFontTypeface* typeface, PPEMRange range) {
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
{
AutoTDWriteTable<SkOTTableEmbeddedBitmapLocation> eblc(typeface->fDWriteFontFace.get());
if (!eblc.fExists) {
@@ -363,7 +362,7 @@
if (DWRITE_MEASURING_MODE_GDI_CLASSIC == fMeasuringMode ||
DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode)
{
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
HRVM(fTypeface->fDWriteFontFace->GetGdiCompatibleGlyphMetrics(
fTextSizeMeasure,
1.0f, // pixelsPerDip
@@ -373,7 +372,7 @@
&gm),
"Could not get gdi compatible glyph metrics.");
} else {
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
HRVM(fTypeface->fDWriteFontFace->GetDesignGlyphMetrics(&glyphId, 1, &gm),
"Could not get design metrics.");
}
@@ -432,7 +431,7 @@
SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis;
{
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
HRM(fTypeface->fFactory->CreateGlyphRunAnalysis(
&run,
1.0f, // pixelsPerDip,
@@ -737,7 +736,7 @@
SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis;
{
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
HRNM(fTypeface->fFactory->CreateGlyphRunAnalysis(&run,
1.0f, // pixelsPerDip,
&fXform,
@@ -821,7 +820,7 @@
HRVM(SkDWriteGeometrySink::Create(&path, &geometryToPath),
"Could not create geometry to path converter.");
{
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
HRVM(colorGlyph->glyphRun.fontFace->GetGlyphRunOutline(
colorGlyph->glyphRun.fontEmSize,
colorGlyph->glyphRun.glyphIndices,
@@ -899,7 +898,7 @@
"Could not create geometry to path converter.");
uint16_t glyphId = glyph.getGlyphID();
{
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
//TODO: convert to<->from DIUs? This would make a difference if hinting.
//It may not be needed, it appears that DirectWrite only hints at em size.
HRVM(fTypeface->fDWriteFontFace->GetGlyphRunOutline(SkScalarToFloat(fTextSizeRender),