From 706f324ed2cae8af0b6565522c206f8c2fdf3b7b Mon Sep 17 00:00:00 2001 From: yamt Date: Tue, 27 Jul 2004 22:01:51 +0000 Subject: [PATCH] pthread_kill test. (currently fail with concurrency > 1) --- regress/lib/libpthread/Makefile | 7 +- regress/lib/libpthread/kill1/Makefile | 14 +++ regress/lib/libpthread/kill1/kill1.c | 117 ++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 regress/lib/libpthread/kill1/Makefile create mode 100644 regress/lib/libpthread/kill1/kill1.c diff --git a/regress/lib/libpthread/Makefile b/regress/lib/libpthread/Makefile index fe48d90b9bb2..534ad6a0fd45 100644 --- a/regress/lib/libpthread/Makefile +++ b/regress/lib/libpthread/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.20 2004/07/07 21:53:10 nathanw Exp $ +# $NetBSD: Makefile,v 1.21 2004/07/27 22:01:51 yamt Exp $ .include @@ -16,8 +16,9 @@ ARCHSUBDIR= ${MACHINE_CPU} .if defined(ARCHSUBDIR) SUBDIR+= atexit barrier1 cancel2 cond1 cond2 cond3 cond4 cond5 condcancel1 \ - conddestroy1 exit1 fork mutex1 mutex2 mutex3 mutex4 name once1 once2 \ - preempt1 resolv sem sigalarm sigmask1 siglongjmp1 sigsuspend + conddestroy1 exit1 fork kill1 mutex1 mutex2 mutex3 mutex4 name \ + once1 once2 preempt1 resolv sem sigalarm sigmask1 siglongjmp1 \ + sigsuspend .endif diff --git a/regress/lib/libpthread/kill1/Makefile b/regress/lib/libpthread/kill1/Makefile new file mode 100644 index 000000000000..474c27b53bb4 --- /dev/null +++ b/regress/lib/libpthread/kill1/Makefile @@ -0,0 +1,14 @@ +# $NetBSD: Makefile,v 1.1 2004/07/27 22:01:51 yamt Exp $ + +WARNS=2 + +PROG= kill1 + +LDADD= -lpthread + +NOMAN= + +regress: + ./kill1 + +.include diff --git a/regress/lib/libpthread/kill1/kill1.c b/regress/lib/libpthread/kill1/kill1.c new file mode 100644 index 000000000000..27966b7b2181 --- /dev/null +++ b/regress/lib/libpthread/kill1/kill1.c @@ -0,0 +1,117 @@ +/* $NetBSD: kill1.c,v 1.1 2004/07/27 22:01:51 yamt Exp $ */ + +/*- + * 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 +#include +#include +#include +#include + +#define NTHREAD 16 +struct threadinfo { + pthread_t id; + int gotsignal; +} th[NTHREAD]; + +pthread_t mainthread; + +void sighandler(int); +void *f(void *); +int main(int, char *[]); + +void +sighandler(int signo) +{ + pthread_t self; + int i; + + self = pthread_self(); + if (self == mainthread || signo != SIGUSR1) + errx(EXIT_FAILURE, "unexpected signal"); + + for (i = 0; i < NTHREAD; i++) + if (self == th[i].id) + break; + + if (i == NTHREAD) + errx(EXIT_FAILURE, "unknown thread"); + + th[i].gotsignal++; +} + +void * +f(void *arg) +{ + struct threadinfo volatile *t = arg; + + while (t->gotsignal < 1) { + /* busy loop */ + } + + return NULL; +} + +int +main(int argc, char *argv[]) +{ + int ret; + int i; + pthread_t self; + + mainthread = pthread_self(); + + if (SIG_ERR == signal(SIGUSR1, sighandler)) + err(EXIT_FAILURE, "signal"); + + for (i = 0; i < NTHREAD; i++) { + ret = pthread_create(&th[i].id, NULL, f, &th[i]); + if (ret) + err(EXIT_FAILURE, "pthread_create"); + } + + sched_yield(); + + self = pthread_self(); + if (self != mainthread) + errx(EXIT_FAILURE, "thread id changed"); + + for (i = 0; i < NTHREAD; i++) { + ret = pthread_kill(th[i].id, SIGUSR1); + if (ret) + err(EXIT_FAILURE, "pthread_kill"); + } + + for (i = 0; i < NTHREAD; i++) { + ret = pthread_join(th[i].id, NULL); + if (ret) + err(EXIT_FAILURE, "pthread_join"); + } + + exit(EXIT_SUCCESS); +} +