aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ports/SkThread_pthread.cpp
blob: 4ee857d51c3a857458c3707643ea7677b287e0f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "SkThread.h"

#include <pthread.h>
#include <errno.h>

SkMutex gAtomicMutex;

int32_t sk_atomic_inc(int32_t* addr)
{
    SkAutoMutexAcquire ac(gAtomicMutex);

    int32_t value = *addr;
    *addr = value + 1;
    return value;
}

int32_t sk_atomic_dec(int32_t* addr)
{
    SkAutoMutexAcquire ac(gAtomicMutex);
    
    int32_t value = *addr;
    *addr = value - 1;
    return value;
}

//////////////////////////////////////////////////////////////////////////////

static void print_pthread_error(int status)
{
    switch (status) {
    case 0: // success
        break;
    case EINVAL:
        printf("pthread error [%d] EINVAL\n", status);
        break;
    case EBUSY:
        printf("pthread error [%d] EBUSY\n", status);
        break;
    default:
        printf("pthread error [%d] unknown\n", status);
        break;
    }
}

SkMutex::SkMutex(bool isGlobal) : fIsGlobal(isGlobal)
{
    if (sizeof(pthread_mutex_t) > sizeof(fStorage))
    {
        SkDEBUGF(("pthread mutex size = %d\n", sizeof(pthread_mutex_t)));
        SkASSERT(!"mutex storage is too small");
    }

    int status;
    pthread_mutexattr_t attr;

    status = pthread_mutexattr_init(&attr);
    print_pthread_error(status);
    SkASSERT(0 == status);
    
    status = pthread_mutex_init((pthread_mutex_t*)fStorage, &attr);
    print_pthread_error(status);
    SkASSERT(0 == status);
}

SkMutex::~SkMutex()
{
    int status = pthread_mutex_destroy((pthread_mutex_t*)fStorage);
    
    // only report errors on non-global mutexes
    if (!fIsGlobal)
    {
        print_pthread_error(status);
        SkASSERT(0 == status);
    }
}

void SkMutex::acquire()
{
    int status = pthread_mutex_lock((pthread_mutex_t*)fStorage);
    print_pthread_error(status);
    SkASSERT(0 == status);
}

void SkMutex::release()
{
    int status = pthread_mutex_unlock((pthread_mutex_t*)fStorage);
    print_pthread_error(status);
    SkASSERT(0 == status);
}