epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkThread.h" |
| 9 | |
| 10 | #include <pthread.h> |
| 11 | #include <errno.h> |
| 12 | |
digit@google.com | f66436c | 2012-01-11 17:44:41 +0000 | [diff] [blame] | 13 | #ifndef SK_BUILD_FOR_ANDROID |
| 14 | |
bsalomon@google.com | 0986600 | 2011-08-18 20:31:35 +0000 | [diff] [blame] | 15 | /** |
| 16 | We prefer the GCC intrinsic implementation of the atomic operations over the |
| 17 | SkMutex-based implementation. The SkMutex version suffers from static |
| 18 | destructor ordering problems. |
| 19 | Note clang also defines the GCC version macros and implements the intrinsics. |
| 20 | TODO: Verify that gcc-style __sync_* intrinsics work on ARM |
| 21 | According to this the intrinsics are supported on ARM in LLVM 2.7+ |
| 22 | http://llvm.org/releases/2.7/docs/ReleaseNotes.html |
| 23 | */ |
| 24 | #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || __GNUC__ > 4 |
| 25 | #if (defined(__x86_64) || defined(__i386__)) |
| 26 | #define GCC_INTRINSIC |
| 27 | #endif |
| 28 | #endif |
| 29 | |
| 30 | #if defined(GCC_INTRINSIC) |
| 31 | |
| 32 | int32_t sk_atomic_inc(int32_t* addr) |
| 33 | { |
| 34 | return __sync_fetch_and_add(addr, 1); |
| 35 | } |
| 36 | |
| 37 | int32_t sk_atomic_dec(int32_t* addr) |
| 38 | { |
| 39 | return __sync_fetch_and_add(addr, -1); |
| 40 | } |
| 41 | |
| 42 | #else |
| 43 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | SkMutex gAtomicMutex; |
| 45 | |
| 46 | int32_t sk_atomic_inc(int32_t* addr) |
| 47 | { |
| 48 | SkAutoMutexAcquire ac(gAtomicMutex); |
| 49 | |
| 50 | int32_t value = *addr; |
| 51 | *addr = value + 1; |
| 52 | return value; |
| 53 | } |
| 54 | |
| 55 | int32_t sk_atomic_dec(int32_t* addr) |
| 56 | { |
| 57 | SkAutoMutexAcquire ac(gAtomicMutex); |
| 58 | |
| 59 | int32_t value = *addr; |
| 60 | *addr = value - 1; |
| 61 | return value; |
| 62 | } |
| 63 | |
bsalomon@google.com | 0986600 | 2011-08-18 20:31:35 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
digit@google.com | f66436c | 2012-01-11 17:44:41 +0000 | [diff] [blame] | 66 | #endif // SK_BUILD_FOR_ANDROID |
| 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | ////////////////////////////////////////////////////////////////////////////// |
| 69 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 70 | static void print_pthread_error(int status) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | switch (status) { |
| 72 | case 0: // success |
| 73 | break; |
| 74 | case EINVAL: |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 75 | SkDebugf("pthread error [%d] EINVAL\n", status); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | break; |
| 77 | case EBUSY: |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 78 | SkDebugf("pthread error [%d] EBUSY\n", status); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | break; |
| 80 | default: |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 81 | SkDebugf("pthread error [%d] unknown\n", status); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 86 | SkMutex::SkMutex() { |
| 87 | if (sizeof(pthread_mutex_t) > sizeof(fStorage)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | SkDEBUGF(("pthread mutex size = %d\n", sizeof(pthread_mutex_t))); |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 89 | SkDEBUGFAIL("mutex storage is too small"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int status; |
| 93 | pthread_mutexattr_t attr; |
| 94 | |
| 95 | status = pthread_mutexattr_init(&attr); |
| 96 | print_pthread_error(status); |
| 97 | SkASSERT(0 == status); |
| 98 | |
| 99 | status = pthread_mutex_init((pthread_mutex_t*)fStorage, &attr); |
| 100 | print_pthread_error(status); |
| 101 | SkASSERT(0 == status); |
| 102 | } |
| 103 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 104 | SkMutex::~SkMutex() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | int status = pthread_mutex_destroy((pthread_mutex_t*)fStorage); |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 106 | #if 0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | // only report errors on non-global mutexes |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 108 | if (!fIsGlobal) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | print_pthread_error(status); |
| 110 | SkASSERT(0 == status); |
| 111 | } |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 112 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 115 | void SkMutex::acquire() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | int status = pthread_mutex_lock((pthread_mutex_t*)fStorage); |
| 117 | print_pthread_error(status); |
| 118 | SkASSERT(0 == status); |
| 119 | } |
| 120 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame^] | 121 | void SkMutex::release() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | int status = pthread_mutex_unlock((pthread_mutex_t*)fStorage); |
| 123 | print_pthread_error(status); |
| 124 | SkASSERT(0 == status); |
| 125 | } |
| 126 | |