]> code.delx.au - pulseaudio/blob - src/pulse/operation.c
Merge dead branch 'glitch-free'
[pulseaudio] / src / pulse / operation.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27 #include <pulsecore/macro.h>
28 #include <pulsecore/flist.h>
29
30 #include "internal.h"
31 #include "operation.h"
32
33 PA_STATIC_FLIST_DECLARE(operations, 0, pa_xfree);
34
35 pa_operation *pa_operation_new(pa_context *c, pa_stream *s, pa_operation_cb_t cb, void *userdata) {
36 pa_operation *o;
37 pa_assert(c);
38
39 if (!(o = pa_flist_pop(PA_STATIC_FLIST_GET(operations))))
40 o = pa_xnew(pa_operation, 1);
41
42 PA_REFCNT_INIT(o);
43 o->context = c;
44 o->stream = s;
45 o->private = NULL;
46
47 o->state = PA_OPERATION_RUNNING;
48 o->callback = cb;
49 o->userdata = userdata;
50
51 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
52 PA_LLIST_PREPEND(pa_operation, c->operations, o);
53 pa_operation_ref(o);
54
55 return o;
56 }
57
58 pa_operation *pa_operation_ref(pa_operation *o) {
59 pa_assert(o);
60 pa_assert(PA_REFCNT_VALUE(o) >= 1);
61
62 PA_REFCNT_INC(o);
63 return o;
64 }
65 void pa_operation_unref(pa_operation *o) {
66 pa_assert(o);
67 pa_assert(PA_REFCNT_VALUE(o) >= 1);
68
69 if (PA_REFCNT_DEC(o) <= 0) {
70 pa_assert(!o->context);
71 pa_assert(!o->stream);
72
73 if (pa_flist_push(PA_STATIC_FLIST_GET(operations), o) < 0)
74 pa_xfree(o);
75 }
76 }
77
78 static void operation_unlink(pa_operation *o) {
79 pa_assert(o);
80
81 if (o->context) {
82 pa_assert(PA_REFCNT_VALUE(o) >= 2);
83
84 PA_LLIST_REMOVE(pa_operation, o->context->operations, o);
85 pa_operation_unref(o);
86
87 o->context = NULL;
88 }
89
90 o->stream = NULL;
91 o->callback = NULL;
92 o->userdata = NULL;
93 }
94
95 static void operation_set_state(pa_operation *o, pa_operation_state_t st) {
96 pa_assert(o);
97 pa_assert(PA_REFCNT_VALUE(o) >= 1);
98
99 if (st == o->state)
100 return;
101
102 pa_operation_ref(o);
103
104 o->state = st;
105
106 if ((o->state == PA_OPERATION_DONE) || (o->state == PA_OPERATION_CANCELED))
107 operation_unlink(o);
108
109 pa_operation_unref(o);
110 }
111
112 void pa_operation_cancel(pa_operation *o) {
113 pa_assert(o);
114 pa_assert(PA_REFCNT_VALUE(o) >= 1);
115
116 operation_set_state(o, PA_OPERATION_CANCELED);
117 }
118
119 void pa_operation_done(pa_operation *o) {
120 pa_assert(o);
121 pa_assert(PA_REFCNT_VALUE(o) >= 1);
122
123 operation_set_state(o, PA_OPERATION_DONE);
124 }
125
126 pa_operation_state_t pa_operation_get_state(pa_operation *o) {
127 pa_assert(o);
128 pa_assert(PA_REFCNT_VALUE(o) >= 1);
129
130 return o->state;
131 }