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