Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Unified Diff: include/private/SkMutex.h

Issue 2055023003: Type-erase SkAutoMutexAcquire and SkAutoExclusive. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dm/DM.cpp ('k') | src/core/SkGlyphCache.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/private/SkMutex.h
diff --git a/include/private/SkMutex.h b/include/private/SkMutex.h
index 3b0e1c47ca16e34cbeeb6e2b7b3b12d006815670..7cfdb1132c2f97a1795abc2728b0dcdbbccb3232 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
« no previous file with comments | « dm/DM.cpp ('k') | src/core/SkGlyphCache.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698