aboutsummaryrefslogtreecommitdiffstats
path: root/audio/audio_pt_int.c
blob: e889a983b1f91b89b86bf25ac8af891bc16eb30b (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "qemu-common.h"
#include "audio.h"

#define AUDIO_CAP "audio-pt"

#include "audio_int.h"
#include "audio_pt_int.h"

static void logerr (struct audio_pt *pt, int err, const char *fmt, ...)
{
    va_list ap;

    va_start (ap, fmt);
    AUD_vlog (pt->drv, fmt, ap);
    va_end (ap);

    AUD_log (NULL, "\n");
    AUD_log (pt->drv, "Reason: %s\n", strerror (err));
}

int audio_pt_init (struct audio_pt *p, void *(*func) (void *),
                   void *opaque, const char *drv, const char *cap)
{
    int err, err2;
    const char *efunc;

    p->drv = drv;

    err = pthread_mutex_init (&p->mutex, NULL);
    if (err) {
        efunc = "pthread_mutex_init";
        goto err0;
    }

    err = pthread_cond_init (&p->cond, NULL);
    if (err) {
        efunc = "pthread_cond_init";
        goto err1;
    }

    err = pthread_create (&p->thread, NULL, func, opaque);
    if (err) {
        efunc = "pthread_create";
        goto err2;
    }

    return 0;

 err2:
    err2 = pthread_cond_destroy (&p->cond);
    if (err2) {
        logerr (p, err2, "%s(%s): pthread_cond_destroy failed", cap, AUDIO_FUNC);
    }

 err1:
    err2 = pthread_mutex_destroy (&p->mutex);
    if (err2) {
        logerr (p, err2, "%s(%s): pthread_mutex_destroy failed", cap, AUDIO_FUNC);
    }

 err0:
    logerr (p, err, "%s(%s): %s failed", cap, AUDIO_FUNC, efunc);
    return -1;
}

int audio_pt_fini (struct audio_pt *p, const char *cap)
{
    int err, ret = 0;

    err = pthread_cond_destroy (&p->cond);
    if (err) {
        logerr (p, err, "%s(%s): pthread_cond_destroy failed", cap, AUDIO_FUNC);
        ret = -1;
    }

    err = pthread_mutex_destroy (&p->mutex);
    if (err) {
        logerr (p, err, "%s(%s): pthread_mutex_destroy failed", cap, AUDIO_FUNC);
        ret = -1;
    }
    return ret;
}

int audio_pt_lock (struct audio_pt *p, const char *cap)
{
    int err;

    err = pthread_mutex_lock (&p->mutex);
    if (err) {
        logerr (p, err, "%s(%s): pthread_mutex_lock failed", cap, AUDIO_FUNC);
        return -1;
    }
    return 0;
}

int audio_pt_unlock (struct audio_pt *p, const char *cap)
{
    int err;

    err = pthread_mutex_unlock (&p->mutex);
    if (err) {
        logerr (p, err, "%s(%s): pthread_mutex_unlock failed", cap, AUDIO_FUNC);
        return -1;
    }
    return 0;
}

int audio_pt_wait (struct audio_pt *p, const char *cap)
{
    int err;

    err = pthread_cond_wait (&p->cond, &p->mutex);
    if (err) {
        logerr (p, err, "%s(%s): pthread_cond_wait failed", cap, AUDIO_FUNC);
        return -1;
    }
    return 0;
}

int audio_pt_unlock_and_signal (struct audio_pt *p, const char *cap)
{
    int err;

    err = pthread_mutex_unlock (&p->mutex);
    if (err) {
        logerr (p, err, "%s(%s): pthread_mutex_unlock failed", cap, AUDIO_FUNC);
        return -1;
    }
    err = pthread_cond_signal (&p->cond);
    if (err) {
        logerr (p, err, "%s(%s): pthread_cond_signal failed", cap, AUDIO_FUNC);
        return -1;
    }
    return 0;
}

int audio_pt_join (struct audio_pt *p, void **arg, const char *cap)
{
    int err;
    void *ret;

    err = pthread_join (p->thread, &ret);
    if (err) {
        logerr (p, err, "%s(%s): pthread_join failed", cap, AUDIO_FUNC);
        return -1;
    }
    *arg = ret;
    return 0;
}