Git init
[framework/multimedia/pulseaudio.git] / src / tests / lock-autospawn-test.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2008 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 <sys/poll.h>
27 #include <string.h>
28
29 #include <pulsecore/macro.h>
30 #include <pulsecore/thread.h>
31 #include <pulsecore/lock-autospawn.h>
32 #include <pulse/util.h>
33
34 static void thread_func(void*k) {
35     pa_assert_se(pa_autospawn_lock_init() >= 0);
36
37     pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
38
39     pa_assert_se(pa_autospawn_lock_acquire(TRUE) > 0);
40
41     pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
42
43     pa_msleep(5000);
44
45     pa_log("%i, Releasing", PA_PTR_TO_INT(k));
46
47     pa_autospawn_lock_release();
48
49     pa_autospawn_lock_done(FALSE);
50 }
51
52 static void thread_func2(void *k) {
53     int fd;
54
55     pa_assert_se((fd = pa_autospawn_lock_init()) >= 0);
56
57     pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
58
59     for (;;) {
60         struct pollfd pollfd;
61         int j;
62
63         if ((j = pa_autospawn_lock_acquire(FALSE)) > 0)
64             break;
65
66         pa_assert(j == 0);
67
68         memset(&pollfd, 0, sizeof(pollfd));
69         pollfd.fd = fd;
70         pollfd.events = POLLIN;
71
72         pa_assert_se(poll(&pollfd, 1, -1) == 1);
73
74         pa_log("%i, woke up", PA_PTR_TO_INT(k));
75     }
76
77     pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
78
79     pa_msleep(5000);
80
81     pa_log("%i, Releasing", PA_PTR_TO_INT(k));
82
83     pa_autospawn_lock_release();
84
85     pa_autospawn_lock_done(FALSE);
86 }
87
88 int main(int argc, char**argv) {
89     pa_thread *a, *b, *c, *d;
90
91     pa_assert_se((a = pa_thread_new(thread_func, PA_INT_TO_PTR(1))));
92     pa_assert_se((b = pa_thread_new(thread_func2, PA_INT_TO_PTR(2))));
93     pa_assert_se((c = pa_thread_new(thread_func2, PA_INT_TO_PTR(3))));
94     pa_assert_se((d = pa_thread_new(thread_func, PA_INT_TO_PTR(4))));
95
96     pa_thread_join(a);
97     pa_thread_join(b);
98     pa_thread_join(c);
99     pa_thread_join(d);
100
101     pa_thread_free(a);
102     pa_thread_free(b);
103     pa_thread_free(c);
104     pa_thread_free(d);
105
106     pa_log("End");
107
108     return 0;
109 }