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 #include "../private/SkSemaphore.h" | 12 #include "../private/SkSemaphore.h" |
12 #include "../private/SkThreadID.h" | |
13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
14 | 14 |
15 // TODO: no need for this anymore. | 15 #ifdef SK_DEBUG |
16 #define SK_DECLARE_STATIC_MUTEX(name) static SkMutex name; | 16 #include "../private/SkThreadID.h" |
| 17 #endif |
17 | 18 |
18 class SkMutex { | 19 #define SK_MUTEX_SEMAPHORE_INIT {1, {0}} |
19 public: | |
20 constexpr SkMutex() = default; | |
21 | 20 |
22 SkMutex(const SkMutex&) = delete; | 21 #ifdef SK_DEBUG |
23 SkMutex& operator=(const SkMutex&) = delete; | 22 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0} |
| 23 #else |
| 24 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT} |
| 25 #endif |
24 | 26 |
| 27 // Using POD-style initialization prevents the generation of a static initialize
r. |
| 28 // |
| 29 // Without magic statics there are no thread safety guarantees on initialization |
| 30 // of local statics (even POD). As a result, it is illegal to use |
| 31 // SK_DECLARE_STATIC_MUTEX in a function. |
| 32 // |
| 33 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be |
| 34 // initialized in a class with this macro. |
| 35 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_
BASE_MUTEX_INIT; |
| 36 |
| 37 struct SkBaseMutex { |
25 void acquire() { | 38 void acquire() { |
26 fSemaphore.wait(); | 39 fSemaphore.wait(); |
27 SkDEBUGCODE(fOwner = SkGetThreadID();) | 40 SkDEBUGCODE(fOwner = SkGetThreadID();) |
28 } | 41 } |
29 | 42 |
30 void release() { | 43 void release() { |
31 this->assertHeld(); | 44 this->assertHeld(); |
32 SkDEBUGCODE(fOwner = kIllegalThreadID;) | 45 SkDEBUGCODE(fOwner = kIllegalThreadID;) |
33 fSemaphore.signal(); | 46 fSemaphore.signal(); |
34 } | 47 } |
35 | 48 |
36 void assertHeld() { | 49 void assertHeld() { |
37 SkASSERT(fOwner == SkGetThreadID()); | 50 SkASSERT(fOwner == SkGetThreadID()); |
38 } | 51 } |
39 | 52 |
40 private: | 53 SkBaseSemaphore fSemaphore; |
41 SkSemaphore fSemaphore{1}; | 54 SkDEBUGCODE(SkThreadID fOwner;) |
42 SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};) | 55 }; |
| 56 |
| 57 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq
uire to work. |
| 58 class SkMutex : public SkBaseMutex { |
| 59 public: |
| 60 SkMutex () { |
| 61 fSemaphore = SK_MUTEX_SEMAPHORE_INIT; |
| 62 SkDEBUGCODE(fOwner = kIllegalThreadID); |
| 63 } |
| 64 ~SkMutex () { fSemaphore.deleteSemaphore(); } |
| 65 SkMutex(const SkMutex&) = delete; |
| 66 SkMutex& operator=(const SkMutex&) = delete; |
43 }; | 67 }; |
44 | 68 |
45 template <typename Lock> | 69 template <typename Lock> |
46 class SkAutoTAcquire : SkNoncopyable { | 70 class SkAutoTAcquire : SkNoncopyable { |
47 public: | 71 public: |
48 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { | 72 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { |
49 SkASSERT(fMutex != nullptr); | 73 SkASSERT(fMutex != nullptr); |
50 mutex.acquire(); | 74 mutex.acquire(); |
51 } | 75 } |
52 | 76 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 // mutex, thus removing the check for the null pointer. | 109 // mutex, thus removing the check for the null pointer. |
86 template <typename Lock> | 110 template <typename Lock> |
87 class SkAutoTExclusive { | 111 class SkAutoTExclusive { |
88 public: | 112 public: |
89 SkAutoTExclusive(Lock& lock) : fLock(lock) { lock.acquire(); } | 113 SkAutoTExclusive(Lock& lock) : fLock(lock) { lock.acquire(); } |
90 ~SkAutoTExclusive() { fLock.release(); } | 114 ~SkAutoTExclusive() { fLock.release(); } |
91 private: | 115 private: |
92 Lock &fLock; | 116 Lock &fLock; |
93 }; | 117 }; |
94 | 118 |
95 typedef SkAutoTAcquire<SkMutex> SkAutoMutexAcquire; | 119 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; |
96 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 120 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
97 | 121 |
98 typedef SkAutoTExclusive<SkMutex> SkAutoMutexExclusive; | 122 typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive; |
99 #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive) | 123 #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive) |
100 | 124 |
101 #endif//SkMutex_DEFINED | 125 #endif//SkMutex_DEFINED |
OLD | NEW |