Chromium Code Reviews| 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 12 matching lines...) Expand all Loading... | |
| 23 // Using POD-style initialization prevents the generation of a static initialize r. | 23 // Using POD-style initialization prevents the generation of a static initialize r. |
| 24 // | 24 // |
| 25 // Without magic statics there are no thread safety guarantees on initialization | 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 | 26 // of local statics (even POD). As a result, it is illegal to use |
| 27 // SK_DECLARE_STATIC_MUTEX in a function. | 27 // SK_DECLARE_STATIC_MUTEX in a function. |
| 28 // | 28 // |
| 29 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be | 29 // Because SkBaseMutex is not a primitive, a static SkBaseMutex cannot be |
| 30 // initialized in a class with this macro. | 30 // initialized in a class with this macro. |
| 31 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_ BASE_MUTEX_INIT; | 31 #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_ BASE_MUTEX_INIT; |
| 32 | 32 |
| 33 // TODO(herb): unify this with the ThreadID in SkSharedMutex.cpp. | |
| 34 #ifdef SK_DEBUG | 33 #ifdef SK_DEBUG |
| 35 #ifdef SK_BUILD_FOR_WIN | 34 #include "../private/SkThreadID.h" |
| 36 #include <windows.h> | 35 static const SkThreadID kNoOwner = 0; |
|
mtklein
2015/09/29 19:52:40
Think it makes sense to move this knowledge into S
herb_g
2015/09/29 20:25:02
Done.
| |
| 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 | 36 #endif |
| 43 | 37 |
| 44 typedef int64_t SkThreadID; | |
| 45 | |
| 46 SkDEBUGCODE(static const SkThreadID kNoOwner = 0;) | |
| 47 | |
| 48 struct SkBaseMutex { | 38 struct SkBaseMutex { |
| 49 void acquire() { | 39 void acquire() { |
| 50 fSemaphore.wait(); | 40 fSemaphore.wait(); |
| 51 SkDEBUGCODE(fOwner = sk_get_thread_id();) | 41 SkDEBUGCODE(fOwner = SkGetThreadID();) |
| 52 } | 42 } |
| 53 | 43 |
| 54 void release() { | 44 void release() { |
| 55 this->assertHeld(); | 45 this->assertHeld(); |
| 56 SkDEBUGCODE(fOwner = kNoOwner;) | 46 SkDEBUGCODE(fOwner = kNoOwner;) |
| 57 fSemaphore.signal(); | 47 fSemaphore.signal(); |
| 58 } | 48 } |
| 59 | 49 |
| 60 void assertHeld() { | 50 void assertHeld() { |
| 61 SkASSERT(fOwner == sk_get_thread_id()); | 51 SkASSERT(fOwner == SkGetThreadID()); |
| 62 } | 52 } |
| 63 | 53 |
| 64 SkBaseSemaphore fSemaphore; | 54 SkBaseSemaphore fSemaphore; |
| 65 SkDEBUGCODE(SkThreadID fOwner;) | 55 SkDEBUGCODE(SkThreadID fOwner;) |
| 66 }; | 56 }; |
| 67 | 57 |
| 68 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq uire to work. | 58 // This needs to use subclassing instead of encapsulation to make SkAutoMutexAcq uire to work. |
| 69 class SkMutex : public SkBaseMutex { | 59 class SkMutex : public SkBaseMutex { |
| 70 public: | 60 public: |
| 71 SkMutex () { | 61 SkMutex () { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 } | 103 } |
| 114 | 104 |
| 115 private: | 105 private: |
| 116 Lock* fMutex; | 106 Lock* fMutex; |
| 117 }; | 107 }; |
| 118 | 108 |
| 119 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; | 109 typedef SkAutoTAcquire<SkBaseMutex> SkAutoMutexAcquire; |
| 120 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 110 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
| 121 | 111 |
| 122 #endif//SkMutex_DEFINED | 112 #endif//SkMutex_DEFINED |
| OLD | NEW |