blob: 4ee857d51c3a857458c3707643ea7677b287e0f1 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SkThread.h"
2
3#include <pthread.h>
4#include <errno.h>
5
6SkMutex gAtomicMutex;
7
8int32_t sk_atomic_inc(int32_t* addr)
9{
10 SkAutoMutexAcquire ac(gAtomicMutex);
11
12 int32_t value = *addr;
13 *addr = value + 1;
14 return value;
15}
16
17int32_t sk_atomic_dec(int32_t* addr)
18{
19 SkAutoMutexAcquire ac(gAtomicMutex);
20
21 int32_t value = *addr;
22 *addr = value - 1;
23 return value;
24}
25
26//////////////////////////////////////////////////////////////////////////////
27
28static void print_pthread_error(int status)
29{
30 switch (status) {
31 case 0: // success
32 break;
33 case EINVAL:
34 printf("pthread error [%d] EINVAL\n", status);
35 break;
36 case EBUSY:
37 printf("pthread error [%d] EBUSY\n", status);
38 break;
39 default:
40 printf("pthread error [%d] unknown\n", status);
41 break;
42 }
43}
44
45SkMutex::SkMutex(bool isGlobal) : fIsGlobal(isGlobal)
46{
47 if (sizeof(pthread_mutex_t) > sizeof(fStorage))
48 {
49 SkDEBUGF(("pthread mutex size = %d\n", sizeof(pthread_mutex_t)));
50 SkASSERT(!"mutex storage is too small");
51 }
52
53 int status;
54 pthread_mutexattr_t attr;
55
56 status = pthread_mutexattr_init(&attr);
57 print_pthread_error(status);
58 SkASSERT(0 == status);
59
60 status = pthread_mutex_init((pthread_mutex_t*)fStorage, &attr);
61 print_pthread_error(status);
62 SkASSERT(0 == status);
63}
64
65SkMutex::~SkMutex()
66{
67 int status = pthread_mutex_destroy((pthread_mutex_t*)fStorage);
68
69 // only report errors on non-global mutexes
70 if (!fIsGlobal)
71 {
72 print_pthread_error(status);
73 SkASSERT(0 == status);
74 }
75}
76
77void SkMutex::acquire()
78{
79 int status = pthread_mutex_lock((pthread_mutex_t*)fStorage);
80 print_pthread_error(status);
81 SkASSERT(0 == status);
82}
83
84void SkMutex::release()
85{
86 int status = pthread_mutex_unlock((pthread_mutex_t*)fStorage);
87 print_pthread_error(status);
88 SkASSERT(0 == status);
89}
90