| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkMutex_DEFINED | 8 #ifndef SkMutex_DEFINED |
| 9 #define SkMutex_DEFINED | 9 #define SkMutex_DEFINED |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 SkBaseSemaphore fSemaphore{1}; | 37 SkBaseSemaphore fSemaphore{1}; |
| 38 SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};) | 38 SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};) |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 class SkMutex : public SkBaseMutex { | 41 class SkMutex : public SkBaseMutex { |
| 42 public: | 42 public: |
| 43 using SkBaseMutex::SkBaseMutex; | 43 using SkBaseMutex::SkBaseMutex; |
| 44 ~SkMutex() { fSemaphore.cleanup(); } | 44 ~SkMutex() { fSemaphore.cleanup(); } |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 template <typename Lock> | 47 class SkAutoMutexAcquire { |
| 48 class SkAutoTAcquire : SkNoncopyable { | |
| 49 public: | 48 public: |
| 50 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { | 49 template <typename T> |
| 51 SkASSERT(fMutex != nullptr); | 50 SkAutoMutexAcquire(T* mutex) : fMutex(mutex) { |
| 52 mutex.acquire(); | |
| 53 } | |
| 54 | |
| 55 explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) { | |
| 56 if (mutex) { | 51 if (mutex) { |
| 57 mutex->acquire(); | 52 mutex->acquire(); |
| 58 } | 53 } |
| 54 fRelease = [](void* mutex) { ((T*)mutex)->release(); }; |
| 59 } | 55 } |
| 60 | 56 |
| 61 /** If the mutex has not been released, release it now. */ | 57 template <typename T> |
| 62 ~SkAutoTAcquire() { | 58 SkAutoMutexAcquire(T& mutex) : SkAutoMutexAcquire(&mutex) {} |
| 63 if (fMutex) { | |
| 64 fMutex->release(); | |
| 65 } | |
| 66 } | |
| 67 | 59 |
| 68 /** If the mutex has not been released, release it now. */ | 60 ~SkAutoMutexAcquire() { this->release(); } |
| 61 |
| 69 void release() { | 62 void release() { |
| 70 if (fMutex) { | 63 if (fMutex) { |
| 71 fMutex->release(); | 64 fRelease(fMutex); |
| 72 fMutex = nullptr; | |
| 73 } | 65 } |
| 74 } | 66 fMutex = nullptr; |
| 75 | |
| 76 /** Assert that we're holding the mutex. */ | |
| 77 void assertHeld() { | |
| 78 SkASSERT(fMutex); | |
| 79 fMutex->assertHeld(); | |
| 80 } | 67 } |
| 81 | 68 |
| 82 private: | 69 private: |
| 83 Lock* fMutex; | 70 void* fMutex; |
| 71 void (*fRelease)(void*); |
| 84 }; | 72 }; |
| 85 | |
| 86 // SkAutoTExclusive is a lighter weight version of SkAutoTAcquire. It assumes th
at there is a valid | |
| 87 // mutex, thus removing the check for the null pointer. | |
| 88 template <typename Lock> | |
| 89 class SkAutoTExclusive { | |
| 90 public: | |
| 91 SkAutoTExclusive(Lock& lock) : fLock(lock) { lock.acquire(); } | |
| 92 ~SkAutoTExclusive() { fLock.release(); } | |
| 93 private: | |
| 94 Lock &fLock; | |
| 95 }; | |
| 96 | |
| 97 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; | |
| 98 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 73 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
| 99 | 74 |
| 100 typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive; | 75 // SkAutoExclusive is a lighter weight version of SkAutoMutexAcquire. |
| 101 #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive) | 76 // It assumes that there is a valid mutex, obviating the null check. |
| 77 class SkAutoExclusive { |
| 78 public: |
| 79 template <typename T> |
| 80 SkAutoExclusive(T& mutex) : fMutex(&mutex) { |
| 81 mutex.acquire(); |
| 82 |
| 83 fRelease = [](void* mutex) { ((T*)mutex)->release(); }; |
| 84 } |
| 85 ~SkAutoExclusive() { fRelease(fMutex); } |
| 86 |
| 87 private: |
| 88 void* fMutex; |
| 89 void (*fRelease)(void*); |
| 90 }; |
| 91 #define SkAutoExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoExclusive) |
| 102 | 92 |
| 103 #endif//SkMutex_DEFINED | 93 #endif//SkMutex_DEFINED |
| OLD | NEW |