root / audio / audio_pt_int.c @ 16415335
History | View | Annotate | Download (3.2 kB)
1 | ca9cc28c | balrog | #include "qemu-common.h" |
---|---|---|---|
2 | ca9cc28c | balrog | #include "audio.h" |
3 | ca9cc28c | balrog | |
4 | ca9cc28c | balrog | #define AUDIO_CAP "audio-pt" |
5 | ca9cc28c | balrog | |
6 | ca9cc28c | balrog | #include "audio_int.h" |
7 | ca9cc28c | balrog | #include "audio_pt_int.h" |
8 | ca9cc28c | balrog | |
9 | ca9cc28c | balrog | static void logerr (struct audio_pt *pt, int err, const char *fmt, ...) |
10 | ca9cc28c | balrog | { |
11 | ca9cc28c | balrog | va_list ap; |
12 | ca9cc28c | balrog | |
13 | ca9cc28c | balrog | va_start (ap, fmt); |
14 | ca9cc28c | balrog | AUD_vlog (pt->drv, fmt, ap); |
15 | ca9cc28c | balrog | va_end (ap); |
16 | ca9cc28c | balrog | |
17 | ca9cc28c | balrog | AUD_log (NULL, "\n"); |
18 | ca9cc28c | balrog | AUD_log (pt->drv, "Reason: %s\n", strerror (err));
|
19 | ca9cc28c | balrog | } |
20 | ca9cc28c | balrog | |
21 | ca9cc28c | balrog | int audio_pt_init (struct audio_pt *p, void *(*func) (void *), |
22 | ca9cc28c | balrog | void *opaque, const char *drv, const char *cap) |
23 | ca9cc28c | balrog | { |
24 | ca9cc28c | balrog | int err, err2;
|
25 | ca9cc28c | balrog | const char *efunc; |
26 | ca9cc28c | balrog | |
27 | ca9cc28c | balrog | p->drv = drv; |
28 | ca9cc28c | balrog | |
29 | ca9cc28c | balrog | err = pthread_mutex_init (&p->mutex, NULL);
|
30 | ca9cc28c | balrog | if (err) {
|
31 | ca9cc28c | balrog | efunc = "pthread_mutex_init";
|
32 | ca9cc28c | balrog | goto err0;
|
33 | ca9cc28c | balrog | } |
34 | ca9cc28c | balrog | |
35 | ca9cc28c | balrog | err = pthread_cond_init (&p->cond, NULL);
|
36 | ca9cc28c | balrog | if (err) {
|
37 | ca9cc28c | balrog | efunc = "pthread_cond_init";
|
38 | ca9cc28c | balrog | goto err1;
|
39 | ca9cc28c | balrog | } |
40 | ca9cc28c | balrog | |
41 | ca9cc28c | balrog | err = pthread_create (&p->thread, NULL, func, opaque);
|
42 | ca9cc28c | balrog | if (err) {
|
43 | ca9cc28c | balrog | efunc = "pthread_create";
|
44 | ca9cc28c | balrog | goto err2;
|
45 | ca9cc28c | balrog | } |
46 | ca9cc28c | balrog | |
47 | ca9cc28c | balrog | return 0; |
48 | ca9cc28c | balrog | |
49 | ca9cc28c | balrog | err2:
|
50 | ca9cc28c | balrog | err2 = pthread_cond_destroy (&p->cond); |
51 | ca9cc28c | balrog | if (err2) {
|
52 | ca9cc28c | balrog | logerr (p, err2, "%s(%s): pthread_cond_destroy failed", cap, AUDIO_FUNC);
|
53 | ca9cc28c | balrog | } |
54 | ca9cc28c | balrog | |
55 | ca9cc28c | balrog | err1:
|
56 | ca9cc28c | balrog | err2 = pthread_mutex_destroy (&p->mutex); |
57 | ca9cc28c | balrog | if (err2) {
|
58 | ca9cc28c | balrog | logerr (p, err2, "%s(%s): pthread_mutex_destroy failed", cap, AUDIO_FUNC);
|
59 | ca9cc28c | balrog | } |
60 | ca9cc28c | balrog | |
61 | ca9cc28c | balrog | err0:
|
62 | ca9cc28c | balrog | logerr (p, err, "%s(%s): %s failed", cap, AUDIO_FUNC, efunc);
|
63 | ca9cc28c | balrog | return -1; |
64 | ca9cc28c | balrog | } |
65 | ca9cc28c | balrog | |
66 | ca9cc28c | balrog | int audio_pt_fini (struct audio_pt *p, const char *cap) |
67 | ca9cc28c | balrog | { |
68 | ca9cc28c | balrog | int err, ret = 0; |
69 | ca9cc28c | balrog | |
70 | ca9cc28c | balrog | err = pthread_cond_destroy (&p->cond); |
71 | ca9cc28c | balrog | if (err) {
|
72 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_cond_destroy failed", cap, AUDIO_FUNC);
|
73 | ca9cc28c | balrog | ret = -1;
|
74 | ca9cc28c | balrog | } |
75 | ca9cc28c | balrog | |
76 | ca9cc28c | balrog | err = pthread_mutex_destroy (&p->mutex); |
77 | ca9cc28c | balrog | if (err) {
|
78 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_mutex_destroy failed", cap, AUDIO_FUNC);
|
79 | ca9cc28c | balrog | ret = -1;
|
80 | ca9cc28c | balrog | } |
81 | ca9cc28c | balrog | return ret;
|
82 | ca9cc28c | balrog | } |
83 | ca9cc28c | balrog | |
84 | ca9cc28c | balrog | int audio_pt_lock (struct audio_pt *p, const char *cap) |
85 | ca9cc28c | balrog | { |
86 | ca9cc28c | balrog | int err;
|
87 | ca9cc28c | balrog | |
88 | ca9cc28c | balrog | err = pthread_mutex_lock (&p->mutex); |
89 | ca9cc28c | balrog | if (err) {
|
90 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_mutex_lock failed", cap, AUDIO_FUNC);
|
91 | ca9cc28c | balrog | return -1; |
92 | ca9cc28c | balrog | } |
93 | ca9cc28c | balrog | return 0; |
94 | ca9cc28c | balrog | } |
95 | ca9cc28c | balrog | |
96 | ca9cc28c | balrog | int audio_pt_unlock (struct audio_pt *p, const char *cap) |
97 | ca9cc28c | balrog | { |
98 | ca9cc28c | balrog | int err;
|
99 | ca9cc28c | balrog | |
100 | ca9cc28c | balrog | err = pthread_mutex_unlock (&p->mutex); |
101 | ca9cc28c | balrog | if (err) {
|
102 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_mutex_unlock failed", cap, AUDIO_FUNC);
|
103 | ca9cc28c | balrog | return -1; |
104 | ca9cc28c | balrog | } |
105 | ca9cc28c | balrog | return 0; |
106 | ca9cc28c | balrog | } |
107 | ca9cc28c | balrog | |
108 | ca9cc28c | balrog | int audio_pt_wait (struct audio_pt *p, const char *cap) |
109 | ca9cc28c | balrog | { |
110 | ca9cc28c | balrog | int err;
|
111 | ca9cc28c | balrog | |
112 | ca9cc28c | balrog | err = pthread_cond_wait (&p->cond, &p->mutex); |
113 | ca9cc28c | balrog | if (err) {
|
114 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_cond_wait failed", cap, AUDIO_FUNC);
|
115 | ca9cc28c | balrog | return -1; |
116 | ca9cc28c | balrog | } |
117 | ca9cc28c | balrog | return 0; |
118 | ca9cc28c | balrog | } |
119 | ca9cc28c | balrog | |
120 | ca9cc28c | balrog | int audio_pt_unlock_and_signal (struct audio_pt *p, const char *cap) |
121 | ca9cc28c | balrog | { |
122 | ca9cc28c | balrog | int err;
|
123 | ca9cc28c | balrog | |
124 | ca9cc28c | balrog | err = pthread_mutex_unlock (&p->mutex); |
125 | ca9cc28c | balrog | if (err) {
|
126 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_mutex_unlock failed", cap, AUDIO_FUNC);
|
127 | ca9cc28c | balrog | return -1; |
128 | ca9cc28c | balrog | } |
129 | ca9cc28c | balrog | err = pthread_cond_signal (&p->cond); |
130 | ca9cc28c | balrog | if (err) {
|
131 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_cond_signal failed", cap, AUDIO_FUNC);
|
132 | ca9cc28c | balrog | return -1; |
133 | ca9cc28c | balrog | } |
134 | ca9cc28c | balrog | return 0; |
135 | ca9cc28c | balrog | } |
136 | ca9cc28c | balrog | |
137 | ca9cc28c | balrog | int audio_pt_join (struct audio_pt *p, void **arg, const char *cap) |
138 | ca9cc28c | balrog | { |
139 | ca9cc28c | balrog | int err;
|
140 | ca9cc28c | balrog | void *ret;
|
141 | ca9cc28c | balrog | |
142 | ca9cc28c | balrog | err = pthread_join (p->thread, &ret); |
143 | ca9cc28c | balrog | if (err) {
|
144 | ca9cc28c | balrog | logerr (p, err, "%s(%s): pthread_join failed", cap, AUDIO_FUNC);
|
145 | ca9cc28c | balrog | return -1; |
146 | ca9cc28c | balrog | } |
147 | ca9cc28c | balrog | *arg = ret; |
148 | ca9cc28c | balrog | return 0; |
149 | ca9cc28c | balrog | } |