diff options
author | ngie <ngie@FreeBSD.org> | 2014-10-02 23:26:49 +0000 |
---|---|---|
committer | ngie <ngie@FreeBSD.org> | 2014-10-02 23:26:49 +0000 |
commit | 3f09b8d0af642c2aeb96a4d667cefb7fe3bce443 (patch) | |
tree | 544932e2a2c5a5a202b752beefba0b3e327b3858 /contrib/netbsd-tests/lib/libpthread | |
parent | b941fec92da62b0eab650295f4e8a381dbbc04b4 (diff) | |
parent | e1f2d32c0e0678782c353c48364cddedfae58b0a (diff) | |
download | FreeBSD-src-3f09b8d0af642c2aeb96a4d667cefb7fe3bce443.zip FreeBSD-src-3f09b8d0af642c2aeb96a4d667cefb7fe3bce443.tar.gz |
Import the NetBSD test suite from ^/vendor/NetBSD/tests/09.30.2014_20.45 ,
minus the vendor Makefiles
Provide directions for how to bootstrap the vendor sources in
FREEBSD-upgrade
MFC after 2 weeks
Discussed with: rpaulo
Sponsored by: EMC / Isilon Storage Division
Diffstat (limited to 'contrib/netbsd-tests/lib/libpthread')
35 files changed, 4749 insertions, 0 deletions
diff --git a/contrib/netbsd-tests/lib/libpthread/d_mach b/contrib/netbsd-tests/lib/libpthread/d_mach new file mode 100644 index 0000000..f3c3981 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/d_mach @@ -0,0 +1,92 @@ +above.warped.net +anoncvs.cirr.com +anoncvs.isc.netbsd.org +anoncvs.leo.org +anoncvs.netbsd.lt +anoncvs.netbsd.ro +anoncvs.netbsd.se +antioche.antioche.eu.org +boulder.tele.dk +centaurus.4web.cz +chur.math.ntnu.no +cnftp.bjpu.edu.cn +console.netbsd.org +cvs.fi.netbsd.org +cvs.mikrolahti.fi +cvs.netbsd.org +cvsup-netbsd.leo.org +cvsup.netbsd.se +cvsup.pasta.cs.uit.no +ftp.bitcon.no +ftp.chg.ru +ftp.duth.gr +ftp.estpak.ee +ftp.fsn.hu +ftp.funet.fi +ftp.grondar.za +ftp.leo.org +ftp.netbsd.lt +ftp.netbsd.org +ftp.nluug.nl +ftp.plig.org +ftp.uni-erlangen.de +ftp.xgate.co.kr +gd.tuwien.ac.at +gort.ludd.luth.se +grappa.unix-ag.uni-kl.de +info.wins.uva.nl +irc.warped.net +knug.youn.co.kr +lala.iri.co.jp +mail.jp.netbsd.org +mail.kr.netbsd.org +mail.netbsd.org +melanoma.cs.rmit.edu.au +mirror.aarnet.edu.au +mirror.netbsd.com.br +mirror03.inet.tele.dk +moon.vub.ac.be +nbwww.sergei.cc +net.bsd.cz +netbsd.3miasto.net +netbsd.4ka.mipt.ru +netbsd.apk.od.ua +netbsd.csie.nctu.edu.tw +netbsd.enderunix.org +netbsd.ftp.fu-berlin.de +netbsd.netlead.com.au +netbsd.nsysu.edu.tw +netbsd.pair.com +netbsd.stevens-tech.edu +netbsd.triada.bg +netbsd.unix.net.nz +netbsd.unixtech.be +netbsd.vejas.lt +netbsd.wagener-consulting.lu +netbsd.zarco.org +netbsdiso.interoute.net.uk +netbsdwww.bitcon.no +netbsdwww.cordef.com.pl +netbsdwww.cs.rmit.edu.au +netbsdwww.interoute.net.uk +news.gw.com +ns.netbsd.org +pigu.iri.co.jp +pluto.cdpa.nsysu.edu.tw +projects.slowass.net +server6.pasta.cs.uit.no +skeleton.phys.spbu.ru +snoopy.allbsd.org +spike.allbsd.org +sundry.netbsd.org +tanya.sergei.cc +web-a.fi.gw.com +web-a.us.gw.com +web.netbsd.mirror.arhea.net +www.en.netbsd.de +www.netbsd.cl +www.netbsd.nl +www.netbsd.org +www.netbsd.ro +zathras.netbsd.org +zeppo.rediris.es diff --git a/contrib/netbsd-tests/lib/libpthread/dlopen/dso/h_pthread_dlopen.c b/contrib/netbsd-tests/lib/libpthread/dlopen/dso/h_pthread_dlopen.c new file mode 100644 index 0000000..63bf6a3 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/dlopen/dso/h_pthread_dlopen.c @@ -0,0 +1,86 @@ +/* $NetBSD: h_pthread_dlopen.c,v 1.1 2013/03/21 16:50:22 christos Exp $ */ +/*- + * Copyright (c) 2013 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Emmanuel Dreyfus. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: h_pthread_dlopen.c,v 1.1 2013/03/21 16:50:22 christos Exp $"); + +#if 0 +#include <atf-c.h> +#else +#include <assert.h> +#define ATF_REQUIRE(a) assert(a) +#endif +#include <unistd.h> +#include <pthread.h> + +int testf_dso_null(void); +int testf_dso_mutex_lock(pthread_mutex_t *); +int testf_dso_mutex_unlock(pthread_mutex_t *); +int testf_dso_pthread_create(pthread_t *, const pthread_attr_t *, + void *(*)(void *), void *); + +int +testf_dso_null(void) +{ + return 0xcafe; +} + +int +testf_dso_mutex_lock(pthread_mutex_t *mtx) +{ + ATF_REQUIRE(mtx != NULL); + ATF_REQUIRE(pthread_mutex_lock(mtx) == 0); + + return 0xcafe; +} + +int +testf_dso_mutex_unlock(pthread_mutex_t *mtx) +{ + ATF_REQUIRE(mtx != NULL); + ATF_REQUIRE(pthread_mutex_unlock(mtx) == 0); + + return 0xcafe; +} + +int +testf_dso_pthread_create(pthread_t *thread, const pthread_attr_t *attr, + void *(*routine)(void *), void *arg) +{ + int ret; + + ret = pthread_create(thread, attr, routine, arg); + ATF_REQUIRE(ret == 0); + + return 0; +} diff --git a/contrib/netbsd-tests/lib/libpthread/dlopen/t_dlopen.c b/contrib/netbsd-tests/lib/libpthread/dlopen/t_dlopen.c new file mode 100644 index 0000000..68e9835 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/dlopen/t_dlopen.c @@ -0,0 +1,171 @@ +/* $NetBSD: t_dlopen.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */ +/*- + * Copyright (c) 2013 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Emmanuel Dreyfus + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_dlopen.c,v 1.1 2013/03/21 16:50:21 christos Exp $"); + +#include <atf-c.h> +#include <dlfcn.h> +#include <pthread.h> +#include <unistd.h> + +ATF_TC(dlopen); + +ATF_TC_HEAD(dlopen, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test if dlopen can load -lpthread DSO"); +} + +#define DSO TESTDIR "/h_pthread_dlopen.so" + +ATF_TC_BODY(dlopen, tc) +{ + void *handle; + int (*testf_dso_null)(void); + handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL); + ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror()); + + testf_dso_null = dlsym(handle, "testf_dso_null"); + ATF_REQUIRE_MSG(testf_dso_null != NULL, "dlsym fails: %s", dlerror()); + + ATF_REQUIRE(testf_dso_null() == 0xcafe); + + ATF_REQUIRE(dlclose(handle) == 0); +} + +ATF_TC(dlopen_mutex); + +ATF_TC_HEAD(dlopen_mutex, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test if dlopen can load -lpthread DSO without breaking mutex"); +} + +ATF_TC_BODY(dlopen_mutex, tc) +{ + pthread_mutex_t mtx; + void *handle; + int (*testf_dso_null)(void); + + ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0); + ATF_REQUIRE(pthread_mutex_lock(&mtx) == 0); + + handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL); + ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror()); + + testf_dso_null = dlsym(handle, "testf_dso_null"); + ATF_REQUIRE_MSG(testf_dso_null != NULL, "dlsym fails: %s", dlerror()); + + ATF_REQUIRE(testf_dso_null() == 0xcafe); + + ATF_REQUIRE(pthread_mutex_unlock(&mtx) == 0); + + ATF_REQUIRE(dlclose(handle) == 0); + + pthread_mutex_destroy(&mtx); +} + +ATF_TC(dlopen_mutex_libc); + +ATF_TC_HEAD(dlopen_mutex_libc, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test if dlopen can load -lpthread DSO and use libc locked mutex"); +} + +ATF_TC_BODY(dlopen_mutex_libc, tc) +{ + pthread_mutex_t mtx; + void *handle; + int (*testf_dso_mutex_unlock)(pthread_mutex_t *); + + ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0); + ATF_REQUIRE(pthread_mutex_lock(&mtx) == 0); + + handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL); + ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror()); + + testf_dso_mutex_unlock = dlsym(handle, "testf_dso_mutex_unlock"); + ATF_REQUIRE_MSG(testf_dso_mutex_unlock != NULL, + "dlsym fails: %s", dlerror()); + + ATF_REQUIRE(testf_dso_mutex_unlock(&mtx) == 0xcafe); + + dlclose(handle); + + pthread_mutex_destroy(&mtx); +} + +ATF_TC(dlopen_mutex_libpthread); + +ATF_TC_HEAD(dlopen_mutex_libpthread, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test if dlopen can load -lpthread DSO and use " + "libpthread locked mutex"); +} + +ATF_TC_BODY(dlopen_mutex_libpthread, tc) +{ + pthread_mutex_t mtx; + void *handle; + int (*testf_dso_mutex_lock)(pthread_mutex_t *); + + ATF_REQUIRE(pthread_mutex_init(&mtx, NULL) == 0); + + handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL); + ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror()); + + testf_dso_mutex_lock = dlsym(handle, "testf_dso_mutex_lock"); + ATF_REQUIRE_MSG(testf_dso_mutex_lock != NULL, + "dlsym fails: %s", dlerror()); + + ATF_REQUIRE(testf_dso_mutex_lock(&mtx) == 0xcafe); + + ATF_REQUIRE(pthread_mutex_unlock(&mtx) == 0); + + dlclose(handle); + + pthread_mutex_destroy(&mtx); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, dlopen); + ATF_TP_ADD_TC(tp, dlopen_mutex); + ATF_TP_ADD_TC(tp, dlopen_mutex_libc); + ATF_TP_ADD_TC(tp, dlopen_mutex_libpthread); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/dlopen/t_dso_pthread_create.c b/contrib/netbsd-tests/lib/libpthread/dlopen/t_dso_pthread_create.c new file mode 100644 index 0000000..ab8bec3 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/dlopen/t_dso_pthread_create.c @@ -0,0 +1,96 @@ +/* $NetBSD: t_dso_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */ +/*- + * Copyright (c) 2013 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Emmanuel Dreyfus + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_dso_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $"); + +#include <sys/resource.h> +#include <atf-c.h> +#include <dlfcn.h> +#include <pthread.h> +#include <unistd.h> + +#define DSO TESTDIR "/h_pthread_dlopen.so" + +void * +routine(void *arg) +{ + ATF_REQUIRE((intptr_t)arg == 0xcafe); + return NULL; +} + +ATF_TC(dso_pthread_create_dso); + +ATF_TC_HEAD(dso_pthread_create_dso, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test if non -lpthread main can call pthread_create() " + "in -lpthread DSO"); +} + +ATF_TC_BODY(dso_pthread_create_dso, tc) +{ + int ret; + pthread_t thread; + void *arg = (void *)0xcafe; + void *handle; + int (*testf_dso_pthread_create)(pthread_t *, pthread_attr_t *, + void *(*)(void *), void *); + struct rlimit rl; + + atf_tc_expect_signal(6, + "calling pthread_create() requires -lpthread main"); + + rl.rlim_max = rl.rlim_cur = 0; + ATF_REQUIRE_EQ(setrlimit(RLIMIT_CORE, &rl), 0); + + handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL); + ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror()); + + testf_dso_pthread_create = dlsym(handle, "testf_dso_pthread_create"); + ATF_REQUIRE_MSG(testf_dso_pthread_create != NULL, + "dlsym fails: %s", dlerror()); + + ret = testf_dso_pthread_create(&thread, NULL, routine, arg); + ATF_REQUIRE(ret == 0); + + ATF_REQUIRE(dlclose(handle) == 0); + +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, dso_pthread_create_dso); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/dlopen/t_main_pthread_create.c b/contrib/netbsd-tests/lib/libpthread/dlopen/t_main_pthread_create.c new file mode 100644 index 0000000..7ba89b3 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/dlopen/t_main_pthread_create.c @@ -0,0 +1,106 @@ +/* $NetBSD: t_main_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */ +/*- + * Copyright (c) 2013 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Emmanuel Dreyfus + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_main_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $"); + +#include <atf-c.h> +#include <dlfcn.h> +#include <pthread.h> +#include <unistd.h> + +#define DSO TESTDIR "/h_pthread_dlopen.so" + +void * +routine(void *arg) +{ + ATF_REQUIRE((intptr_t)arg == 0xcafe); + return NULL; +} + +ATF_TC(main_pthread_create_main); + +ATF_TC_HEAD(main_pthread_create_main, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test if -lpthread main can call pthread_create() in main()"); +} + +ATF_TC_BODY(main_pthread_create_main, tc) +{ + int ret; + pthread_t thread; + void *arg = (void *)0xcafe; + + ret = pthread_create(&thread, NULL, routine, arg); + ATF_REQUIRE(ret == 0); +} + +ATF_TC(main_pthread_create_dso); + +ATF_TC_HEAD(main_pthread_create_dso, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test if -lpthread main can call pthread_create() in DSO"); +} + +ATF_TC_BODY(main_pthread_create_dso, tc) +{ + int ret; + pthread_t thread; + void *arg = (void *)0xcafe; + void *handle; + int (*testf_dso_pthread_create)(pthread_t *, pthread_attr_t *, + void *(*)(void *), void *); + + handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL); + ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror()); + + testf_dso_pthread_create = dlsym(handle, "testf_dso_pthread_create"); + ATF_REQUIRE_MSG(testf_dso_pthread_create != NULL, + "dlsym fails: %s", dlerror()); + + ret = testf_dso_pthread_create(&thread, NULL, routine, arg); + ATF_REQUIRE(ret == 0); + + ATF_REQUIRE(dlclose(handle) == 0); + +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, main_pthread_create_main); + ATF_TP_ADD_TC(tp, main_pthread_create_dso); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/h_atexit.c b/contrib/netbsd-tests/lib/libpthread/h_atexit.c new file mode 100644 index 0000000..2d4c91c --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/h_atexit.c @@ -0,0 +1,183 @@ +/* $NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Program to test atexit(3) and __cxa_atexit()/__cxa_finalize(). + * + * Written by Jason R. Thorpe, February 2003. + * Public domain. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: h_atexit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <stdio.h> +#include <stdlib.h> +#include <signal.h> +#include <string.h> +#include <unistd.h> + +extern int __cxa_atexit(void (*func)(void *), void *, void *); +extern void __cxa_finalize(void *); + +static int dso_handle_1; +static int dso_handle_2; +static int dso_handle_3; + +static int arg_1; +static int arg_2; +static int arg_3; + +static int exiting_state; + +#define ASSERT(expr) \ +do { \ + if ((expr) == 0) { \ + write(STDERR_FILENO, __func__, strlen(__func__)); \ + write(STDERR_FILENO, ": ", 2); \ + write(STDERR_FILENO, __STRING(expr), \ + strlen(__STRING(expr))); \ + write(STDERR_FILENO, "\n", 1); \ + my_abort(); \ + } \ +} while (/*CONSTCOND*/0) + +#define SUCCESS() \ +do { \ + write(STDOUT_FILENO, __func__, strlen(__func__)); \ + write(STDOUT_FILENO, "\n", 1); \ +} while (/*CONSTCOND*/0) + +static void +my_abort(void) +{ + + kill(getpid(), SIGABRT); + /* NOTREACHED */ +} + +static void +cxa_handler_5(void *arg) +{ + static int cxa_handler_5_called; + + ASSERT(arg == (void *)&arg_1); + ASSERT(cxa_handler_5_called == 0); + ASSERT(exiting_state == 5); + + exiting_state--; + cxa_handler_5_called = 1; + SUCCESS(); +} + +static void +cxa_handler_4(void *arg) +{ + static int cxa_handler_4_called; + + ASSERT(arg == (void *)&arg_1); + ASSERT(cxa_handler_4_called == 0); + ASSERT(exiting_state == 4); + + exiting_state--; + cxa_handler_4_called = 1; + SUCCESS(); +} + +static void +cxa_handler_3(void *arg) +{ + static int cxa_handler_3_called; + + ASSERT(arg == (void *)&arg_2); + ASSERT(cxa_handler_3_called == 0); + ASSERT(exiting_state == 3); + + exiting_state--; + cxa_handler_3_called = 1; + SUCCESS(); +} + +static void +cxa_handler_2(void *arg) +{ + static int cxa_handler_2_called; + + ASSERT(arg == (void *)&arg_3); + ASSERT(cxa_handler_2_called == 0); + ASSERT(exiting_state == 2); + + exiting_state--; + cxa_handler_2_called = 1; + SUCCESS(); +} + +static void +normal_handler_1(void) +{ + static int normal_handler_1_called; + + ASSERT(normal_handler_1_called == 0); + ASSERT(exiting_state == 1); + + exiting_state--; + normal_handler_1_called = 1; + SUCCESS(); +} + +static void +normal_handler_0(void) +{ + static int normal_handler_0_called; + + ASSERT(normal_handler_0_called == 0); + ASSERT(exiting_state == 0); + + normal_handler_0_called = 1; + SUCCESS(); +} + +int +main(int argc, char *argv[]) +{ + + exiting_state = 5; + + ASSERT(0 == atexit(normal_handler_0)); + ASSERT(0 == atexit(normal_handler_1)); + ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, &dso_handle_1)); + ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, &dso_handle_1)); + ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, &dso_handle_2)); + ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, &dso_handle_3)); + + __cxa_finalize(&dso_handle_1); + __cxa_finalize(&dso_handle_2); + exit(0); +} diff --git a/contrib/netbsd-tests/lib/libpthread/h_cancel.c b/contrib/netbsd-tests/lib/libpthread/h_cancel.c new file mode 100644 index 0000000..1077806 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/h_cancel.c @@ -0,0 +1,58 @@ +/* $NetBSD: h_cancel.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: h_cancel.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <stdio.h> +#include <stdlib.h> +#include <pthread.h> +#include <unistd.h> + +int +main(void) +{ + char str1[] = "You should see this.\n"; + char str2[] = "You should not see this.\n"; + + printf("Cancellation test: Self-cancellation and disabling.\n"); + + pthread_cancel(pthread_self()); + + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + + write(STDOUT_FILENO, str1, sizeof(str1)-1); + + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + + write(STDOUT_FILENO, str2, sizeof(str2)-1); + + exit(1); +} diff --git a/contrib/netbsd-tests/lib/libpthread/h_common.h b/contrib/netbsd-tests/lib/libpthread/h_common.h new file mode 100644 index 0000000..f4d03bc --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/h_common.h @@ -0,0 +1,12 @@ +#ifndef H_COMMON_H +#define H_COMMON_H + +#include <string.h> + +#define PTHREAD_REQUIRE(x) \ + do { \ + int ret = (x); \ + ATF_REQUIRE_MSG(ret == 0, "%s: %s", #x, strerror(ret)); \ + } while (0) + +#endif // H_COMMON_H diff --git a/contrib/netbsd-tests/lib/libpthread/h_exit.c b/contrib/netbsd-tests/lib/libpthread/h_exit.c new file mode 100644 index 0000000..a37f88f --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/h_exit.c @@ -0,0 +1,47 @@ +/* $NetBSD: h_exit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: h_exit.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <stdio.h> +#include <stdlib.h> +#include <pthread.h> + +int +main(void) +{ + printf("Test of pthread_exit() in main thread only.\n"); + + pthread_exit(NULL); + + printf("You shouldn't see this."); + exit(1); +} diff --git a/contrib/netbsd-tests/lib/libpthread/h_resolv.c b/contrib/netbsd-tests/lib/libpthread/h_resolv.c new file mode 100644 index 0000000..9c5fedc --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/h_resolv.c @@ -0,0 +1,208 @@ +/* $NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */ + +/*- + * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: h_resolv.c,v 1.2 2010/11/03 16:10:22 christos Exp $"); + +#include <pthread.h> +#include <stdio.h> +#include <netdb.h> +#include <stdlib.h> +#include <unistd.h> +#include <err.h> +#include <string.h> +#include <stringlist.h> + +#define NTHREADS 10 +#define NHOSTS 100 +#define WS " \t\n\r" + +static StringList *hosts = NULL; +static int debug = 0; +static int *ask = NULL; +static int *got = NULL; + +static void usage(void) __attribute__((__noreturn__)); +static void load(const char *); +static void resolvone(int); +static void *resolvloop(void *); +static void run(int *); + +static pthread_mutex_t stats = PTHREAD_MUTEX_INITIALIZER; + +static void +usage(void) +{ + (void)fprintf(stderr, + "Usage: %s [-d] [-h <nhosts>] [-n <nthreads>] <file> ...\n", + getprogname()); + exit(1); +} + +static void +load(const char *fname) +{ + FILE *fp; + size_t len; + char *line; + + if ((fp = fopen(fname, "r")) == NULL) + err(1, "Cannot open `%s'", fname); + while ((line = fgetln(fp, &len)) != NULL) { + char c = line[len]; + char *ptr; + line[len] = '\0'; + for (ptr = strtok(line, WS); ptr; ptr = strtok(NULL, WS)) + sl_add(hosts, strdup(ptr)); + line[len] = c; + } + + (void)fclose(fp); +} + +static void +resolvone(int n) +{ + char buf[1024]; + pthread_t self = pthread_self(); + size_t i = (random() & 0x0fffffff) % hosts->sl_cur; + char *host = hosts->sl_str[i]; + struct addrinfo *res; + int error, len; + if (debug) { + len = snprintf(buf, sizeof(buf), "%p: %d resolving %s %d\n", + self, n, host, (int)i); + (void)write(STDOUT_FILENO, buf, len); + } + error = getaddrinfo(host, NULL, NULL, &res); + if (debug) { + len = snprintf(buf, sizeof(buf), "%p: host %s %s\n", + self, host, error ? "not found" : "ok"); + (void)write(STDOUT_FILENO, buf, len); + } + pthread_mutex_lock(&stats); + ask[i]++; + got[i] += error == 0; + pthread_mutex_unlock(&stats); + if (error == 0) + freeaddrinfo(res); +} + +static void * +resolvloop(void *p) +{ + int *nhosts = (int *)p; + if (*nhosts == 0) + return NULL; + do + resolvone(*nhosts); + while (--(*nhosts)); + return NULL; +} + +static void +run(int *nhosts) +{ + pthread_t self = pthread_self(); + if (pthread_create(&self, NULL, resolvloop, nhosts) != 0) + err(1, "pthread_create"); +} + +int +main(int argc, char *argv[]) +{ + int nthreads = NTHREADS; + int nhosts = NHOSTS; + int i, c, done, *nleft; + hosts = sl_init(); + + srandom(1234); + + while ((c = getopt(argc, argv, "dh:n:")) != -1) + switch (c) { + case 'd': + debug++; + break; + case 'h': + nhosts = atoi(optarg); + break; + case 'n': + nthreads = atoi(optarg); + break; + default: + usage(); + } + + for (i = optind; i < argc; i++) + load(argv[i]); + + if (hosts->sl_cur == 0) + usage(); + + if ((nleft = malloc(nthreads * sizeof(int))) == NULL) + err(1, "malloc"); + if ((ask = calloc(hosts->sl_cur, sizeof(int))) == NULL) + err(1, "calloc"); + if ((got = calloc(hosts->sl_cur, sizeof(int))) == NULL) + err(1, "calloc"); + + + for (i = 0; i < nthreads; i++) { + nleft[i] = nhosts; + run(&nleft[i]); + } + + for (done = 0; !done;) { + done = 1; + for (i = 0; i < nthreads; i++) { + if (nleft[i] != 0) { + done = 0; + break; + } + } + sleep(1); + } + c = 0; + for (i = 0; i < (int)hosts->sl_cur; i++) { + if (ask[i] != got[i] && got[i] != 0) { + warnx("Error: host %s ask %d got %d\n", + hosts->sl_str[i], ask[i], got[i]); + c++; + } + } + free(nleft); + free(ask); + free(got); + sl_free(hosts, 1); + return c; +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_atexit.sh b/contrib/netbsd-tests/lib/libpthread/t_atexit.sh new file mode 100755 index 0000000..7b99618 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_atexit.sh @@ -0,0 +1,49 @@ +# $NetBSD: t_atexit.sh,v 1.2 2010/11/07 17:51:20 jmmv Exp $ +# +# Copyright (c) 2008 The NetBSD Foundation, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +atf_test_case atexit +atexit_head() +{ + atf_set "descr" "Checks atexit functionality" +} +atexit_body() +{ + cat >expout <<EOF +cxa_handler_5 +cxa_handler_4 +cxa_handler_3 +cxa_handler_2 +normal_handler_1 +normal_handler_0 +EOF + atf_check -o file:expout "$(atf_get_srcdir)/h_atexit" +} + +atf_init_test_cases() +{ + atf_add_test_case atexit +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_barrier.c b/contrib/netbsd-tests/lib/libpthread/t_barrier.c new file mode 100644 index 0000000..a06cfd9 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_barrier.c @@ -0,0 +1,110 @@ +/* $NetBSD: t_barrier.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_barrier.c,v 1.2 2010/11/03 16:10:22 christos Exp $"); + +#include <pthread.h> +#include <stdio.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +#define COUNT 5 + +pthread_barrier_t barrier; +pthread_mutex_t mutex; +int serial_count; +int after_barrier_count; + +static void * +threadfunc(void *arg) +{ + int which = (int)(long)arg; + int rv; + + printf("thread %d entering barrier\n", which); + rv = pthread_barrier_wait(&barrier); + printf("thread %d leaving barrier -> %d\n", which, rv); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + after_barrier_count++; + if (rv == PTHREAD_BARRIER_SERIAL_THREAD) + serial_count++; + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + + return NULL; +} + +ATF_TC(barrier); +ATF_TC_HEAD(barrier, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks barriers"); +} +ATF_TC_BODY(barrier, tc) +{ + int i; + pthread_t new[COUNT]; + void *joinval; + + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); + PTHREAD_REQUIRE(pthread_barrier_init(&barrier, NULL, COUNT)); + + for (i = 0; i < COUNT; i++) { + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + ATF_REQUIRE_EQ(after_barrier_count, 0); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + PTHREAD_REQUIRE(pthread_create(&new[i], NULL, threadfunc, + (void *)(long)i)); + sleep(2); + } + + for (i = 0; i < COUNT; i++) { + PTHREAD_REQUIRE(pthread_join(new[i], &joinval)); + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + ATF_REQUIRE(after_barrier_count > i); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + printf("main joined with thread %d\n", i); + } + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + ATF_REQUIRE_EQ(after_barrier_count, COUNT); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + ATF_REQUIRE_EQ(serial_count, 1); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, barrier); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_cancel.sh b/contrib/netbsd-tests/lib/libpthread/t_cancel.sh new file mode 100755 index 0000000..4a0701e --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_cancel.sh @@ -0,0 +1,44 @@ +# $NetBSD: t_cancel.sh,v 1.1 2010/07/16 15:42:53 jmmv Exp $ +# +# Copyright (c) 2008 The NetBSD Foundation, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +atf_test_case register_while_disabled +register_while_disabled_head() +{ + atf_set "descr" "Checks that a cancellation registered while" \ + "cancellation is enabled is not honored while cancellation is" \ + "disabled" +} +register_while_disabled_body() +{ + atf_check -o inline:"You should see this.\n" \ + "$(atf_get_srcdir)/h_cancel" +} + +atf_init_test_cases() +{ + atf_add_test_case register_while_disabled +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_cond.c b/contrib/netbsd-tests/lib/libpthread/t_cond.c new file mode 100644 index 0000000..9f33c9e --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_cond.c @@ -0,0 +1,563 @@ +/* $NetBSD: t_cond.c,v 1.6 2014/09/03 16:23:24 gson Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_cond.c,v 1.6 2014/09/03 16:23:24 gson Exp $"); + +#include <sys/time.h> + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +static pthread_mutex_t mutex; +static pthread_cond_t cond; +static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t static_cond = PTHREAD_COND_INITIALIZER; +static int count, share, toggle, total; + +static void * +signal_delay_wait_threadfunc(void *arg) +{ + int *shared = (int *) arg; + + printf("2: Second thread.\n"); + + printf("2: Locking mutex\n"); + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + printf("2: Got mutex.\n"); + printf("Shared value: %d. Changing to 0.\n", *shared); + *shared = 0; + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + PTHREAD_REQUIRE(pthread_cond_signal(&cond)); + + return NULL; +} + +ATF_TC(signal_delay_wait); +ATF_TC_HEAD(signal_delay_wait, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks condition variables"); +} +ATF_TC_BODY(signal_delay_wait, tc) +{ + pthread_t new; + void *joinval; + int sharedval; + + printf("1: condition variable test 1\n"); + + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); + PTHREAD_REQUIRE(pthread_cond_init(&cond, NULL)); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + + sharedval = 1; + + PTHREAD_REQUIRE(pthread_create(&new, NULL, signal_delay_wait_threadfunc, + &sharedval)); + + printf("1: Before waiting.\n"); + do { + sleep(2); + PTHREAD_REQUIRE(pthread_cond_wait(&cond, &mutex)); + printf("1: After waiting, in loop.\n"); + } while (sharedval != 0); + + printf("1: After the loop.\n"); + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + + printf("1: After releasing the mutex.\n"); + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + printf("1: Thread joined.\n"); +} + +static void * +signal_before_unlock_threadfunc(void *arg) +{ + int *shared = (int *) arg; + + printf("2: Second thread.\n"); + + printf("2: Locking mutex\n"); + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + printf("2: Got mutex.\n"); + printf("Shared value: %d. Changing to 0.\n", *shared); + *shared = 0; + + /* Signal first, then unlock, for a different test than #1. */ + PTHREAD_REQUIRE(pthread_cond_signal(&cond)); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + + return NULL; +} + +ATF_TC(signal_before_unlock); +ATF_TC_HEAD(signal_before_unlock, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks condition variables: signal before unlocking mutex"); +} +ATF_TC_BODY(signal_before_unlock, tc) +{ + pthread_t new; + void *joinval; + int sharedval; + + printf("1: condition variable test 2\n"); + + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); + PTHREAD_REQUIRE(pthread_cond_init(&cond, NULL)); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + + sharedval = 1; + + PTHREAD_REQUIRE(pthread_create(&new, NULL, + signal_before_unlock_threadfunc, &sharedval)); + + printf("1: Before waiting.\n"); + do { + sleep(2); + PTHREAD_REQUIRE(pthread_cond_wait(&cond, &mutex)); + printf("1: After waiting, in loop.\n"); + } while (sharedval != 0); + + printf("1: After the loop.\n"); + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + + printf("1: After releasing the mutex.\n"); + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + printf("1: Thread joined.\n"); +} + +static void * +signal_before_unlock_static_init_threadfunc(void *arg) +{ + int *shared = (int *) arg; + + printf("2: Second thread.\n"); + + printf("2: Locking mutex\n"); + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + printf("2: Got mutex.\n"); + printf("Shared value: %d. Changing to 0.\n", *shared); + *shared = 0; + + /* Signal first, then unlock, for a different test than #1. */ + PTHREAD_REQUIRE(pthread_cond_signal(&static_cond)); + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + + return NULL; +} + +ATF_TC(signal_before_unlock_static_init); +ATF_TC_HEAD(signal_before_unlock_static_init, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks condition variables: signal before unlocking " + "mutex, use static initializers"); +} +ATF_TC_BODY(signal_before_unlock_static_init, tc) +{ + pthread_t new; + void *joinval; + int sharedval; + + printf("1: condition variable test 3\n"); + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + + sharedval = 1; + + PTHREAD_REQUIRE(pthread_create(&new, NULL, + signal_before_unlock_static_init_threadfunc, &sharedval)); + + printf("1: Before waiting.\n"); + do { + sleep(2); + PTHREAD_REQUIRE(pthread_cond_wait(&static_cond, &static_mutex)); + printf("1: After waiting, in loop.\n"); + } while (sharedval != 0); + + printf("1: After the loop.\n"); + + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + + printf("1: After releasing the mutex.\n"); + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + printf("1: Thread joined.\n"); +} + +static void * +signal_wait_race_threadfunc(void *arg) +{ + printf("2: Second thread.\n"); + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + printf("2: Before the loop.\n"); + while (count>0) { + count--; + total++; + toggle = 0; + /* printf("2: Before signal %d.\n", count); */ + PTHREAD_REQUIRE(pthread_cond_signal(&static_cond)); + do { + PTHREAD_REQUIRE(pthread_cond_wait(&static_cond, + &static_mutex)); + } while (toggle != 1); + } + printf("2: After the loop.\n"); + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + + return NULL; +} + +ATF_TC(signal_wait_race); +ATF_TC_HEAD(signal_wait_race, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks condition variables"); +} +ATF_TC_BODY(signal_wait_race, tc) +{ + pthread_t new; + void *joinval; + int sharedval; + + printf("1: condition variable test 4\n"); + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + + count = 50000; + toggle = 0; + + PTHREAD_REQUIRE(pthread_create(&new, NULL, signal_wait_race_threadfunc, + &sharedval)); + + printf("1: Before waiting.\n"); + while (count>0) { + count--; + total++; + toggle = 1; + /* printf("1: Before signal %d.\n", count); */ + PTHREAD_REQUIRE(pthread_cond_signal(&static_cond)); + do { + PTHREAD_REQUIRE(pthread_cond_wait(&static_cond, + &static_mutex)); + } while (toggle != 0); + } + printf("1: After the loop.\n"); + + toggle = 1; + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + PTHREAD_REQUIRE(pthread_cond_signal(&static_cond)); + + printf("1: After releasing the mutex.\n"); + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + printf("1: Thread joined. Final count = %d, total = %d\n", + count, total); + + ATF_REQUIRE_EQ(count, 0); + ATF_REQUIRE_EQ(total, 50000); +} + +static void * +pthread_cond_timedwait_func(void *arg) +{ + struct timespec ts; + size_t i = 0; + int rv; + + for (;;) { + + if (i++ >= 10000) + pthread_exit(NULL); + + (void)memset(&ts, 0, sizeof(struct timespec)); + + ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts) == 0); + + /* + * Set to one second in the past: + * pthread_cond_timedwait(3) should + * return ETIMEDOUT immediately. + */ + ts.tv_sec = ts.tv_sec - 1; + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + rv = pthread_cond_timedwait(&static_cond, &static_mutex, &ts); + + /* + * Sometimes we catch ESRCH. + * This should never happen. + */ + ATF_REQUIRE(rv == ETIMEDOUT); + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + } +} + +ATF_TC(cond_timedwait_race); +ATF_TC_HEAD(cond_timedwait_race, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test pthread_cond_timedwait(3)"); + +} +ATF_TC_BODY(cond_timedwait_race, tc) +{ + pthread_t tid[64]; + size_t i; + + for (i = 0; i < __arraycount(tid); i++) { + + PTHREAD_REQUIRE(pthread_create(&tid[i], NULL, + pthread_cond_timedwait_func, NULL)); + } + + for (i = 0; i < __arraycount(tid); i++) { + + PTHREAD_REQUIRE(pthread_join(tid[i], NULL)); + } +} + +static void * +broadcast_threadfunc(void *arg) +{ + printf("2: Second thread.\n"); + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + while (count>0) { + count--; + total++; + toggle = 0; + PTHREAD_REQUIRE(pthread_cond_signal(&static_cond)); + do { + PTHREAD_REQUIRE(pthread_cond_wait(&static_cond, + &static_mutex)); + } while (toggle != 1); + } + printf("2: After the loop.\n"); + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + + return NULL; +} + + +ATF_TC(broadcast); +ATF_TC_HEAD(broadcast, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks condition variables: use pthread_cond_broadcast()"); +} +ATF_TC_BODY(broadcast, tc) +{ + pthread_t new; + void *joinval; + int sharedval; + + printf("1: condition variable test 5\n"); + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + + count = 50000; + toggle = 0; + + PTHREAD_REQUIRE(pthread_create(&new, NULL, broadcast_threadfunc, + &sharedval)); + + printf("1: Before waiting.\n"); + while (count>0) { + count--; + total++; + toggle = 1; + PTHREAD_REQUIRE(pthread_cond_broadcast(&static_cond)); + do { + PTHREAD_REQUIRE(pthread_cond_wait(&static_cond, + &static_mutex)); + } while (toggle != 0); + } + printf("1: After the loop.\n"); + + toggle = 1; + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + PTHREAD_REQUIRE(pthread_cond_signal(&static_cond)); + + printf("1: After releasing the mutex.\n"); + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + printf("1: Thread joined. Final count = %d, total = %d\n", count, + total); + + ATF_REQUIRE_EQ(count, 0); + ATF_REQUIRE_EQ(total, 50000); +} + +static void * +bogus_timedwaits_threadfunc(void *arg) +{ + return NULL; +} + +ATF_TC(bogus_timedwaits); +ATF_TC_HEAD(bogus_timedwaits, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks condition variables: bogus timedwaits"); +} +ATF_TC_BODY(bogus_timedwaits, tc) +{ + pthread_t new; + struct timespec ts; + struct timeval tv; + + printf("condition variable test 6: bogus timedwaits\n"); + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + + printf("unthreaded test (past)\n"); + gettimeofday(&tv, NULL); + tv.tv_sec -= 2; /* Place the time in the past */ + TIMEVAL_TO_TIMESPEC(&tv, &ts); + + ATF_REQUIRE_EQ_MSG(pthread_cond_timedwait(&static_cond, &static_mutex, + &ts), ETIMEDOUT, "pthread_cond_timedwait() (unthreaded) in the " + "past"); + + printf("unthreaded test (zero time)\n"); + tv.tv_sec = 0; + tv.tv_usec = 0; + TIMEVAL_TO_TIMESPEC(&tv, &ts); + + ATF_REQUIRE_EQ_MSG(pthread_cond_timedwait(&static_cond, &static_mutex, + &ts), ETIMEDOUT, "pthread_cond_timedwait() (unthreaded) with zero " + "time"); + + PTHREAD_REQUIRE(pthread_create(&new, NULL, bogus_timedwaits_threadfunc, + NULL)); + PTHREAD_REQUIRE(pthread_join(new, NULL)); + + printf("threaded test\n"); + gettimeofday(&tv, NULL); + tv.tv_sec -= 2; /* Place the time in the past */ + TIMEVAL_TO_TIMESPEC(&tv, &ts); + + ATF_REQUIRE_EQ_MSG(pthread_cond_timedwait(&static_cond, &static_mutex, + &ts), ETIMEDOUT, "pthread_cond_timedwait() (threaded) in the past"); + + printf("threaded test (zero time)\n"); + tv.tv_sec = 0; + tv.tv_usec = 0; + TIMEVAL_TO_TIMESPEC(&tv, &ts); + + ATF_REQUIRE_EQ_MSG(pthread_cond_timedwait(&static_cond, &static_mutex, + &ts), ETIMEDOUT, "pthread_cond_timedwait() (threaded) with zero " + "time"); + + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); +} + +static void +unlock(void *arg) +{ + pthread_mutex_unlock((pthread_mutex_t *)arg); +} + +static void * +destroy_after_cancel_threadfunc(void *arg) +{ + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + + pthread_cleanup_push(unlock, &mutex); + + while (1) { + share = 1; + PTHREAD_REQUIRE(pthread_cond_broadcast(&cond)); + PTHREAD_REQUIRE(pthread_cond_wait(&cond, &mutex)); + } + + pthread_cleanup_pop(0); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + + return NULL; +} + +ATF_TC(destroy_after_cancel); +ATF_TC_HEAD(destroy_after_cancel, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks destroying a condition variable " + "after cancelling a wait"); +} +ATF_TC_BODY(destroy_after_cancel, tc) +{ + pthread_t thread; + + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); + PTHREAD_REQUIRE(pthread_cond_init(&cond, NULL)); + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + PTHREAD_REQUIRE(pthread_create(&thread, NULL, + destroy_after_cancel_threadfunc, NULL)); + + while (share == 0) { + PTHREAD_REQUIRE(pthread_cond_wait(&cond, &mutex)); + } + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + PTHREAD_REQUIRE(pthread_cancel(thread)); + + PTHREAD_REQUIRE(pthread_join(thread, NULL)); + PTHREAD_REQUIRE(pthread_cond_destroy(&cond)); + + PTHREAD_REQUIRE(pthread_mutex_destroy(&mutex)); +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, signal_delay_wait); + ATF_TP_ADD_TC(tp, signal_before_unlock); + ATF_TP_ADD_TC(tp, signal_before_unlock_static_init); + ATF_TP_ADD_TC(tp, signal_wait_race); + ATF_TP_ADD_TC(tp, cond_timedwait_race); + ATF_TP_ADD_TC(tp, broadcast); + ATF_TP_ADD_TC(tp, bogus_timedwaits); + ATF_TP_ADD_TC(tp, destroy_after_cancel); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_condwait.c b/contrib/netbsd-tests/lib/libpthread/t_condwait.c new file mode 100644 index 0000000..9b79587 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_condwait.c @@ -0,0 +1,142 @@ +/* $NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $ */ + +/* + * Copyright (c) 2013 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $"); + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "isqemu.h" + +#define WAITTIME 2 /* Timeout wait secound */ + +static const int debug = 1; + +static void * +run(void *param) +{ + struct timespec ts, to, te; + clockid_t clck; + pthread_condattr_t attr; + pthread_cond_t cond; + pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; + int ret = 0; + + + clck = *(clockid_t *)param; + pthread_condattr_init(&attr); + pthread_condattr_setclock(&attr, clck); /* MONOTONIC or MONOTONIC */ + pthread_cond_init(&cond, &attr); + + ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0); + + ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0); + to = ts; + + if (debug) + printf("started: %lld.%09ld sec\n", (long long)to.tv_sec, + to.tv_nsec); + + ts.tv_sec += WAITTIME; /* Timeout wait */ + + switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) { + case ETIMEDOUT: + /* Timeout */ + ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0); + timespecsub(&te, &to, &to); + if (debug) { + printf("timeout: %lld.%09ld sec\n", + (long long)te.tv_sec, te.tv_nsec); + printf("elapsed: %lld.%09ld sec\n", + (long long)to.tv_sec, to.tv_nsec); + } + if (isQEMU()) { + double to_seconds = to.tv_sec + 1e-9 * to.tv_nsec; + ATF_REQUIRE(to_seconds >= WAITTIME * 0.9); + /* Loose upper limit because of qemu timing bugs */ + ATF_REQUIRE(to_seconds < WAITTIME * 2.5); + } else { + ATF_REQUIRE_EQ(to.tv_sec, WAITTIME); + } + break; + default: + ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret)); + } + + ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)), + "pthread_mutex_unlock: %s", strerror(ret)); + pthread_exit(&ret); +} + +static void +cond_wait(clockid_t clck, const char *msg) { + pthread_t child; + + if (debug) + printf( "**** %s clock wait starting\n", msg); + ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0); + ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */ + if (debug) + printf( "**** %s clock wait ended\n", msg); +} + +ATF_TC(cond_wait_real); +ATF_TC_HEAD(cond_wait_real, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " + "with CLOCK_REALTIME"); +} + +ATF_TC_BODY(cond_wait_real, tc) { + cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME"); +} + +ATF_TC(cond_wait_mono); +ATF_TC_HEAD(cond_wait_mono, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait " + "with CLOCK_MONOTONIC"); +} + +ATF_TC_BODY(cond_wait_mono, tc) { + cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, cond_wait_real); + ATF_TP_ADD_TC(tp, cond_wait_mono); + return 0; +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_detach.c b/contrib/netbsd-tests/lib/libpthread/t_detach.c new file mode 100644 index 0000000..21db871 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_detach.c @@ -0,0 +1,91 @@ +/* $NetBSD: t_detach.c,v 1.1 2011/03/24 13:52:04 jruoho Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jukka Ruohonen. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_detach.c,v 1.1 2011/03/24 13:52:04 jruoho Exp $"); + +#include <pthread.h> +#include <errno.h> + +#include <atf-c.h> + +#include "h_common.h" + +static void *func(void *); + +static void * +func(void *arg) +{ + return NULL; +} + +ATF_TC(pthread_detach); +ATF_TC_HEAD(pthread_detach, tc) +{ + atf_tc_set_md_var(tc, "descr", "A test of pthread_detach(3)"); +} + +ATF_TC_BODY(pthread_detach, tc) +{ + const int state = PTHREAD_CREATE_JOINABLE; + pthread_attr_t attr; + pthread_t t; + int rv; + + /* + * Create a joinable thread. + */ + PTHREAD_REQUIRE(pthread_attr_init(&attr)); + PTHREAD_REQUIRE(pthread_attr_setdetachstate(&attr, state)); + PTHREAD_REQUIRE(pthread_create(&t, &attr, func, NULL)); + + /* + * Detach the thread and try to + * join it; EINVAL should follow. + */ + PTHREAD_REQUIRE(pthread_detach(t)); + + rv = pthread_join(t, NULL); + ATF_REQUIRE(rv == EINVAL); + + /* + * As usual, ESRCH should follow if + * we try to detach an invalid thread. + */ + rv = pthread_cancel(NULL); + ATF_REQUIRE(rv == ESRCH); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, pthread_detach); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_equal.c b/contrib/netbsd-tests/lib/libpthread/t_equal.c new file mode 100644 index 0000000..bcda996 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_equal.c @@ -0,0 +1,74 @@ +/* $NetBSD: t_equal.c,v 1.1 2011/03/24 12:40:59 jruoho Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jukka Ruohonen. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_equal.c,v 1.1 2011/03/24 12:40:59 jruoho Exp $"); + +#include <pthread.h> + +#include <atf-c.h> + +#include "h_common.h" + +static void *func(void *); + +static void * +func(void *arg) +{ + return NULL; +} + +ATF_TC(pthread_equal); +ATF_TC_HEAD(pthread_equal, tc) +{ + atf_tc_set_md_var(tc, "descr", "A test of pthread_equal(3)"); +} + +ATF_TC_BODY(pthread_equal, tc) +{ + pthread_t t1, t2; + + ATF_REQUIRE(pthread_create(&t1, NULL, func, NULL) == 0); + ATF_REQUIRE(pthread_create(&t2, NULL, func, NULL) == 0); + + ATF_REQUIRE(pthread_equal(t1, t1) != 0); + ATF_REQUIRE(pthread_equal(t2, t2) != 0); + ATF_REQUIRE(pthread_equal(t1, t2) == 0); + + ATF_REQUIRE(pthread_join(t1, NULL) == 0); + ATF_REQUIRE(pthread_join(t2, NULL) == 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, pthread_equal); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_exit.sh b/contrib/netbsd-tests/lib/libpthread/t_exit.sh new file mode 100755 index 0000000..7fbd689 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_exit.sh @@ -0,0 +1,41 @@ +# $NetBSD: t_exit.sh,v 1.1 2010/07/16 15:42:53 jmmv Exp $ +# +# Copyright (c) 2008 The NetBSD Foundation, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +atf_test_case main_thread +main_thread_head() +{ + atf_set "descr" "Checks calling pthread_exit() in the main thread" +} +main_thread_body() +{ + atf_check -o ignore "$(atf_get_srcdir)/h_exit" +} + +atf_init_test_cases() +{ + atf_add_test_case main_thread +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_fork.c b/contrib/netbsd-tests/lib/libpthread/t_fork.c new file mode 100644 index 0000000..ab8806d --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_fork.c @@ -0,0 +1,107 @@ +/* $NetBSD: t_fork.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_fork.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +/* + * Written by Love Hörnquist Åstrand <lha@NetBSD.org>, March 2003. + * Public domain. + */ + +#include <sys/types.h> +#include <sys/wait.h> + +#include <errno.h> +#include <pthread.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +static pid_t parent; +static int thread_survived = 0; + +static void * +print_pid(void *arg) +{ + sleep(3); + + thread_survived = 1; + if (parent != getpid()) { + exit(1); + } + return NULL; +} + +ATF_TC(fork); +ATF_TC_HEAD(fork, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks that child process doesn't get threads"); +} +ATF_TC_BODY(fork, tc) +{ + pthread_t p; + pid_t fork_pid; + + parent = getpid(); + + PTHREAD_REQUIRE(pthread_create(&p, NULL, print_pid, NULL)); + + fork_pid = fork(); + ATF_REQUIRE(fork_pid != -1); + + if (fork_pid) { + int status; + + PTHREAD_REQUIRE(pthread_join(p, NULL)); + ATF_REQUIRE_MSG(thread_survived, "thread did not survive in parent"); + + waitpid(fork_pid, &status, 0); + ATF_REQUIRE_MSG(WIFEXITED(status), "child died wrongly"); + ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), 0, "thread survived in child"); + } else { + sleep(5); + exit(thread_survived ? 1 : 0); + } +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, fork); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_fpu.c b/contrib/netbsd-tests/lib/libpthread/t_fpu.c new file mode 100644 index 0000000..4047b1f --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_fpu.c @@ -0,0 +1,148 @@ +/* $NetBSD: t_fpu.c,v 1.2 2013/01/27 14:47:37 mbalmer Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_fpu.c,v 1.2 2013/01/27 14:47:37 mbalmer Exp $"); + +/* + * This is adapted from part of csw/cstest of the MPD implementation by + * the University of Arizona CS department (http://www.cs.arizona.edu/sr/) + * which is in the public domain: + * + * "The MPD system is in the public domain and you may use and distribute it + * as you wish. We ask that you retain credits referencing the University + * of Arizona and that you identify any changes you make. + * + * We can't provide a warranty with MPD; it's up to you to determine its + * suitability and reliability for your needs. We would like to hear of + * any problems you encounter but we cannot promise a timely correction." + * + * It was changed to use pthread_create() and sched_yield() instead of + * the internal MPD context switching primitives by Ignatios Souvatzis + * <is@netbsd.org>. + */ + +#include <math.h> +#include <pthread.h> +#include <sched.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +#define N_RECURSE 10 + +static void recurse(void); + +int recursion_depth = 0; +pthread_mutex_t recursion_depth_lock; + +static void * +stir(void *p) +{ + double *q = (double *)p; + double x = *q++; + double y = *q++; + double z = *q++; + + for (;;) { + x = sin ((y = cos (x + y + .4)) - (z = cos (x + z + .6))); + PTHREAD_REQUIRE(sched_yield()); + } +} + +static double +mul3(double x, double y, double z) +{ + PTHREAD_REQUIRE(sched_yield()); + + return x * y * z; +} + +static void * +bar(void *p) +{ + double d; + int rc; + + d = mul3(mul3(2., 3., 5.), mul3(7., 11., 13.), mul3(17., 19., 23.)); + ATF_REQUIRE_EQ(d, 223092870.); + + PTHREAD_REQUIRE(pthread_mutex_lock(&recursion_depth_lock)); + rc = recursion_depth++; + PTHREAD_REQUIRE(pthread_mutex_unlock(&recursion_depth_lock)); + + if (rc < N_RECURSE) + recurse(); + else + atf_tc_pass(); + + /* NOTREACHED */ + return NULL; +} + +static void +recurse(void) { + pthread_t s2; + pthread_create(&s2, 0, bar, 0); + sleep(20); /* XXX must be long enough for our slowest machine */ +} + +ATF_TC(fpu); +ATF_TC_HEAD(fpu, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks that thread context switches will leave the " + "floating point computations unharmed"); +} +ATF_TC_BODY(fpu, tc) +{ + double stirseed[] = { 1.7, 3.2, 2.4 }; + pthread_t s5; + + printf("Testing threaded floating point computations...\n"); + + PTHREAD_REQUIRE(pthread_mutex_init(&recursion_depth_lock, 0)); + + pthread_create(&s5, 0, stir, stirseed); + recurse(); + + atf_tc_fail("exiting from main"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, fpu); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_join.c b/contrib/netbsd-tests/lib/libpthread/t_join.c new file mode 100644 index 0000000..7f6f834 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_join.c @@ -0,0 +1,174 @@ +/* $NetBSD: t_join.c,v 1.8 2012/03/12 20:17:16 joerg Exp $ */ + +/*- + * Copyright (c) 2010 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jukka Ruohonen. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_join.c,v 1.8 2012/03/12 20:17:16 joerg Exp $"); + +#include <errno.h> +#include <pthread.h> +#include <stdint.h> + +#include <atf-c.h> + +#include "h_common.h" + +#ifdef CHECK_STACK_ALIGNMENT +extern int check_stack_alignment(void); +#endif + +#define STACKSIZE 65536 + +static bool error; + +static void *threadfunc1(void *); +static void *threadfunc2(void *); + +ATF_TC(pthread_join); +ATF_TC_HEAD(pthread_join, tc) +{ + + atf_tc_set_md_var(tc, "descr", + "Checks basic error conditions in pthread_join(3)"); +} + +ATF_TC_BODY(pthread_join, tc) +{ + pthread_t thread; + + PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc1, NULL)); + PTHREAD_REQUIRE(pthread_join(thread, NULL)); +} + +static void * +threadfunc1(void *arg) +{ + pthread_t thread[25]; + pthread_t caller; + void *val = NULL; + uintptr_t i; + int rv; + pthread_attr_t attr; + + caller = pthread_self(); + +#ifdef CHECK_STACK_ALIGNMENT + /* + * Check alignment of thread stack, if supported. + */ + ATF_REQUIRE(check_stack_alignment()); +#endif + + /* + * The behavior is undefined, but should error + * out, if we try to join the calling thread. + */ + rv = pthread_join(caller, NULL); + + /* + * The specification recommends EDEADLK. + */ + ATF_REQUIRE(rv != 0); + ATF_REQUIRE_EQ(rv, EDEADLK); + + ATF_REQUIRE(pthread_attr_init(&attr) == 0); + + for (i = 0; i < __arraycount(thread); i++) { + + error = true; + + ATF_REQUIRE(pthread_attr_setstacksize(&attr, STACKSIZE * (i + 1)) == 0); + + rv = pthread_create(&thread[i], &attr, threadfunc2, (void *)i); + + ATF_REQUIRE_EQ(rv, 0); + + /* + * Check join and exit condition. + */ + PTHREAD_REQUIRE(pthread_join(thread[i], &val)); + + ATF_REQUIRE_EQ(error, false); + + ATF_REQUIRE(val != NULL); + ATF_REQUIRE(val == (void *)(i + 1)); + + /* + * Once the thread has returned, ESRCH should + * again follow if we try to join it again. + */ + rv = pthread_join(thread[i], NULL); + + ATF_REQUIRE_EQ(rv, ESRCH); + + /* + * Try to detach the exited thread. + */ + rv = pthread_detach(thread[i]); + + ATF_REQUIRE(rv != 0); + } + + ATF_REQUIRE(pthread_attr_destroy(&attr) == 0); + + pthread_exit(NULL); + + return NULL; +} + +static void * +threadfunc2(void *arg) +{ + static uintptr_t i = 0; + uintptr_t j; + pthread_attr_t attr; + size_t stacksize; + + j = (uintptr_t)arg; + + ATF_REQUIRE(pthread_attr_get_np(pthread_self(), &attr) == 0); + ATF_REQUIRE(pthread_attr_getstacksize(&attr, &stacksize) == 0); + ATF_REQUIRE(stacksize == STACKSIZE * (j + 1)); + ATF_REQUIRE(pthread_attr_destroy(&attr) == 0); + + if (i++ == j) + error = false; + + pthread_exit((void *)i); + + return NULL; +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, pthread_join); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_kill.c b/contrib/netbsd-tests/lib/libpthread/t_kill.c new file mode 100644 index 0000000..2fcdd99 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_kill.c @@ -0,0 +1,147 @@ +/* $NetBSD: t_kill.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/*- + * Copyright (c)2004 YAMAMOTO Takashi, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_kill.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <pthread.h> +#include <signal.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +#include <atf-c.h> + +#include "h_common.h" + +#define NTHREAD 2 + +struct threadinfo { + pthread_t id; + sig_atomic_t gotsignal; +} th[NTHREAD]; + +pthread_t mainthread; + +static void +sighandler(int signo) +{ + pthread_t self; + int i; + + self = pthread_self(); + ATF_REQUIRE_MSG((self != mainthread) && (signo == SIGUSR1), + "unexpected signal"); + + for (i = 0; i < NTHREAD; i++) + if (self == th[i].id) + break; + + ATF_REQUIRE_MSG(i != NTHREAD, "unknown thread"); + + th[i].gotsignal++; +} + +static void * +f(void *arg) +{ + struct threadinfo volatile *t = arg; + + while (t->gotsignal < 1) { + /* busy loop */ + } + + return NULL; +} + +ATF_TC(simple); +ATF_TC_HEAD(simple, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks pthread_kill()"); +} +ATF_TC_BODY(simple, tc) +{ + int i; + pthread_t self; + + mainthread = pthread_self(); + + ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR); + + for (i = 0; i < NTHREAD; i++) { + PTHREAD_REQUIRE(pthread_create(&th[i].id, NULL, f, &th[i])); + } + + sched_yield(); + + self = pthread_self(); + ATF_REQUIRE_EQ_MSG(self, mainthread, "thread id changed"); + + for (i = 0; i < NTHREAD; i++) { + PTHREAD_REQUIRE(pthread_kill(th[i].id, SIGUSR1)); + } + + for (i = 0; i < NTHREAD; i++) { + PTHREAD_REQUIRE(pthread_join(th[i].id, NULL)); + } +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, simple); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_mutex.c b/contrib/netbsd-tests/lib/libpthread/t_mutex.c new file mode 100644 index 0000000..1b435c2 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_mutex.c @@ -0,0 +1,316 @@ +/* $NetBSD: t_mutex.c,v 1.6 2014/02/09 21:26:07 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_mutex.c,v 1.6 2014/02/09 21:26:07 jmmv Exp $"); + +#include <pthread.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> +#include <atf-c/config.h> + +#include "h_common.h" + +static pthread_mutex_t mutex; +static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER; +static int global_x; + +static void * +mutex1_threadfunc(void *arg) +{ + int *param; + + printf("2: Second thread.\n"); + + param = arg; + printf("2: Locking mutex\n"); + pthread_mutex_lock(&mutex); + printf("2: Got mutex. *param = %d\n", *param); + ATF_REQUIRE_EQ(*param, 20); + (*param)++; + + pthread_mutex_unlock(&mutex); + + return param; +} + +ATF_TC(mutex1); +ATF_TC_HEAD(mutex1, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks mutexes"); +} +ATF_TC_BODY(mutex1, tc) +{ + int x; + pthread_t new; + void *joinval; + + printf("1: Mutex-test 1\n"); + + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); + x = 1; + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x)); + printf("1: Before changing the value.\n"); + sleep(2); + x = 20; + printf("1: Before releasing the mutex.\n"); + sleep(2); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + printf("1: After releasing the mutex.\n"); + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + printf("1: Thread joined. X was %d. Return value (int) was %d\n", + x, *(int *)joinval); + ATF_REQUIRE_EQ(x, 21); + ATF_REQUIRE_EQ(*(int *)joinval, 21); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); +} + +static void * +mutex2_threadfunc(void *arg) +{ + long count = *(int *)arg; + + printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count); + + while (count--) { + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + global_x++; + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + } + + return (void *)count; +} + +ATF_TC(mutex2); +ATF_TC_HEAD(mutex2, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks mutexes"); +#if defined(__powerpc__) + atf_tc_set_md_var(tc, "timeout", "40"); +#endif +} +ATF_TC_BODY(mutex2, tc) +{ + int count, count2; + pthread_t new; + void *joinval; + + printf("1: Mutex-test 2\n"); + +#if defined(__powerpc__) + atf_tc_expect_timeout("PR port-powerpc/44387"); +#endif + + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); + + global_x = 0; + count = count2 = 10000000; + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2)); + + printf("1: Thread %p\n", pthread_self()); + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + + while (count--) { + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + global_x++; + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + } + + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + printf("1: Thread joined. X was %d. Return value (long) was %ld\n", + global_x, (long)joinval); + ATF_REQUIRE_EQ(global_x, 20000000); + +#if defined(__powerpc__) + /* XXX force a timeout in ppc case since an un-triggered race + otherwise looks like a "failure" */ + /* We sleep for longer than the timeout to make ATF not + complain about unexpected success */ + sleep(41); +#endif +} + +static void * +mutex3_threadfunc(void *arg) +{ + long count = *(int *)arg; + + printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count); + + while (count--) { + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + global_x++; + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + } + + return (void *)count; +} + +ATF_TC(mutex3); +ATF_TC_HEAD(mutex3, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static " + "initializer"); +#if defined(__powerpc__) + atf_tc_set_md_var(tc, "timeout", "40"); +#endif +} +ATF_TC_BODY(mutex3, tc) +{ + int count, count2; + pthread_t new; + void *joinval; + + printf("1: Mutex-test 3\n"); + +#if defined(__powerpc__) + atf_tc_expect_timeout("PR port-powerpc/44387"); +#endif + + global_x = 0; + count = count2 = 10000000; + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2)); + + printf("1: Thread %p\n", pthread_self()); + + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + + while (count--) { + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + global_x++; + PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); + } + + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); + printf("1: Thread joined. X was %d. Return value (long) was %ld\n", + global_x, (long)joinval); + ATF_REQUIRE_EQ(global_x, 20000000); + +#if defined(__powerpc__) + /* XXX force a timeout in ppc case since an un-triggered race + otherwise looks like a "failure" */ + /* We sleep for longer than the timeout to make ATF not + complain about unexpected success */ + sleep(41); +#endif +} + +static void * +mutex4_threadfunc(void *arg) +{ + int *param; + + printf("2: Second thread.\n"); + + param = arg; + printf("2: Locking mutex\n"); + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + printf("2: Got mutex. *param = %d\n", *param); + (*param)++; + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + + return param; +} + +ATF_TC(mutex4); +ATF_TC_HEAD(mutex4, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks mutexes"); +} +ATF_TC_BODY(mutex4, tc) +{ + int x; + pthread_t new; + pthread_mutexattr_t mattr; + void *joinval; + + printf("1: Mutex-test 4\n"); + + PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr)); + PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE)); + + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr)); + + PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr)); + + x = 1; + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x)); + + printf("1: Before recursively acquiring the mutex.\n"); + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + + printf("1: Before releasing the mutex once.\n"); + sleep(2); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + printf("1: After releasing the mutex once.\n"); + + x = 20; + + printf("1: Before releasing the mutex twice.\n"); + sleep(2); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + printf("1: After releasing the mutex twice.\n"); + + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + printf("1: Thread joined. X was %d. Return value (int) was %d\n", + x, *(int *)joinval); + ATF_REQUIRE_EQ(x, 21); + ATF_REQUIRE_EQ(*(int *)joinval, 21); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, mutex1); + ATF_TP_ADD_TC(tp, mutex2); + ATF_TP_ADD_TC(tp, mutex3); + ATF_TP_ADD_TC(tp, mutex4); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_name.c b/contrib/netbsd-tests/lib/libpthread/t_name.c new file mode 100644 index 0000000..d17895e --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_name.c @@ -0,0 +1,103 @@ +/* $NetBSD: t_name.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_name.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +#define NAME_TOO_LONG "12345678901234567890123456789012" /* 32 chars */ +#define NAME_JUST_RIGHT "1234567890123456789012345678901" /* 31 chars */ + +#define CONST_NAME "xyzzy" +char non_const_name[] = CONST_NAME; + +static void * +threadfunc(void *arg) +{ + pthread_t self = pthread_self(); + char retname[32]; + + PTHREAD_REQUIRE(pthread_getname_np(self, retname, sizeof(retname))); + ATF_REQUIRE_STREQ(retname, NAME_JUST_RIGHT); + + PTHREAD_REQUIRE(pthread_setname_np(self, non_const_name, NULL)); + (void) memset(non_const_name, 0, sizeof(non_const_name)); + PTHREAD_REQUIRE(pthread_getname_np(self, retname, sizeof(retname))); + ATF_REQUIRE_STREQ(retname, CONST_NAME); + + return NULL; +} + +ATF_TC(name); +ATF_TC_HEAD(name, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks pthread_{,attr}_{get,set}name_np() API"); +} +ATF_TC_BODY(name, tc) +{ + pthread_t thr, self = pthread_self(); + pthread_attr_t attr; + char retname[32]; + + PTHREAD_REQUIRE(pthread_attr_init(&attr)); + PTHREAD_REQUIRE(pthread_attr_getname_np(&attr, retname, + sizeof(retname), NULL)); + ATF_REQUIRE_EQ(retname[0], '\0'); + ATF_REQUIRE_EQ(pthread_attr_setname_np(&attr, NAME_TOO_LONG, NULL), EINVAL); + PTHREAD_REQUIRE(pthread_attr_setname_np(&attr, "%s", + __UNCONST(NAME_JUST_RIGHT))); + + (void) strcpy(retname, "foo"); + PTHREAD_REQUIRE(pthread_getname_np(self, retname, sizeof(retname))); + ATF_REQUIRE_EQ(retname[0], '\0'); + + PTHREAD_REQUIRE(pthread_create(&thr, &attr, threadfunc, NULL)); + PTHREAD_REQUIRE(pthread_join(thr, NULL)); + + ATF_REQUIRE_EQ(pthread_getname_np(thr, retname, sizeof(retname)), ESRCH); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, name); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_once.c b/contrib/netbsd-tests/lib/libpthread/t_once.c new file mode 100644 index 0000000..575d5d7 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_once.c @@ -0,0 +1,196 @@ +/* $NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <pthread.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +#include "h_common.h" + +static pthread_once_t once = PTHREAD_ONCE_INIT; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +static int x; + +#define NTHREADS 25 + +static void +ofunc(void) +{ + printf("Variable x has value %d\n", x); + x++; +} + +ATF_TC(once1); +ATF_TC_HEAD(once1, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks pthread_once()"); +} +ATF_TC_BODY(once1, tc) +{ + + printf("1: Test 1 of pthread_once()\n"); + + PTHREAD_REQUIRE(pthread_once(&once, ofunc)); + PTHREAD_REQUIRE(pthread_once(&once, ofunc)); + + printf("1: X has value %d\n",x ); + ATF_REQUIRE_EQ(x, 1); +} + +static void +once2_ofunc(void) +{ + x++; + printf("ofunc: Variable x has value %d\n", x); + x++; +} + +static void * +once2_threadfunc(void *arg) +{ + int num; + + PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc)); + + num = *(int *)arg; + printf("Thread %d sees x with value %d\n", num, x); + ATF_REQUIRE_EQ(x, 2); + + return NULL; +} + +ATF_TC(once2); +ATF_TC_HEAD(once2, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks pthread_once()"); +} +ATF_TC_BODY(once2, tc) +{ + pthread_t threads[NTHREADS]; + int id[NTHREADS]; + int i; + + printf("1: Test 2 of pthread_once()\n"); + + for (i=0; i < NTHREADS; i++) { + id[i] = i; + PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i])); + } + + for (i=0; i < NTHREADS; i++) + PTHREAD_REQUIRE(pthread_join(threads[i], NULL)); + + printf("1: X has value %d\n",x ); + ATF_REQUIRE_EQ(x, 2); +} + +static void +once3_cleanup(void *m) +{ + pthread_mutex_t *mu = m; + + PTHREAD_REQUIRE(pthread_mutex_unlock(mu)); +} + +static void +once3_ofunc(void) +{ + pthread_testcancel(); +} + +static void * +once3_threadfunc(void *arg) +{ + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + pthread_cleanup_push(once3_cleanup, &mutex); + PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc)); + pthread_cleanup_pop(1); + + return NULL; +} + +static void +handler(int sig, siginfo_t *info, void *ctx) +{ + atf_tc_fail("Signal handler was called; " + "main thread deadlocked in pthread_once()"); +} + +ATF_TC(once3); +ATF_TC_HEAD(once3, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks pthread_once()"); +} +ATF_TC_BODY(once3, tc) +{ + pthread_t thread; + struct sigaction act; + struct itimerval it; + printf("Test 3 of pthread_once() (test versus cancellation)\n"); + + act.sa_sigaction = handler; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_SIGINFO; + sigaction(SIGALRM, &act, NULL); + + timerclear(&it.it_value); + it.it_value.tv_usec = 500000; + timerclear(&it.it_interval); + setitimer(ITIMER_REAL, &it, NULL); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL)); + PTHREAD_REQUIRE(pthread_cancel(thread)); + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + PTHREAD_REQUIRE(pthread_join(thread, NULL)); + + PTHREAD_REQUIRE(pthread_once(&once, ofunc)); + + /* Cancel timer */ + timerclear(&it.it_value); + setitimer(ITIMER_REAL, &it, NULL); + + printf("Test succeeded\n"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, once1); + ATF_TP_ADD_TC(tp, once2); + ATF_TP_ADD_TC(tp, once3); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_preempt.c b/contrib/netbsd-tests/lib/libpthread/t_preempt.c new file mode 100644 index 0000000..7ec43b6 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_preempt.c @@ -0,0 +1,128 @@ +/* $NetBSD: t_preempt.c,v 1.2 2010/11/03 16:10:22 christos Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_preempt.c,v 1.2 2010/11/03 16:10:22 christos Exp $"); + +#include <errno.h> +#include <fcntl.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +pthread_mutex_t mutex; +pthread_cond_t cond; +int started; + +#define HUGE_BUFFER 1<<20 +#define NTHREADS 1 + +static void * +threadfunc(void *arg) +{ + printf("2: Second thread.\n"); + + printf("2: Locking mutex\n"); + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + printf("2: Got mutex.\n"); + started++; + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + PTHREAD_REQUIRE(pthread_cond_signal(&cond)); + sleep(1); + + return NULL; +} + +ATF_TC(preempt1); +ATF_TC_HEAD(preempt1, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks kernel preemption during a large uiomove"); +} +ATF_TC_BODY(preempt1, tc) +{ + int i; + ssize_t rv; + pthread_t new; + void *joinval; + + char *mem; + int fd; + + mem = malloc(HUGE_BUFFER); + ATF_REQUIRE_MSG(mem != NULL, "%s", strerror(errno)); + + fd = open("/dev/urandom", O_RDONLY, 0); + ATF_REQUIRE_MSG(fd != -1, "%s", strerror(errno)); + + printf("1: preempt test\n"); + + PTHREAD_REQUIRE(pthread_cond_init(&cond, NULL)); + PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); + + PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); + + started = 0; + + for (i = 0; i < NTHREADS; i++) { + PTHREAD_REQUIRE(pthread_create(&new, NULL, threadfunc, NULL)); + } + + while (started < NTHREADS) { + PTHREAD_REQUIRE(pthread_cond_wait(&cond, &mutex)); + } + + printf("1: Thread has started.\n"); + + PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); + printf("1: After releasing the mutex.\n"); + + rv = read(fd, mem, HUGE_BUFFER); + close(fd); + ATF_REQUIRE_EQ(rv, HUGE_BUFFER); + + PTHREAD_REQUIRE(pthread_join(new, &joinval)); + + printf("1: Thread joined.\n"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, preempt1); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_resolv.sh b/contrib/netbsd-tests/lib/libpthread/t_resolv.sh new file mode 100755 index 0000000..847eadc --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_resolv.sh @@ -0,0 +1,42 @@ +# $NetBSD: t_resolv.sh,v 1.1 2010/07/16 15:42:53 jmmv Exp $ +# +# Copyright (c) 2008 The NetBSD Foundation, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +atf_test_case resolv +resolv_head() +{ + atf_set "descr" "Checks parallel name resolution" +} +resolv_body() +{ + "$(atf_get_srcdir)"/h_resolv -d -n 5 -h 5 \ + "$(atf_get_srcdir)"/d_mach || atf_fail "test failed" +} + +atf_init_test_cases() +{ + atf_add_test_case resolv +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_rwlock.c b/contrib/netbsd-tests/lib/libpthread/t_rwlock.c new file mode 100644 index 0000000..b2a3d6f --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_rwlock.c @@ -0,0 +1,125 @@ +/* $NetBSD: t_rwlock.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/*- + * Copyright (c)2004 YAMAMOTO Takashi, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_rwlock.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <errno.h> +#include <pthread.h> +#include <stdlib.h> +#include <string.h> + +#include <atf-c.h> + +#include "h_common.h" + +pthread_rwlock_t lk; + +struct timespec to; + +/* ARGSUSED */ +static void * +do_nothing(void *dummy) +{ + return NULL; +} + +ATF_TC(rwlock1); +ATF_TC_HEAD(rwlock1, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks read/write locks"); +} +ATF_TC_BODY(rwlock1, tc) +{ + int error; + pthread_t t; + + PTHREAD_REQUIRE(pthread_create(&t, NULL, do_nothing, NULL)); + PTHREAD_REQUIRE(pthread_rwlock_init(&lk, NULL)); + PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk)); + PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk)); + PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk)); + + ATF_REQUIRE_EQ(pthread_rwlock_trywrlock(&lk), EBUSY); + + ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0, + "%s", strerror(errno)); + to.tv_sec++; + error = pthread_rwlock_timedwrlock(&lk, &to); + ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK, + "%s", strerror(error)); + + PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk)); + + ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0, + "%s", strerror(errno)); + to.tv_sec++; + PTHREAD_REQUIRE(pthread_rwlock_timedwrlock(&lk, &to)); + + ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0, + "%s", strerror(errno)); + to.tv_sec++; + error = pthread_rwlock_timedwrlock(&lk, &to); + ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK, + "%s", strerror(error)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, rwlock1); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_sem.c b/contrib/netbsd-tests/lib/libpthread/t_sem.c new file mode 100644 index 0000000..467d182 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_sem.c @@ -0,0 +1,307 @@ +/* $NetBSD: t_sem.c,v 1.7 2012/03/09 19:46:37 joerg Exp $ */ + +/* + * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/*- + * Copyright (c)2004 YAMAMOTO Takashi, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/**************************************************************************** + * + * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice(s), this list of conditions and the following disclaimer as + * the first lines of this file unmodified other than the possible + * addition of one or more copyright notices. + * 2. Redistributions in binary form must reproduce the above copyright + * notice(s), this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008, 2010\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_sem.c,v 1.7 2012/03/09 19:46:37 joerg Exp $"); + +#include <errno.h> +#include <fcntl.h> +#include <pthread.h> +#include <semaphore.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <atf-c.h> +#include <atf-c/config.h> + +#include "h_common.h" + +#define NTHREADS 10 + +#define _LIBC_R_ + +#define SEM_REQUIRE(x) \ + ATF_REQUIRE_EQ_MSG(x, 0, "%s", strerror(errno)) + +static sem_t sem; + +ATF_TC(named); +ATF_TC_HEAD(named, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks named semaphores"); +} +ATF_TC_BODY(named, tc) +{ + sem_t *semp; + + ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); + + printf("Test begin\n"); + + (void) sem_unlink("/foo"); + semp = sem_open("/foo", O_CREAT | O_EXCL, 0644, 0); + ATF_REQUIRE_MSG(semp != SEM_FAILED, "%s", strerror(errno)); + SEM_REQUIRE(sem_close(semp)); + SEM_REQUIRE(sem_unlink("/foo")); + + printf("Test end\n"); +} + +ATF_TC(unnamed); +ATF_TC_HEAD(unnamed, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks unnamed semaphores"); +} + +static void * +entry(void * a_arg) +{ + pthread_t self = pthread_self(); + sem_t *semp = (sem_t *) a_arg; + + printf("Thread %p waiting for semaphore...\n", self); + sem_wait(semp); + printf("Thread %p got semaphore\n", self); + + return NULL; +} + +ATF_TC_BODY(unnamed, tc) +{ + sem_t sem_a, sem_b; + pthread_t threads[NTHREADS]; + unsigned i, j; + int val; + + ATF_REQUIRE_MSG(-1 != sysconf(_SC_SEMAPHORES), "%s", strerror(errno)); + + printf("Test begin\n"); + + SEM_REQUIRE(sem_init(&sem_b, 0, 0)); + SEM_REQUIRE(sem_getvalue(&sem_b, &val)); + ATF_REQUIRE_EQ(0, val); + + SEM_REQUIRE(sem_post(&sem_b)); + SEM_REQUIRE(sem_getvalue(&sem_b, &val)); + ATF_REQUIRE_EQ(1, val); + + SEM_REQUIRE(sem_wait(&sem_b)); + ATF_REQUIRE_EQ(sem_trywait(&sem_b), -1); + ATF_REQUIRE_EQ(errno, EAGAIN); + SEM_REQUIRE(sem_post(&sem_b)); + SEM_REQUIRE(sem_trywait(&sem_b)); + SEM_REQUIRE(sem_post(&sem_b)); + SEM_REQUIRE(sem_wait(&sem_b)); + SEM_REQUIRE(sem_post(&sem_b)); + + SEM_REQUIRE(sem_destroy(&sem_b)); + + SEM_REQUIRE(sem_init(&sem_a, 0, 0)); + + for (j = 0; j < 2; j++) { + for (i = 0; i < NTHREADS; i++) { + PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, + entry, (void *) &sem_a)); + } + + for (i = 0; i < NTHREADS; i++) { + usleep(10000); + printf("main loop %u: posting...\n", j+1); + SEM_REQUIRE(sem_post(&sem_a)); + } + + for (i = 0; i < NTHREADS; i++) { + PTHREAD_REQUIRE(pthread_join(threads[i], NULL)); + } + } + + SEM_REQUIRE(sem_destroy(&sem_a)); + + printf("Test end\n"); +} + +static void +sighandler(int signo) +{ + /* printf("signal %d\n", signo); */ + + ATF_REQUIRE_EQ_MSG(signo, SIGALRM, "unexpected signal"); + SEM_REQUIRE(sem_post(&sem)); +} + +static void +alarm_ms(const int ms) +{ + struct itimerval timer; + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_usec = 0; + timer.it_value.tv_sec = 0; + timer.it_value.tv_usec = ms * 1000; + ATF_REQUIRE(setitimer(ITIMER_REAL, &timer, NULL) == 0); +} + +static void * +threadfunc(void *arg) +{ + int i, ret; + + printf("Entering loop\n"); + for (i = 0; i < 500; ) { + if ((i & 1) != 0) { + do { + ret = sem_wait(&sem); + } while (ret == -1 && errno == EINTR); + ATF_REQUIRE(ret == 0); + } else { + ret = sem_trywait(&sem); + if (ret == -1) { + ATF_REQUIRE(errno == EAGAIN); + continue; + } + } + printf("%s: %d\n", __func__, i); + alarm_ms(5); + i++; + } + + return NULL; +} + +static void +before_start_test(const bool use_pthread) +{ + pthread_t t; + + SEM_REQUIRE(sem_init(&sem, 0, 0)); + ATF_REQUIRE(SIG_ERR != signal(SIGALRM, sighandler)); + + alarm_ms(5); + + if (use_pthread) { + PTHREAD_REQUIRE(pthread_create(&t, NULL, threadfunc, NULL)); + PTHREAD_REQUIRE(pthread_join(t, NULL)); + } else { + threadfunc(NULL); + } +} + +ATF_TC(before_start_no_threads); +ATF_TC_HEAD(before_start_no_threads, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks using semaphores without any " + "thread running"); + atf_tc_set_md_var(tc, "timeout", "40"); +} +ATF_TC_BODY(before_start_no_threads, tc) +{ + before_start_test(false); +} + +ATF_TC(before_start_one_thread); +ATF_TC_HEAD(before_start_one_thread, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks using semaphores before " + "starting one thread"); + atf_tc_set_md_var(tc, "timeout", "40"); +} +ATF_TC_BODY(before_start_one_thread, tc) +{ + before_start_test(true); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, named); + ATF_TP_ADD_TC(tp, unnamed); + ATF_TP_ADD_TC(tp, before_start_no_threads); + ATF_TP_ADD_TC(tp, before_start_one_thread); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_sigalarm.c b/contrib/netbsd-tests/lib/libpthread/t_sigalarm.c new file mode 100644 index 0000000..0a58c4e --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_sigalarm.c @@ -0,0 +1,107 @@ +/* $NetBSD: t_sigalarm.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_sigalarm.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <stdio.h> +#include <signal.h> +#include <unistd.h> +#include <pthread.h> + +#include <atf-c.h> + +#include "h_common.h" + +int alarm_set; + +#ifdef SA_SIGINFO +static void +alarm_handler(int signo, siginfo_t *si, void *ctx) +{ + ATF_REQUIRE_EQ_MSG(si->si_signo, signo, "Received unexpected signal"); + alarm_set = 1; +} +#else +static void +alarm_handler(int signo) +{ + ATF_REQUIRE_EQ_MSG(SIGALRM, signo, "Received unexpected signal"); + alarm_set = 1; +} +#endif + +static void * +setup(void *dummy) +{ + struct sigaction sa; + sigset_t ss; +#ifdef SA_SIGINFO + sa.sa_flags = SA_SIGINFO; + sa.sa_sigaction = alarm_handler; +#else + sa.sa_flags = 0; + sa.sa_handler = alarm_handler; +#endif + sigfillset(&ss); + sigprocmask(SIG_SETMASK, &ss, NULL); + sigemptyset(&sa.sa_mask); + sigaction(SIGALRM, &sa, NULL); + alarm(1); + + return NULL; +} + +ATF_TC(sigalarm); +ATF_TC_HEAD(sigalarm, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks sigsuspend in libpthread when pthread lib is initialized"); +} +ATF_TC_BODY(sigalarm, tc) +{ + sigset_t set; + pthread_t self = pthread_self(); + + PTHREAD_REQUIRE(pthread_create(&self, NULL, setup, NULL)); + + sigemptyset(&set); + sigsuspend(&set); + alarm(0); + + ATF_REQUIRE(alarm_set); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, sigalarm); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_siglongjmp.c b/contrib/netbsd-tests/lib/libpthread/t_siglongjmp.c new file mode 100644 index 0000000..298879c --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_siglongjmp.c @@ -0,0 +1,109 @@ +/* $NetBSD: t_siglongjmp.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_siglongjmp.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +/* + * Regression test for siglongjmp out of a signal handler back into + * its thread. + * + * Written by Christian Limpach <cl@NetBSD.org>, December 2003. + * Public domain. + */ + +#include <sys/resource.h> + +#include <pthread.h> +#include <setjmp.h> +#include <signal.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +static sigjmp_buf env; + +static void * +thread(void *arg) +{ + return NULL; +} + +static void +handler(int sig, siginfo_t *info, void *ctx) +{ + siglongjmp(env, 1); +} + +ATF_TC(siglongjmp1); +ATF_TC_HEAD(siglongjmp1, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks siglongjmp() out of a signal handler back into its thread"); +} +ATF_TC_BODY(siglongjmp1, tc) +{ + pthread_t t; + sigset_t nset; + struct rlimit rlim; + struct sigaction act; + + rlim.rlim_cur = rlim.rlim_max = 0; + (void)setrlimit(RLIMIT_CORE, &rlim); + + PTHREAD_REQUIRE(pthread_create(&t, NULL, thread, NULL)); + + sigemptyset(&nset); + sigaddset(&nset, SIGUSR1); + PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &nset, NULL)); + + act.sa_sigaction = handler; + sigemptyset(&act.sa_mask); + sigaddset(&act.sa_mask, SIGUSR2); + act.sa_flags = 0; + sigaction(SIGSEGV, &act, NULL); + + ATF_REQUIRE_EQ(sigsetjmp(env, 1), 0); + + PTHREAD_REQUIRE(pthread_sigmask(0, NULL, &nset)); + + ATF_REQUIRE_EQ_MSG(sigismember(&nset, SIGSEGV), 0, "SIGSEGV set"); + ATF_REQUIRE_EQ_MSG(sigismember(&nset, SIGUSR2), 0, "SIGUSR2 set"); + ATF_REQUIRE_MSG(sigismember(&nset, SIGUSR1), "SIGUSR1 not set"); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, siglongjmp1); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_sigmask.c b/contrib/netbsd-tests/lib/libpthread/t_sigmask.c new file mode 100644 index 0000000..69e0577 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_sigmask.c @@ -0,0 +1,261 @@ +/* $NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 christos Exp $ */ + +/* + * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008, 2010\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_sigmask.c,v 1.3 2013/10/19 17:45:01 christos Exp $"); + +/* + * Regression test for pthread_sigmask when SA upcalls aren't started yet. + * + * Written by Christian Limpach <cl@NetBSD.org>, December 2003. + * Public domain. + */ + +#include <errno.h> +#include <pthread.h> +#include <signal.h> +#include <stdio.h> +#include <unistd.h> + +#include <sys/resource.h> + +#include <atf-c.h> + +#include "h_common.h" + +static volatile sig_atomic_t flag; +static volatile sig_atomic_t flag2; + +static volatile pthread_t thr_usr1; +static volatile pthread_t thr_usr2; + +static sig_atomic_t count = 0; + +ATF_TC(upcalls_not_started); +ATF_TC_HEAD(upcalls_not_started, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks pthread_sigmask when SA upcalls " + "aren't started yet"); +} +ATF_TC_BODY(upcalls_not_started, tc) +{ + sigset_t nset; + struct rlimit rlim; + + rlim.rlim_cur = rlim.rlim_max = 0; + (void) setrlimit(RLIMIT_CORE, &rlim); + + sigemptyset(&nset); + sigaddset(&nset, SIGFPE); + pthread_sigmask(SIG_BLOCK, &nset, NULL); + + kill(getpid(), SIGFPE); +} + +static void +upcalls_not_started_handler1(int sig, siginfo_t *info, void *ctx) +{ + + kill(getpid(), SIGUSR2); + /* + * If the mask is properly set, SIGUSR2 will not be handled + * until this handler returns. + */ + flag = 1; +} + +static void +upcalls_not_started_handler2(int sig, siginfo_t *info, void *ctx) +{ + if (flag == 1) + flag = 2; +} + +ATF_TC(before_threads); +ATF_TC_HEAD(before_threads, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected " + "before threads are started"); +} +ATF_TC_BODY(before_threads, tc) +{ + struct sigaction act; + + act.sa_sigaction = upcalls_not_started_handler1; + sigemptyset(&act.sa_mask); + sigaddset(&act.sa_mask, SIGUSR2); + act.sa_flags = SA_SIGINFO; + + ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0); + + act.sa_sigaction = upcalls_not_started_handler2; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_SIGINFO; + (void)sigaction(SIGUSR2, &act, NULL); + + kill(getpid(), SIGUSR1); + + ATF_REQUIRE_EQ(flag, 2); + printf("Success: Both handlers ran in order\n"); +} + +static void +respected_while_running_handler1(int sig, siginfo_t *info, void *ctx) +{ + + kill(getpid(), SIGUSR2); + /* + * If the mask is properly set, SIGUSR2 will not be handled + * by the current thread until this handler returns. + */ + flag = 1; + thr_usr1 = pthread_self(); +} + +static void +respected_while_running_handler2(int sig, siginfo_t *info, void *ctx) +{ + if (flag == 1) + flag = 2; + flag2 = 1; + thr_usr2 = pthread_self(); +} + +static void * +respected_while_running_threadroutine(void *arg) +{ + + kill(getpid(), SIGUSR1); + sleep(1); + + if (flag == 2) + printf("Success: Both handlers ran in order\n"); + else if (flag == 1 && flag2 == 1 && thr_usr1 != thr_usr2) + printf("Success: Handlers were invoked by different threads\n"); + else { + printf("Failure: flag=%d, flag2=%d, thr1=%p, thr2=%p\n", + (int)flag, (int)flag2, (void *)thr_usr1, (void *)thr_usr2); + atf_tc_fail("failure"); + } + + return NULL; +} + +ATF_TC(respected_while_running); +ATF_TC_HEAD(respected_while_running, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks that signal masks are respected " + "while threads are running"); +} +ATF_TC_BODY(respected_while_running, tc) +{ + struct sigaction act; + pthread_t thread; + + act.sa_sigaction = respected_while_running_handler1; + sigemptyset(&act.sa_mask); + sigaddset(&act.sa_mask, SIGUSR2); + act.sa_flags = SA_SIGINFO; + + ATF_REQUIRE_EQ(sigaction(SIGUSR1, &act, NULL), 0); + + act.sa_sigaction = respected_while_running_handler2; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_SIGINFO; + (void)sigaction(SIGUSR2, &act, NULL); + + PTHREAD_REQUIRE(pthread_create(&thread, NULL, + respected_while_running_threadroutine, NULL)); + PTHREAD_REQUIRE(pthread_join(thread, NULL)); +} + +static void +incorrect_mask_bug_handler(int sig) +{ + count++; +} + +static void * +incorrect_mask_bug_sleeper(void* arg) +{ + int i; + for (i = 0; i < 10; i++) + sleep(1); + + atf_tc_fail("sleeper"); +} + +ATF_TC(incorrect_mask_bug); +ATF_TC_HEAD(incorrect_mask_bug, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks for bug in libpthread where " + "incorrect signal mask was used"); +} +ATF_TC_BODY(incorrect_mask_bug, tc) +{ + pthread_t id; + struct sigaction act; + + act.sa_sigaction = NULL; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + act.sa_handler = incorrect_mask_bug_handler; + + ATF_REQUIRE_EQ_MSG(sigaction(SIGALRM, &act, NULL), 0, "%s", + strerror(errno)); + + sigaddset(&act.sa_mask, SIGALRM); + PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL)); + + PTHREAD_REQUIRE(pthread_create(&id, NULL, incorrect_mask_bug_sleeper, + NULL)); + sleep(1); + + sigemptyset(&act.sa_mask); + PTHREAD_REQUIRE(pthread_sigmask(SIG_SETMASK, &act.sa_mask, NULL)); + + for (;;) { + alarm(1); + if (select(1, NULL, NULL, NULL, NULL) == -1 && errno == EINTR) + if (count == 2) + return; + } +} + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, upcalls_not_started); + ATF_TP_ADD_TC(tp, before_threads); + ATF_TP_ADD_TC(tp, respected_while_running); + ATF_TP_ADD_TC(tp, incorrect_mask_bug); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_sigsuspend.c b/contrib/netbsd-tests/lib/libpthread/t_sigsuspend.c new file mode 100644 index 0000000..1179657 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_sigsuspend.c @@ -0,0 +1,90 @@ +/* $NetBSD: t_sigsuspend.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_sigsuspend.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +/* + * Regression test for sigsuspend in libpthread when pthread lib isn't + * initialized. + * + * Written by Love FIXME strand <lha@NetBSD.org>, March 2003. + * Public domain. + */ + +#include <stdio.h> +#include <signal.h> +#include <unistd.h> + +#include <atf-c.h> + +int alarm_set; + +static void +alarm_handler(int signo) +{ + alarm_set = 1; +} + +ATF_TC(sigsuspend); +ATF_TC_HEAD(sigsuspend, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks sigsuspend in libpthread when pthread lib isn't initialized"); +} +ATF_TC_BODY(sigsuspend, tc) +{ + struct sigaction sa; + sigset_t set; + + sa.sa_flags = 0; + sa.sa_handler = alarm_handler; + sigemptyset(&sa.sa_mask); + + sigaction(SIGALRM, &sa, NULL); + + alarm(1); + + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + + sigsuspend(&set); + + alarm(0); + + ATF_REQUIRE(alarm_set); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, sigsuspend); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_sleep.c b/contrib/netbsd-tests/lib/libpthread/t_sleep.c new file mode 100644 index 0000000..93a30e3 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_sleep.c @@ -0,0 +1,104 @@ +/* $NetBSD: t_sleep.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ + +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__COPYRIGHT("@(#) Copyright (c) 2008\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD: t_sleep.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); + +#include <sys/time.h> + +#include <pthread.h> +#include <signal.h> +#include <stdio.h> +#include <unistd.h> + +#include <atf-c.h> + +#include "h_common.h" + +#define LONGTIME 2000000000 + +static void * +threadfunc(void *arg) +{ + sleep(LONGTIME); + + return NULL; +} + +static void +handler(int sig) +{ + /* + * Nothing to do; invoking the handler is enough to interrupt + * the sleep. + */ +} + +ATF_TC(sleep1); +ATF_TC_HEAD(sleep1, tc) +{ + atf_tc_set_md_var(tc, "descr", "Checks sleeping past the time when " + "time_t wraps"); +} +ATF_TC_BODY(sleep1, tc) +{ + pthread_t thread; + struct itimerval it; + struct sigaction act; + sigset_t mtsm; + + printf("Testing sleeps unreasonably far into the future.\n"); + + act.sa_handler = handler; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + sigaction(SIGALRM, &act, NULL); + + PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL)); + + /* make sure the signal is delivered to the child thread */ + sigemptyset(&mtsm); + sigaddset(&mtsm, SIGALRM); + PTHREAD_REQUIRE(pthread_sigmask(SIG_BLOCK, &mtsm, 0)); + + timerclear(&it.it_interval); + timerclear(&it.it_value); + it.it_value.tv_sec = 1; + setitimer(ITIMER_REAL, &it, NULL); + + PTHREAD_REQUIRE(pthread_join(thread, NULL)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, sleep1); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libpthread/t_swapcontext.c b/contrib/netbsd-tests/lib/libpthread/t_swapcontext.c new file mode 100644 index 0000000..8fd2314 --- /dev/null +++ b/contrib/netbsd-tests/lib/libpthread/t_swapcontext.c @@ -0,0 +1,112 @@ +/* $NetBSD: t_swapcontext.c,v 1.2 2014/08/25 16:31:15 bouyer Exp $ */ + +/* + * Copyright (c) 2012 Emmanuel Dreyfus. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD"); + +#include <pthread.h> +#include <ucontext.h> +#include <stdio.h> +#include <stdlib.h> + +#include <atf-c.h> + +#include "h_common.h" + +#define STACKSIZE 65536 + +char stack[STACKSIZE]; +ucontext_t nctx; +ucontext_t octx; +void *oself; +void *nself; +int val1, val2; + +/* ARGSUSED0 */ +static void +swapfunc(void *arg) +{ + /* + * If the test fails, we are very likely to crash + * without the opportunity to report + */ + nself = (void *)pthread_self(); + printf("after swapcontext self = %p\n", nself); + + ATF_REQUIRE_EQ(oself, nself); + printf("Test succeeded\n"); + /* Go back in main */ + ATF_REQUIRE(swapcontext(&nctx, &octx)); + + /* NOTREACHED */ + return; +} + +/* ARGSUSED0 */ +static void * +threadfunc(void *arg) +{ + nctx.uc_stack.ss_sp = stack; + nctx.uc_stack.ss_size = sizeof(stack); + + makecontext(&nctx, (void *)*swapfunc, 0); + + oself = (void *)pthread_self(); + printf("before swapcontext self = %p\n", oself); + PTHREAD_REQUIRE(swapcontext(&octx, &nctx)); + + /* NOTREACHED */ + return NULL; +} + + +ATF_TC(swapcontext1); +ATF_TC_HEAD(swapcontext1, tc) +{ + atf_tc_set_md_var(tc, "descr", "Testing if swapcontext() " + "alters pthread_self()"); +} +ATF_TC_BODY(swapcontext1, tc) +{ + pthread_t thread; + + oself = (void *)&val1; + nself = (void *)&val2; + + printf("Testing if swapcontext() alters pthread_self()\n"); + + PTHREAD_REQUIRE(getcontext(&nctx)); + PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc, NULL)); + PTHREAD_REQUIRE(pthread_join(thread, NULL)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, swapcontext1); + + return atf_no_error(); +} |