2 This file is part of PulseAudio.
4 Copyright 2008 Lennart Poettering
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.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
28 #include <pulsecore/poll.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/thread.h>
31 #include <pulsecore/lock-autospawn.h>
32 #include <pulse/util.h>
34 static void thread_func(void*k) {
35 fail_unless(pa_autospawn_lock_init() >= 0);
37 pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
39 fail_unless(pa_autospawn_lock_acquire(true) > 0);
41 pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
45 pa_log("%i, Releasing", PA_PTR_TO_INT(k));
47 pa_autospawn_lock_release();
49 pa_autospawn_lock_done(false);
52 static void thread_func2(void *k) {
55 fail_unless((fd = pa_autospawn_lock_init()) >= 0);
57 pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k));
63 if ((j = pa_autospawn_lock_acquire(false)) > 0)
68 memset(&pollfd, 0, sizeof(pollfd));
70 pollfd.events = POLLIN;
72 fail_unless(pa_poll(&pollfd, 1, -1) == 1);
74 pa_log("%i, woke up", PA_PTR_TO_INT(k));
77 pa_log("%i, Got the lock!, Sleeping for 5s", PA_PTR_TO_INT(k));
81 pa_log("%i, Releasing", PA_PTR_TO_INT(k));
83 pa_autospawn_lock_release();
85 pa_autospawn_lock_done(false);
88 START_TEST (lockautospawn_test) {
89 pa_thread *a, *b, *c, *d;
91 pa_assert_se((a = pa_thread_new("test1", thread_func, PA_INT_TO_PTR(1))));
92 pa_assert_se((b = pa_thread_new("test2", thread_func2, PA_INT_TO_PTR(2))));
93 pa_assert_se((c = pa_thread_new("test3", thread_func2, PA_INT_TO_PTR(3))));
94 pa_assert_se((d = pa_thread_new("test4", thread_func, PA_INT_TO_PTR(4))));
108 int main(int argc, char *argv[]) {
114 s = suite_create("Lock Auto Spawn");
115 tc = tcase_create("lockautospawn");
116 tcase_add_test(tc, lockautospawn_test);
117 /* the default timeout is too small,
118 * set it to a reasonable large one.
120 tcase_set_timeout(tc, 60 * 60);
121 suite_add_tcase(s, tc);
123 sr = srunner_create(s);
124 srunner_run_all(sr, CK_NORMAL);
125 failed = srunner_ntests_failed(sr);
128 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;