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 "SkTypes.h" | 13 #include "SkTypes.h" |
13 | 14 |
14 // IWYU pragma: begin_exports | 15 #define SK_MUTEX_SEMAPHORE_INIT {1, {0}} |
15 #if defined(SK_BUILD_FOR_WIN) | 16 |
16 #include "../ports/SkMutex_win.h" | 17 #ifdef SK_DEBUG |
| 18 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT, 0} |
17 #else | 19 #else |
18 #include "../ports/SkMutex_pthread.h" | 20 #define SK_BASE_MUTEX_INIT {SK_MUTEX_SEMAPHORE_INIT} |
19 #endif | 21 #endif |
20 // IWYU pragma: end_exports | 22 |
| 23 // Using POD-style initialization prevents the generation of a static initialize
r. |
| 24 // |
| 25 // 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 |
| 27 // SK_DECLARE_STATIC_MUTEX in a function. |
| 28 // |
| 29 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be |
| 30 // initialized in a class with this macro. |
| 31 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_
BASE_MUTEX_INIT; |
| 32 |
| 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 { |
| 49 void acquire() { |
| 50 fSemaphore.wait(); |
| 51 SkDEBUGCODE(fOwner = sk_get_thread_id();) |
| 52 } |
| 53 |
| 54 void release() { |
| 55 this->assertHeld(); |
| 56 SkDEBUGCODE(fOwner = kNoOwner;) |
| 57 fSemaphore.signal(); |
| 58 } |
| 59 |
| 60 void assertHeld() { |
| 61 SkASSERT(fOwner == sk_get_thread_id()); |
| 62 } |
| 63 |
| 64 SkBaseSemaphore fSemaphore; |
| 65 SkDEBUGCODE(SkThreadID fOwner;) |
| 66 }; |
| 67 |
| 68 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq
uire to work. |
| 69 class SkMutex : public SkBaseMutex { |
| 70 public: |
| 71 SkMutex () { |
| 72 fSemaphore = SK_MUTEX_SEMAPHORE_INIT; |
| 73 SkDEBUGCODE(fOwner = kNoOwner); |
| 74 } |
| 75 ~SkMutex () { fSemaphore.deleteSemaphore(); } |
| 76 SkMutex(const SkMutex&) = delete; |
| 77 SkMutex& operator=(const SkMutex&) = delete; |
| 78 }; |
21 | 79 |
22 template <typename Lock> | 80 template <typename Lock> |
23 class SkAutoTAcquire : SkNoncopyable { | 81 class SkAutoTAcquire : SkNoncopyable { |
24 public: | 82 public: |
25 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { | 83 explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { |
26 SkASSERT(fMutex != NULL); | 84 SkASSERT(fMutex != NULL); |
27 mutex.acquire(); | 85 mutex.acquire(); |
28 } | 86 } |
29 | 87 |
30 explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) { | 88 explicit SkAutoTAcquire(Lock* mutex) : fMutex(mutex) { |
(...skipping 21 matching lines...) Expand all Loading... |
52 void assertHeld() { | 110 void assertHeld() { |
53 SkASSERT(fMutex); | 111 SkASSERT(fMutex); |
54 fMutex->assertHeld(); | 112 fMutex->assertHeld(); |
55 } | 113 } |
56 | 114 |
57 private: | 115 private: |
58 Lock* fMutex; | 116 Lock* fMutex; |
59 }; | 117 }; |
60 | 118 |
61 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; | 119 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; |
62 | |
63 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 120 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
64 | 121 |
65 | |
66 #endif//SkMutex_DEFINED | 122 #endif//SkMutex_DEFINED |
OLD | NEW |