]> code.delx.au - pulseaudio/blob - src/polyp/operation.c
Clarify behaviour of deferred events.
[pulseaudio] / src / polyp / operation.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 <assert.h>
27
28 #include <polypcore/xmalloc.h>
29
30 #include "internal.h"
31
32 #include "operation.h"
33
34 pa_operation *pa_operation_new(pa_context *c, pa_stream *s, pa_operation_cb_t cb, void *userdata) {
35 pa_operation *o;
36 assert(c);
37
38 o = pa_xmalloc(sizeof(pa_operation));
39 o->ref = 1;
40 o->context = pa_context_ref(c);
41 o->stream = s ? pa_stream_ref(s) : NULL;
42
43 o->state = PA_OPERATION_RUNNING;
44 o->callback = cb;
45 o->userdata = userdata;
46
47 PA_LLIST_PREPEND(pa_operation, o->context->operations, o);
48 return pa_operation_ref(o);
49 }
50
51 pa_operation *pa_operation_ref(pa_operation *o) {
52 assert(o && o->ref >= 1);
53 o->ref++;
54 return o;
55 }
56
57 void pa_operation_unref(pa_operation *o) {
58 assert(o && o->ref >= 1);
59
60 if ((--(o->ref)) == 0) {
61 assert(!o->context);
62 assert(!o->stream);
63 free(o);
64 }
65 }
66
67 static void operation_set_state(pa_operation *o, pa_operation_state_t st) {
68 assert(o && o->ref >= 1);
69
70 if (st == o->state)
71 return;
72
73 if (!o->context)
74 return;
75
76 o->state = st;
77
78 if ((o->state == PA_OPERATION_DONE) || (o->state == PA_OPERATION_CANCELED)) {
79 PA_LLIST_REMOVE(pa_operation, o->context->operations, o);
80 pa_context_unref(o->context);
81 if (o->stream)
82 pa_stream_unref(o->stream);
83 o->context = NULL;
84 o->stream = NULL;
85 o->callback = NULL;
86 o->userdata = NULL;
87
88 pa_operation_unref(o);
89 }
90 }
91
92 void pa_operation_cancel(pa_operation *o) {
93 assert(o && o->ref >= 1);
94 operation_set_state(o, PA_OPERATION_CANCELED);
95 }
96
97 void pa_operation_done(pa_operation *o) {
98 assert(o && o->ref >= 1);
99 operation_set_state(o, PA_OPERATION_DONE);
100 }
101
102 pa_operation_state_t pa_operation_get_state(pa_operation *o) {
103 assert(o && o->ref >= 1);
104 return o->state;
105 }