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, see <http://www.gnu.org/licenses/>. 18***/ 19 20#ifdef HAVE_CONFIG_H 21#include <config.h> 22#endif 23 24#include <check.h> 25 26#include <string.h> 27 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> 33 34static void thread_func(void*k) { 35 fail_unless(pa_autospawn_lock_init() >= 0); 36 37 pa_log("%i, Trying to acquire lock.", PA_PTR_TO_INT(k)); 38 39 fail_unless(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 52static void thread_func2(void *k) { 53 int fd; 54 55 fail_unless((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 fail_unless(j == 0); 67 68 memset(&pollfd, 0, sizeof(pollfd)); 69 pollfd.fd = fd; 70 pollfd.events = POLLIN; 71 72 fail_unless(pa_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 88START_TEST (lockautospawn_test) { 89 pa_thread *a, *b, *c, *d; 90 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)))); 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} 106END_TEST 107 108int main(int argc, char *argv[]) { 109 int failed = 0; 110 Suite *s; 111 TCase *tc; 112 SRunner *sr; 113 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. 119 */ 120 tcase_set_timeout(tc, 60 * 60); 121 suite_add_tcase(s, tc); 122 123 sr = srunner_create(s); 124 srunner_run_all(sr, CK_NORMAL); 125 failed = srunner_ntests_failed(sr); 126 srunner_free(sr); 127 128 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 129} 130