Git init
[framework/multimedia/pulseaudio.git] / src / tests / flist-test.c
1 /***
2   This file is part of PulseAudio.
3
4   PulseAudio is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as published
6   by the Free Software Foundation; either version 2.1 of the License,
7   or (at your option) any later version.
8
9   PulseAudio is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with PulseAudio; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include <pulse/util.h>
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/flist.h>
31 #include <pulsecore/thread.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/core-util.h>
34
35 #define THREADS_MAX 20
36
37 static pa_flist *flist;
38 static int quit = 0;
39
40 static void spin(void) {
41     int k;
42
43     /* Spin a little */
44     k = rand() % 10000;
45     for (; k > 0; k--)
46         pa_thread_yield();
47 }
48
49 static void thread_func(void *data) {
50     char *s = data;
51     int n = 0;
52     int b = 1;
53
54     while (!quit) {
55         char *text;
56
57         /* Allocate some memory, if possible take it from the flist */
58         if (b && (text = pa_flist_pop(flist)))
59             pa_log("%s: popped '%s'", s, text);
60         else {
61             text = pa_sprintf_malloc("Block %i, allocated by %s", n++, s);
62             pa_log("%s: allocated '%s'", s, text);
63         }
64
65         b = !b;
66
67         spin();
68
69         /* Give it back to the flist if possible */
70         if (pa_flist_push(flist, text) < 0) {
71             pa_log("%s: failed to push back '%s'", s, text);
72             pa_xfree(text);
73         } else
74             pa_log("%s: pushed", s);
75
76         spin();
77     }
78
79     if (pa_flist_push(flist, s) < 0)
80         pa_xfree(s);
81 }
82
83 int main(int argc, char* argv[]) {
84     pa_thread *threads[THREADS_MAX];
85     int i;
86
87     flist = pa_flist_new(0);
88
89     for (i = 0; i < THREADS_MAX; i++) {
90         threads[i] = pa_thread_new(thread_func, pa_sprintf_malloc("Thread #%i", i+1));
91         assert(threads[i]);
92     }
93
94     pa_msleep(60000);
95     quit = 1;
96
97     for (i = 0; i < THREADS_MAX; i++)
98         pa_thread_free(threads[i]);
99
100     pa_flist_free(flist, pa_xfree);
101
102     return 0;
103 }