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. | |
12 #include "../private/SkSemaphore.h" | 11 #include "../private/SkSemaphore.h" |
| 12 #include "../private/SkThreadID.h" |
13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
14 | 14 |
15 #ifdef SK_DEBUG | 15 #define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name; |
16 #include "../private/SkThreadID.h" | |
17 #endif | |
18 | 16 |
19 #define SK_MUTEX_SEMAPHORE_INIT {1, {0}} | 17 class SkBaseMutex { |
| 18 public: |
| 19 constexpr SkBaseMutex() = default; |
20 | 20 |
21 #ifdef SK_DEBUG | |
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 | |
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 { | |
38 void acquire() { | 21 void acquire() { |
39 fSemaphore.wait(); | 22 fSemaphore.wait(); |
40 SkDEBUGCODE(fOwner = SkGetThreadID();) | 23 SkDEBUGCODE(fOwner = SkGetThreadID();) |
41 } | 24 } |
42 | 25 |
43 void release() { | 26 void release() { |
44 this->assertHeld(); | 27 this->assertHeld(); |
45 SkDEBUGCODE(fOwner = kIllegalThreadID;) | 28 SkDEBUGCODE(fOwner = kIllegalThreadID;) |
46 fSemaphore.signal(); | 29 fSemaphore.signal(); |
47 } | 30 } |
48 | 31 |
49 void assertHeld() { | 32 void assertHeld() { |
50 SkASSERT(fOwner == SkGetThreadID()); | 33 SkASSERT(fOwner == SkGetThreadID()); |
51 } | 34 } |
52 | 35 |
53 SkBaseSemaphore fSemaphore; | 36 protected: |
54 SkDEBUGCODE(SkThreadID fOwner;) | 37 SkBaseSemaphore fSemaphore{1}; |
| 38 SkDEBUGCODE(SkThreadID fOwner{kIllegalThreadID};) |
55 }; | 39 }; |
56 | 40 |
57 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq
uire to work. | |
58 class SkMutex : public SkBaseMutex { | 41 class SkMutex : public SkBaseMutex { |
59 public: | 42 public: |
60 SkMutex () { | 43 using SkBaseMutex::SkBaseMutex; |
61 fSemaphore = SK_MUTEX_SEMAPHORE_INIT; | 44 ~SkMutex() { fSemaphore.cleanup(); } |
62 SkDEBUGCODE(fOwner = kIllegalThreadID); | |
63 } | |
64 ~SkMutex () { fSemaphore.deleteSemaphore(); } | |
65 SkMutex(const SkMutex&) = delete; | |
66 SkMutex& operator=(const SkMutex&) = delete; | |
67 }; | 45 }; |
68 | 46 |
69 template <typename Lock> | 47 template <typename Lock> |
70 class SkAutoTAcquire : SkNoncopyable { | 48 class SkAutoTAcquire : SkNoncopyable { |
71 public: | 49 public: |
72 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { | 50 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { |
73 SkASSERT(fMutex != nullptr); | 51 SkASSERT(fMutex != nullptr); |
74 mutex.acquire(); | 52 mutex.acquire(); |
75 } | 53 } |
76 | 54 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 Lock &fLock; | 94 Lock &fLock; |
117 }; | 95 }; |
118 | 96 |
119 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; | 97 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; |
120 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 98 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
121 | 99 |
122 typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive; | 100 typedef SkAutoTExclusive<SkBaseMutex> SkAutoMutexExclusive; |
123 #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive) | 101 #define SkAutoMutexExclusive(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexExclusive) |
124 | 102 |
125 #endif//SkMutex_DEFINED | 103 #endif//SkMutex_DEFINED |
OLD | NEW |