]> code.delx.au - pulseaudio/blob - src/tests/asyncq-test.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / tests / asyncq-test.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 <assert.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 #include <pulse/util.h>
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/asyncq.h>
33 #include <pulsecore/thread.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/macro.h>
37
38 static void producer(void *_q) {
39 pa_asyncq *q = _q;
40 int i;
41
42 for (i = 0; i < 1000; i++) {
43 printf("pushing %i\n", i);
44 pa_asyncq_push(q, PA_UINT_TO_PTR(i+1), 1);
45 }
46
47 pa_asyncq_push(q, PA_UINT_TO_PTR(-1), 1);
48 printf("pushed end\n");
49 }
50
51 static void consumer(void *_q) {
52 pa_asyncq *q = _q;
53 void *p;
54 int i;
55
56 sleep(1);
57
58 for (i = 0;; i++) {
59 p = pa_asyncq_pop(q, 1);
60
61 if (p == PA_UINT_TO_PTR(-1))
62 break;
63
64 pa_assert(p == PA_UINT_TO_PTR(i+1));
65
66 printf("popped %i\n", i);
67 }
68
69 printf("popped end\n");
70 }
71
72 int main(int argc, char *argv[]) {
73 pa_asyncq *q;
74 pa_thread *t1, *t2;
75
76 pa_assert_se(q = pa_asyncq_new(0));
77
78 pa_assert_se(t1 = pa_thread_new(producer, q));
79 pa_assert_se(t2 = pa_thread_new(consumer, q));
80
81 pa_thread_free(t1);
82 pa_thread_free(t2);
83
84 pa_asyncq_free(q, NULL);
85
86 return 0;
87 }