| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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_pthread_DEFINED | 8 #ifndef SkMutex_pthread_DEFINED |
| 9 #define SkMutex_pthread_DEFINED | 9 #define SkMutex_pthread_DEFINED |
| 10 | 10 |
| 11 /** Posix pthread_mutex based mutex. */ | 11 /** Posix pthread_mutex based mutex. */ |
| 12 | 12 |
| 13 #ifdef SK_DEBUG_PTHREAD_MUTEX | |
| 14 #include "SkTypes.h" | |
| 15 #define SkDEBUGCODE_PTHREAD_MUTEX(code) code | |
| 16 #else | |
| 17 #define SkDEBUGCODE_PTHREAD_MUTEX(code) | |
| 18 #ifndef SkDebugf | |
| 19 void SkDebugf(const char format[], ...); | |
| 20 #endif | |
| 21 #endif | |
| 22 | |
| 23 #include <errno.h> | 13 #include <errno.h> |
| 24 #include <pthread.h> | 14 #include <pthread.h> |
| 25 | 15 |
| 26 // A SkBaseMutex is a POD structure that can be directly initialized | 16 // A SkBaseMutex is a POD structure that can be directly initialized |
| 27 // at declaration time with SK_DECLARE_STATIC/GLOBAL_MUTEX. This avoids the | 17 // at declaration time with SK_DECLARE_STATIC/GLOBAL_MUTEX. This avoids the |
| 28 // generation of a static initializer in the final machine code (and | 18 // generation of a static initializer in the final machine code (and |
| 29 // a corresponding static finalizer). | 19 // a corresponding static finalizer). |
| 30 struct SkBaseMutex { | 20 struct SkBaseMutex { |
| 31 void acquire() { pthread_mutex_lock(&fMutex); } | 21 void acquire() { |
| 32 void release() { pthread_mutex_unlock(&fMutex); } | 22 pthread_mutex_lock(&fMutex); |
| 23 SkDEBUGCODE(fOwner = pthread_self();) |
| 24 } |
| 25 void release() { |
| 26 this->assertHeld(); |
| 27 SkDEBUGCODE(fOwner = 0;) |
| 28 pthread_mutex_unlock(&fMutex); |
| 29 } |
| 30 void assertHeld() { |
| 31 SkASSERT(pthread_self() == fOwner); |
| 32 } |
| 33 |
| 33 pthread_mutex_t fMutex; | 34 pthread_mutex_t fMutex; |
| 35 SkDEBUGCODE(pthread_t fOwner;) |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 // A normal mutex that requires to be initialized through normal C++ constructio
n, | 38 // A normal mutex that requires to be initialized through normal C++ constructio
n, |
| 37 // i.e. when it's a member of another class, or allocated on the heap. | 39 // i.e. when it's a member of another class, or allocated on the heap. |
| 38 class SkMutex : public SkBaseMutex { | 40 class SkMutex : public SkBaseMutex { |
| 39 public: | 41 public: |
| 40 SkMutex() { | 42 SkMutex() { |
| 41 SkDEBUGCODE_PTHREAD_MUTEX(int status = )pthread_mutex_init(&fMutex, NULL
); | 43 SkDEBUGCODE(int status = )pthread_mutex_init(&fMutex, NULL); |
| 42 SkDEBUGCODE_PTHREAD_MUTEX( | 44 SkDEBUGCODE( |
| 43 if (status != 0) { | 45 if (status != 0) { |
| 44 print_pthread_error(status); | 46 print_pthread_error(status); |
| 45 SkASSERT(0 == status); | 47 SkASSERT(0 == status); |
| 46 } | 48 } |
| 47 ) | 49 ) |
| 48 } | 50 } |
| 49 | 51 |
| 50 ~SkMutex() { | 52 ~SkMutex() { |
| 51 SkDEBUGCODE_PTHREAD_MUTEX(int status = )pthread_mutex_destroy(&fMutex); | 53 SkDEBUGCODE(int status = )pthread_mutex_destroy(&fMutex); |
| 52 SkDEBUGCODE_PTHREAD_MUTEX( | 54 SkDEBUGCODE( |
| 53 if (status != 0) { | 55 if (status != 0) { |
| 54 print_pthread_error(status); | 56 print_pthread_error(status); |
| 55 SkASSERT(0 == status); | 57 SkASSERT(0 == status); |
| 56 } | 58 } |
| 57 ) | 59 ) |
| 58 } | 60 } |
| 59 | 61 |
| 60 private: | 62 private: |
| 61 SkMutex(const SkMutex&); | 63 SkMutex(const SkMutex&); |
| 62 SkMutex& operator=(const SkMutex&); | 64 SkMutex& operator=(const SkMutex&); |
| 63 | 65 |
| 64 static void print_pthread_error(int status) { | 66 static void print_pthread_error(int status) { |
| 65 switch (status) { | 67 switch (status) { |
| 66 case 0: // success | 68 case 0: // success |
| 67 break; | 69 break; |
| 68 case EINVAL: | 70 case EINVAL: |
| 69 SkDebugf("pthread error [%d] EINVAL\n", status); | 71 SkDebugf("pthread error [%d] EINVAL\n", status); |
| 70 break; | 72 break; |
| 71 case EBUSY: | 73 case EBUSY: |
| 72 SkDebugf("pthread error [%d] EBUSY\n", status); | 74 SkDebugf("pthread error [%d] EBUSY\n", status); |
| 73 break; | 75 break; |
| 74 default: | 76 default: |
| 75 SkDebugf("pthread error [%d] unknown\n", status); | 77 SkDebugf("pthread error [%d] unknown\n", status); |
| 76 break; | 78 break; |
| 77 } | 79 } |
| 78 } | 80 } |
| 79 }; | 81 }; |
| 80 | 82 |
| 83 #define SK_BASE_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER, SkDEBUGCODE(0) } |
| 84 |
| 81 // Using POD-style initialization prevents the generation of a static initialize
r. | 85 // Using POD-style initialization prevents the generation of a static initialize
r. |
| 82 #define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = { PTHREAD_MUTEX_
INITIALIZER } | 86 #define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = SK_BASE_MUTEX_IN
IT |
| 83 | 87 |
| 84 // Special case used when the static mutex must be available globally. | 88 // Special case used when the static mutex must be available globally. |
| 85 #define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = { PTHREAD_MUTEX_INITIAL
IZER } | 89 #define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = SK_BASE_MUTEX_INIT |
| 86 | 90 |
| 87 #endif | 91 #endif |
| OLD | NEW |