| 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 |
| 11 // This file is not part of the public Skia API. | 11 // This file is not part of the public Skia API. |
| 12 #include "../private/SkSemaphore.h" | 12 #include "../private/SkSemaphore.h" |
| 13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 14 | 14 |
| 15 #ifdef SK_DEBUG |
| 16 #include "../private/SkThreadID.h" |
| 17 #endif |
| 18 |
| 15 #define SK_MUTEX_SEMAPHORE_INIT {1, {0}} | 19 #define SK_MUTEX_SEMAPHORE_INIT {1, {0}} |
| 16 | 20 |
| 17 #ifdef SK_DEBUG | 21 #ifdef SK_DEBUG |
| 18 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0} | 22 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0} |
| 19 #else | 23 #else |
| 20 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT} | 24 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT} |
| 21 #endif | 25 #endif |
| 22 | 26 |
| 23 // Using POD-style initialization prevents the generation of a static initialize
r. | 27 // Using POD-style initialization prevents the generation of a static initialize
r. |
| 24 // | 28 // |
| 25 // Without magic statics there are no thread safety guarantees on initialization | 29 // Without magic statics there are no thread safety guarantees on initialization |
| 26 // of local statics (even POD). As a result, it is illegal to use | 30 // of local statics (even POD). As a result, it is illegal to use |
| 27 // SK_DECLARE_STATIC_MUTEX in a function. | 31 // SK_DECLARE_STATIC_MUTEX in a function. |
| 28 // | 32 // |
| 29 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be | 33 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be |
| 30 // initialized in a class with this macro. | 34 // initialized in a class with this macro. |
| 31 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_
BASE_MUTEX_INIT; | 35 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_
BASE_MUTEX_INIT; |
| 32 | 36 |
| 33 // TODO(herb): unify this with the ThreadID in SkSharedMutex.cpp. | |
| 34 #ifdef SK_DEBUG | |
| 35 #ifdef SK_BUILD_FOR_WIN | |
| 36 #include <windows.h> | |
| 37 inline int64_t sk_get_thread_id() { return GetCurrentThreadId(); } | |
| 38 #else | |
| 39 #include <pthread.h> | |
| 40 inline int64_t sk_get_thread_id() { return (int64_t)pthread_self(); } | |
| 41 #endif | |
| 42 #endif | |
| 43 | |
| 44 typedef int64_t SkThreadID; | |
| 45 | |
| 46 SkDEBUGCODE(static const SkThreadID kNoOwner = 0;) | |
| 47 | |
| 48 struct SkBaseMutex { | 37 struct SkBaseMutex { |
| 49 void acquire() { | 38 void acquire() { |
| 50 fSemaphore.wait(); | 39 fSemaphore.wait(); |
| 51 SkDEBUGCODE(fOwner = sk_get_thread_id();) | 40 SkDEBUGCODE(fOwner = SkGetThreadID();) |
| 52 } | 41 } |
| 53 | 42 |
| 54 void release() { | 43 void release() { |
| 55 this->assertHeld(); | 44 this->assertHeld(); |
| 56 SkDEBUGCODE(fOwner = kNoOwner;) | 45 SkDEBUGCODE(fOwner = kIllegalThreadID;) |
| 57 fSemaphore.signal(); | 46 fSemaphore.signal(); |
| 58 } | 47 } |
| 59 | 48 |
| 60 void assertHeld() { | 49 void assertHeld() { |
| 61 SkASSERT(fOwner == sk_get_thread_id()); | 50 SkASSERT(fOwner == SkGetThreadID()); |
| 62 } | 51 } |
| 63 | 52 |
| 64 SkBaseSemaphore fSemaphore; | 53 SkBaseSemaphore fSemaphore; |
| 65 SkDEBUGCODE(SkThreadID fOwner;) | 54 SkDEBUGCODE(SkThreadID fOwner;) |
| 66 }; | 55 }; |
| 67 | 56 |
| 68 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq
uire to work. | 57 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq
uire to work. |
| 69 class SkMutex : public SkBaseMutex { | 58 class SkMutex : public SkBaseMutex { |
| 70 public: | 59 public: |
| 71 SkMutex () { | 60 SkMutex () { |
| 72 fSemaphore = SK_MUTEX_SEMAPHORE_INIT; | 61 fSemaphore = SK_MUTEX_SEMAPHORE_INIT; |
| 73 SkDEBUGCODE(fOwner = kNoOwner); | 62 SkDEBUGCODE(fOwner = kIllegalThreadID); |
| 74 } | 63 } |
| 75 ~SkMutex () { fSemaphore.deleteSemaphore(); } | 64 ~SkMutex () { fSemaphore.deleteSemaphore(); } |
| 76 SkMutex(const SkMutex&) = delete; | 65 SkMutex(const SkMutex&) = delete; |
| 77 SkMutex& operator=(const SkMutex&) = delete; | 66 SkMutex& operator=(const SkMutex&) = delete; |
| 78 }; | 67 }; |
| 79 | 68 |
| 80 template <typename Lock> | 69 template <typename Lock> |
| 81 class SkAutoTAcquire : SkNoncopyable { | 70 class SkAutoTAcquire : SkNoncopyable { |
| 82 public: | 71 public: |
| 83 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { | 72 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { |
| 84 SkASSERT(fMutex != NULL); | 73 SkASSERT(fMutex != nullptr); |
| 85 mutex.acquire(); | 74 mutex.acquire(); |
| 86 } | 75 } |
| 87 | 76 |
| 88 explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) { | 77 explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) { |
| 89 if (mutex) { | 78 if (mutex) { |
| 90 mutex->acquire(); | 79 mutex->acquire(); |
| 91 } | 80 } |
| 92 } | 81 } |
| 93 | 82 |
| 94 /** If the mutex has not been released, release it now. */ | 83 /** If the mutex has not been released, release it now. */ |
| 95 ~SkAutoTAcquire() { | 84 ~SkAutoTAcquire() { |
| 96 if (fMutex) { | 85 if (fMutex) { |
| 97 fMutex->release(); | 86 fMutex->release(); |
| 98 } | 87 } |
| 99 } | 88 } |
| 100 | 89 |
| 101 /** If the mutex has not been released, release it now. */ | 90 /** If the mutex has not been released, release it now. */ |
| 102 void release() { | 91 void release() { |
| 103 if (fMutex) { | 92 if (fMutex) { |
| 104 fMutex->release(); | 93 fMutex->release(); |
| 105 fMutex = NULL; | 94 fMutex = nullptr; |
| 106 } | 95 } |
| 107 } | 96 } |
| 108 | 97 |
| 109 /** Assert that we're holding the mutex. */ | 98 /** Assert that we're holding the mutex. */ |
| 110 void assertHeld() { | 99 void assertHeld() { |
| 111 SkASSERT(fMutex); | 100 SkASSERT(fMutex); |
| 112 fMutex->assertHeld(); | 101 fMutex->assertHeld(); |
| 113 } | 102 } |
| 114 | 103 |
| 115 private: | 104 private: |
| 116 Lock* fMutex; | 105 Lock* fMutex; |
| 117 }; | 106 }; |
| 118 | 107 |
| 119 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; | 108 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; |
| 120 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 109 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
| 121 | 110 |
| 122 #endif//SkMutex_DEFINED | 111 #endif//SkMutex_DEFINED |
| OLD | NEW |