]> code.delx.au - pulseaudio/blob - src/pulse/operation.c
volume: Increase PA_SW_VOLUME_SNPRINT_DB_MAX
[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.1 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 #include <pulse/fork-detect.h>
30
31 #include "internal.h"
32 #include "operation.h"
33
34 PA_STATIC_FLIST_DECLARE(operations, 0, pa_xfree);
35
36 pa_operation *pa_operation_new(pa_context *c, pa_stream *s, pa_operation_cb_t cb, void *userdata) {
37 pa_operation *o;
38 pa_assert(c);
39
40 if (!(o = pa_flist_pop(PA_STATIC_FLIST_GET(operations))))
41 o = pa_xnew(pa_operation, 1);
42
43 pa_zero(*o);
44
45 PA_REFCNT_INIT(o);
46 o->context = c;
47 o->stream = s;
48
49 o->state = PA_OPERATION_RUNNING;
50 o->callback = cb;
51 o->userdata = userdata;
52
53 /* Refcounting is strictly one-way: from the "bigger" to the "smaller" object. */
54 PA_LLIST_PREPEND(pa_operation, c->operations, o);
55 pa_operation_ref(o);
56
57 return o;
58 }
59
60 pa_operation *pa_operation_ref(pa_operation *o) {
61 pa_assert(o);
62 pa_assert(PA_REFCNT_VALUE(o) >= 1);
63
64 PA_REFCNT_INC(o);
65 return o;
66 }
67
68 void pa_operation_unref(pa_operation *o) {
69 pa_assert(o);
70 pa_assert(PA_REFCNT_VALUE(o) >= 1);
71
72 if (PA_REFCNT_DEC(o) <= 0) {
73 pa_assert(!o->context);
74 pa_assert(!o->stream);
75
76 if (pa_flist_push(PA_STATIC_FLIST_GET(operations), o) < 0)
77 pa_xfree(o);
78 }
79 }
80
81 static void operation_unlink(pa_operation *o) {
82 pa_assert(o);
83
84 if (o->context) {
85 pa_assert(PA_REFCNT_VALUE(o) >= 2);
86
87 PA_LLIST_REMOVE(pa_operation, o->context->operations, o);
88 pa_operation_unref(o);
89
90 o->context = NULL;
91 }
92
93 o->stream = NULL;
94 o->callback = NULL;
95 o->userdata = NULL;
96 o->state_callback = NULL;
97 o->state_userdata = NULL;
98 }
99
100 static void operation_set_state(pa_operation *o, pa_operation_state_t st) {
101 pa_assert(o);
102 pa_assert(PA_REFCNT_VALUE(o) >= 1);
103
104 if (st == o->state)
105 return;
106
107 pa_operation_ref(o);
108
109 o->state = st;
110
111 if (o->state_callback)
112 o->state_callback(o, o->state_userdata);
113
114 if ((o->state == PA_OPERATION_DONE) || (o->state == PA_OPERATION_CANCELED))
115 operation_unlink(o);
116
117 pa_operation_unref(o);
118 }
119
120 void pa_operation_cancel(pa_operation *o) {
121 pa_assert(o);
122 pa_assert(PA_REFCNT_VALUE(o) >= 1);
123
124 operation_set_state(o, PA_OPERATION_CANCELED);
125 }
126
127 void pa_operation_done(pa_operation *o) {
128 pa_assert(o);
129 pa_assert(PA_REFCNT_VALUE(o) >= 1);
130
131 operation_set_state(o, PA_OPERATION_DONE);
132 }
133
134 pa_operation_state_t pa_operation_get_state(pa_operation *o) {
135 pa_assert(o);
136 pa_assert(PA_REFCNT_VALUE(o) >= 1);
137
138 return o->state;
139 }
140
141 void pa_operation_set_state_callback(pa_operation *o, pa_operation_notify_cb_t cb, void *userdata) {
142 pa_assert(o);
143 pa_assert(PA_REFCNT_VALUE(o) >= 1);
144
145 if (pa_detect_fork())
146 return;
147
148 if (o->state == PA_OPERATION_DONE || o->state == PA_OPERATION_CANCELED)
149 return;
150
151 o->state_callback = cb;
152 o->state_userdata = userdata;
153 }