aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar mtklein <mtklein@chromium.org>2016-06-10 13:56:35 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-06-10 13:56:35 -0700
commitf10637f2795b147751ef413237389d230b869360 (patch)
treec541e51ce4ced899de02eaff49f06aa82f125fda
parent8151103535bab8639dab2cb308b1b9c04651b632 (diff)
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
-rw-r--r--dm/DM.cpp12
-rw-r--r--include/private/SkMutex.h64
-rw-r--r--src/core/SkGlyphCache.cpp14
-rw-r--r--src/ports/SkScalerContext_win_dw.cpp17
4 files changed, 47 insertions, 60 deletions
diff --git a/dm/DM.cpp b/dm/DM.cpp
index 9ddff282f3..158d256fad 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -128,7 +128,7 @@ static void done(const char* config, const char* src, const char* srcOptions, co
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 done(const char* config, const char* src, const char* srcOptions, co
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 @@ static void print_status() {
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 @@ static void print_status() {
#undef _
};
- SkAutoTAcquire<SkSpinlock> lock(gMutex);
+ SkAutoMutexAcquire lock(gMutex);
const DWORD code = e->ExceptionRecord->ExceptionCode;
info("\nCaught exception %u", code);
@@ -205,7 +205,7 @@ static void print_status() {
#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 @@ int dm_main() {
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 3b0e1c47ca..7cfdb1132c 100644
--- a/include/private/SkMutex.h
+++ b/include/private/SkMutex.h
@@ -44,60 +44,50 @@ public:
~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) {}
+
+ ~SkAutoMutexAcquire() { this->release(); }
- /** If the mutex has not been released, release it now. */
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*);
};
+#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
-// 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 {
+// SkAutoExclusive is a lighter weight version of SkAutoMutexAcquire.
+// It assumes that there is a valid mutex, obviating the null check.
+class SkAutoExclusive {
public:
- SkAutoTExclusive(Lock& lock) : fLock(lock) { lock.acquire(); }
- ~SkAutoTExclusive() { fLock.release(); }
-private:
- Lock &fLock;
-};
+ template <typename T>
+ SkAutoExclusive(T& mutex) : fMutex(&mutex) {
+ mutex.acquire();
-typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
-#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
+ fRelease = [](void* mutex) { ((T*)mutex)->release(); };
+ }
+ ~SkAutoExclusive() { fRelease(fMutex); }
-typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive;
-#define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive)
+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 072541d146..30d00b2463 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -470,15 +470,13 @@ void SkGlyphCache::invokeAndRemoveAuxProcs() {
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
-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 @@ int SkGlyphCache_Globals::setCacheCountLimit(int newCount) {
newCount = 0;
}
- Exclusive ac(fLock);
+ SkAutoExclusive ac(fLock);
int prevCount = fCacheCountLimit;
fCacheCountLimit = newCount;
@@ -500,7 +498,7 @@ int SkGlyphCache_Globals::setCacheCountLimit(int newCount) {
}
void SkGlyphCache_Globals::purgeAll() {
- Exclusive ac(fLock);
+ SkAutoExclusive ac(fLock);
this->internalPurge(fTotalMemoryUsed);
}
@@ -534,7 +532,7 @@ SkGlyphCache* SkGlyphCache::VisitCache(SkTypeface* typeface,
SkGlyphCache* cache;
{
- Exclusive ac(globals.fLock);
+ SkAutoExclusive ac(globals.fLock);
globals.validate();
@@ -647,7 +645,7 @@ void SkGlyphCache::DumpMemoryStatistics(SkTraceMemoryDump* dump) {
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::VisitAll(Visitor visitor, void* context) {
///////////////////////////////////////////////////////////////////////////////
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 6f1d6fedc3..c9ff5d86c3 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 isLCD(const SkScalerContext::Rec& rec) {
}
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 void expand_range_if_gridfit_only(DWriteFontTypeface* typeface, int size,
}
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 @@ void SkScalerContext_DW::generateAdvance(SkGlyph* glyph) {
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 @@ void SkScalerContext_DW::generateAdvance(SkGlyph* glyph) {
&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 @@ HRESULT SkScalerContext_DW::getBoundingBox(SkGlyph* glyph,
SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis;
{
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
HRM(fTypeface->fFactory->CreateGlyphRunAnalysis(
&run,
1.0f, // pixelsPerDip,
@@ -737,7 +736,7 @@ const void* SkScalerContext_DW::drawDWMask(const SkGlyph& glyph,
SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis;
{
- Exclusive l(DWriteFactoryMutex);
+ SkAutoExclusive l(DWriteFactoryMutex);
HRNM(fTypeface->fFactory->CreateGlyphRunAnalysis(&run,
1.0f, // pixelsPerDip,
&fXform,
@@ -821,7 +820,7 @@ void SkScalerContext_DW::generateColorGlyphImage(const SkGlyph& glyph) {
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 @@ void SkScalerContext_DW::generatePath(const SkGlyph& glyph, SkPath* path) {
"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),