From b83f6c3cbdabc14d8290b00d9f38ba59bf6719a1 Mon Sep 17 00:00:00 2001 From: mtklein Date: Mon, 9 Jun 2014 14:18:02 -0700 Subject: Add assertHeld() to SkMutex. BUG=skia: R=bungeman@google.com, mtklein@google.com, reed@google.com Author: mtklein@chromium.org Review URL: https://codereview.chromium.org/313823004 --- src/ports/SkMutex_pthread.h | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'src/ports/SkMutex_pthread.h') diff --git a/src/ports/SkMutex_pthread.h b/src/ports/SkMutex_pthread.h index d9f1ae301e..1904140c4c 100644 --- a/src/ports/SkMutex_pthread.h +++ b/src/ports/SkMutex_pthread.h @@ -10,16 +10,6 @@ /** Posix pthread_mutex based mutex. */ -#ifdef SK_DEBUG_PTHREAD_MUTEX -#include "SkTypes.h" -#define SkDEBUGCODE_PTHREAD_MUTEX(code) code -#else -#define SkDEBUGCODE_PTHREAD_MUTEX(code) -#ifndef SkDebugf - void SkDebugf(const char format[], ...); -#endif -#endif - #include #include @@ -28,9 +18,21 @@ // generation of a static initializer in the final machine code (and // a corresponding static finalizer). struct SkBaseMutex { - void acquire() { pthread_mutex_lock(&fMutex); } - void release() { pthread_mutex_unlock(&fMutex); } + void acquire() { + pthread_mutex_lock(&fMutex); + SkDEBUGCODE(fOwner = pthread_self();) + } + void release() { + this->assertHeld(); + SkDEBUGCODE(fOwner = 0;) + pthread_mutex_unlock(&fMutex); + } + void assertHeld() { + SkASSERT(pthread_self() == fOwner); + } + pthread_mutex_t fMutex; + SkDEBUGCODE(pthread_t fOwner;) }; // A normal mutex that requires to be initialized through normal C++ construction, @@ -38,8 +40,8 @@ struct SkBaseMutex { class SkMutex : public SkBaseMutex { public: SkMutex() { - SkDEBUGCODE_PTHREAD_MUTEX(int status = )pthread_mutex_init(&fMutex, NULL); - SkDEBUGCODE_PTHREAD_MUTEX( + SkDEBUGCODE(int status = )pthread_mutex_init(&fMutex, NULL); + SkDEBUGCODE( if (status != 0) { print_pthread_error(status); SkASSERT(0 == status); @@ -48,8 +50,8 @@ public: } ~SkMutex() { - SkDEBUGCODE_PTHREAD_MUTEX(int status = )pthread_mutex_destroy(&fMutex); - SkDEBUGCODE_PTHREAD_MUTEX( + SkDEBUGCODE(int status = )pthread_mutex_destroy(&fMutex); + SkDEBUGCODE( if (status != 0) { print_pthread_error(status); SkASSERT(0 == status); @@ -78,10 +80,12 @@ private: } }; +#define SK_BASE_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER, SkDEBUGCODE(0) } + // Using POD-style initialization prevents the generation of a static initializer. -#define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = { PTHREAD_MUTEX_INITIALIZER } +#define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = SK_BASE_MUTEX_INIT // Special case used when the static mutex must be available globally. -#define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = { PTHREAD_MUTEX_INITIALIZER } +#define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = SK_BASE_MUTEX_INIT #endif -- cgit v1.2.3