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

Unified Diff: include/private/SkMutex.h

Issue 1555953004: Create debug only SkSingleOwner (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: create SkSingleOwner Created 4 years, 11 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
Index: include/private/SkMutex.h
diff --git a/include/private/SkMutex.h b/include/private/SkMutex.h
index 8c78e1205c8796261be4b19468338a00af8afdc5..d2a2d8230deb2503f211c190380badd351267d31 100644
--- a/include/private/SkMutex.h
+++ b/include/private/SkMutex.h
@@ -122,4 +122,40 @@ typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire;
typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive;
#define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive)
+#ifdef SK_DEBUG
+// This is a debug tool to verify an object is only being used from one thread at a time
+class SkSingleOwner {
+public:
+ SkSingleOwner() : fOwner(kIllegalThreadID), fReentranceCount(0) {}
+
+ struct AutoEnforce {
+ AutoEnforce(SkSingleOwner* so) : fSO(so) { fSO->enter(); }
+ ~AutoEnforce() { fSO->exit(); }
+
+ SkSingleOwner* fSO;
+ };
+
+private:
+ void enter() {
+ SkAutoMutexAcquire lock(fMutex);
+ SkThreadID self = SkGetThreadID();
+ SkASSERT(fOwner == self || fOwner == kIllegalThreadID);
+ fReentranceCount++;
+ fOwner = self;
+ }
+
+ void exit() {
+ SkAutoMutexAcquire lock(fMutex);
+ fReentranceCount--;
+ if (fReentranceCount == 0) {
+ fOwner = kIllegalThreadID;
+ }
+ }
+
+ SkMutex fMutex;
+ SkThreadID fOwner; // guarded by fMutex
+ int fReentranceCount; // guarded by fMutex
+};
+#endif
+
#endif//SkMutex_DEFINED
« include/gpu/GrContext.h ('K') | « include/gpu/GrContext.h ('k') | src/gpu/GrContext.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698