aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/private/GrSingleOwner.h
diff options
context:
space:
mode:
authorGravatar joshualitt <joshualitt@chromium.org>2016-01-06 08:26:09 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2016-01-06 08:26:09 -0800
commit1de610a5287cf61d4f3a1fdc7413bd74827a8b6a (patch)
tree627973d2ea356962462fbfb9484c2bdf4cdfc82b /include/private/GrSingleOwner.h
parente114dd63161b5efffbffb17e4d3facad77f34507 (diff)
Create debug only SkSingleOwner
This is so Gpu code can guard against improper multithreaded usage in debug builds TBR=bsalomon@google.com BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1555953004 Review URL: https://codereview.chromium.org/1555953004
Diffstat (limited to 'include/private/GrSingleOwner.h')
-rw-r--r--include/private/GrSingleOwner.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/include/private/GrSingleOwner.h b/include/private/GrSingleOwner.h
new file mode 100644
index 0000000000..e793e9e973
--- /dev/null
+++ b/include/private/GrSingleOwner.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrSingleOwner_DEFINED
+#define GrSingleOwner_DEFINED
+
+#include "SkTypes.h"
+
+#ifdef SK_DEBUG
+#include "SkMutex.h"
+#include "SkThreadID.h"
+
+// This is a debug tool to verify an object is only being used from one thread at a time.
+class GrSingleOwner {
+public:
+ GrSingleOwner() : fOwner(kIllegalThreadID), fReentranceCount(0) {}
+
+ struct AutoEnforce {
+ AutoEnforce(GrSingleOwner* so) : fSO(so) { fSO->enter(); }
+ ~AutoEnforce() { fSO->exit(); }
+
+ GrSingleOwner* 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