1 #ifndef SkMutex_DEFINED 2 #define SkMutex_DEFINED 3 4 // This file is not part of the public Skia API. 5 #include "SkTypes.h" 6 7 #if defined(SK_BUILD_FOR_WIN) 8 #include "../ports/SkMutex_win.h" 9 #else 10 #include "../ports/SkMutex_pthread.h" 11 #endif 12 13 class SkAutoMutexAcquire : SkNoncopyable { 14 public: SkAutoMutexAcquire(SkBaseMutex & mutex)15 explicit SkAutoMutexAcquire(SkBaseMutex& mutex) : fMutex(&mutex) { 16 SkASSERT(fMutex != NULL); 17 mutex.acquire(); 18 } 19 SkAutoMutexAcquire(SkBaseMutex * mutex)20 explicit SkAutoMutexAcquire(SkBaseMutex* mutex) : fMutex(mutex) { 21 if (mutex) { 22 mutex->acquire(); 23 } 24 } 25 26 /** If the mutex has not been released, release it now. */ ~SkAutoMutexAcquire()27 ~SkAutoMutexAcquire() { 28 if (fMutex) { 29 fMutex->release(); 30 } 31 } 32 33 /** If the mutex has not been released, release it now. */ release()34 void release() { 35 if (fMutex) { 36 fMutex->release(); 37 fMutex = NULL; 38 } 39 } 40 41 /** Assert that we're holding the mutex. */ assertHeld()42 void assertHeld() { 43 SkASSERT(fMutex); 44 fMutex->assertHeld(); 45 } 46 47 private: 48 SkBaseMutex* fMutex; 49 }; 50 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) 51 52 53 #endif//SkMutex_DEFINED 54