diff options
author | Martin Pieuchot <mpi@cvs.openbsd.org> | 2012-01-04 17:36:41 +0000 |
---|---|---|
committer | Martin Pieuchot <mpi@cvs.openbsd.org> | 2012-01-04 17:36:41 +0000 |
commit | c7c6a1b80cc3398a467026bf3319857efae5159c (patch) | |
tree | 3bf4ad57d2702095d2ed7adca4415ea799940008 | |
parent | fe87a8c7b7d990cbf83e0df2a8d8b234d9b834b7 (diff) |
Basic semaphore tests
ok guenther@
10 files changed, 215 insertions, 0 deletions
diff --git a/regress/lib/libpthread/semaphore/Makefile b/regress/lib/libpthread/semaphore/Makefile new file mode 100644 index 00000000000..a9a7dad7b02 --- /dev/null +++ b/regress/lib/libpthread/semaphore/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2012/01/04 17:36:40 mpi Exp $ + +SUBDIR = sem_destroy sem_getvalue sem_trywait sem_wait + +.include <bsd.subdir.mk> diff --git a/regress/lib/libpthread/semaphore/Makefile.inc b/regress/lib/libpthread/semaphore/Makefile.inc new file mode 100644 index 00000000000..d10ca560448 --- /dev/null +++ b/regress/lib/libpthread/semaphore/Makefile.inc @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile.inc,v 1.1.1.1 2012/01/04 17:36:40 mpi Exp $ + +CFLAGS += -I${.CURDIR}/../../include + +.include "${.CURDIR}/../../Makefile.inc" diff --git a/regress/lib/libpthread/semaphore/sem_destroy/Makefile b/regress/lib/libpthread/semaphore/sem_destroy/Makefile new file mode 100644 index 00000000000..a486f17025e --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_destroy/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1.1.1 2012/01/04 17:36:40 mpi Exp $ + +PROG= sem_destroy + +.include <bsd.regress.mk> diff --git a/regress/lib/libpthread/semaphore/sem_destroy/sem_destroy.c b/regress/lib/libpthread/semaphore/sem_destroy/sem_destroy.c new file mode 100644 index 00000000000..b38965260f3 --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_destroy/sem_destroy.c @@ -0,0 +1,75 @@ +/* $OpenBSD: sem_destroy.c,v 1.1.1.1 2012/01/04 17:36:40 mpi Exp $ */ +/* + * Martin Pieuchot <mpi@openbsd.org>, 2011. Public Domain. + */ + +#include <errno.h> +#include <unistd.h> +#include <pthread.h> +#include <semaphore.h> +#include "test.h" + +int val; +sem_t cons_sem; +sem_t prod_sem; + +void *producer(void *arg); +void *consumer(void *arg); + +int +main(int argc, char **argv) +{ + pthread_t prod_th, cons_th; + long counter = 4; + + CHECKn(sem_destroy(&cons_sem)); + ASSERT(errno == EINVAL); + + val = 0; + + CHECKr(sem_init(&cons_sem, 0, 0)); + CHECKr(sem_init(&prod_sem, 0, 1)); + + CHECKr(pthread_create(&prod_th, NULL, producer, &counter)); + CHECKr(pthread_create(&cons_th, NULL, consumer, &counter)); + + CHECKr(pthread_join(prod_th, NULL)); + CHECKr(pthread_join(cons_th, NULL)); + + pthread_exit(NULL); + + CHECKr(sem_destroy(&prod_sem)); + CHECKr(sem_destroy(&cons_sem)); + + SUCCEED; +} + +void * +producer(void *arg) +{ + long *counter = arg; + int i; + + for (i = 0; i < *counter; i++) { + sem_wait(&prod_sem); + val++; + sem_post(&cons_sem); + } + + return (NULL); +} + +void * +consumer(void *arg) +{ + long *counter = arg; + int i; + + for (i = 0; i < *counter; i++) { + sem_wait(&cons_sem); + val--; + sem_post(&prod_sem); + } + + return (NULL); +} diff --git a/regress/lib/libpthread/semaphore/sem_getvalue/Makefile b/regress/lib/libpthread/semaphore/sem_getvalue/Makefile new file mode 100644 index 00000000000..456d24956f9 --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_getvalue/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1.1.1 2012/01/04 17:36:40 mpi Exp $ + +PROG= sem_getvalue + +.include <bsd.regress.mk> diff --git a/regress/lib/libpthread/semaphore/sem_getvalue/sem_getvalue.c b/regress/lib/libpthread/semaphore/sem_getvalue/sem_getvalue.c new file mode 100644 index 00000000000..8f9cf62d782 --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_getvalue/sem_getvalue.c @@ -0,0 +1,27 @@ +/* $OpenBSD: sem_getvalue.c,v 1.1 2012/01/04 17:36:40 mpi Exp $ */ +/* + * Martin Pieuchot <mpi@openbsd.org>, 2011. Public Domain. + */ + +#include <unistd.h> +#include <semaphore.h> +#include "test.h" + +int +main(int argc, char **argv) +{ + sem_t sem; + int val; + + CHECKr(sem_init(&sem, 0, 0)); + CHECKe(sem_getvalue(&sem, &val)); + ASSERT(val == 0); + + CHECKr(sem_post(&sem)); + CHECKe(sem_getvalue(&sem, &val)); + ASSERT(val == 1); + + CHECKe(sem_destroy(&sem)); + + SUCCEED; +} diff --git a/regress/lib/libpthread/semaphore/sem_trywait/Makefile b/regress/lib/libpthread/semaphore/sem_trywait/Makefile new file mode 100644 index 00000000000..50d9851c698 --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_trywait/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1.1.1 2012/01/04 17:36:40 mpi Exp $ + +PROG= sem_trywait + +.include <bsd.regress.mk> diff --git a/regress/lib/libpthread/semaphore/sem_trywait/sem_trywait.c b/regress/lib/libpthread/semaphore/sem_trywait/sem_trywait.c new file mode 100644 index 00000000000..2430c1386d7 --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_trywait/sem_trywait.c @@ -0,0 +1,34 @@ +/* $OpenBSD: sem_trywait.c,v 1.1 2012/01/04 17:36:40 mpi Exp $ */ +/* + * Martin Pieuchot <mpi@openbsd.org>, 2011. Public Domain. + */ + +#include <errno.h> +#include <unistd.h> +#include <semaphore.h> +#include "test.h" + +int +main(int argc, char **argv) +{ + sem_t sem; + int val; + + CHECKn(sem_trywait(&sem)); + ASSERT(errno == EINVAL); + + CHECKr(sem_init(&sem, 0, 0)); + + CHECKn(sem_trywait(&sem)); + ASSERT(errno == EAGAIN); + + CHECKr(sem_post(&sem)); + CHECKr(sem_trywait(&sem)); + + CHECKe(sem_getvalue(&sem, &val)); + ASSERT(val == 0); + + CHECKe(sem_destroy(&sem)); + + SUCCEED; +} diff --git a/regress/lib/libpthread/semaphore/sem_wait/Makefile b/regress/lib/libpthread/semaphore/sem_wait/Makefile new file mode 100644 index 00000000000..947b4af1d31 --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_wait/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1.1.1 2012/01/04 17:36:40 mpi Exp $ + +PROG= sem_wait + +.include <bsd.regress.mk> diff --git a/regress/lib/libpthread/semaphore/sem_wait/sem_wait.c b/regress/lib/libpthread/semaphore/sem_wait/sem_wait.c new file mode 100644 index 00000000000..8da14c6f507 --- /dev/null +++ b/regress/lib/libpthread/semaphore/sem_wait/sem_wait.c @@ -0,0 +1,49 @@ +/* $OpenBSD: sem_wait.c,v 1.1 2012/01/04 17:36:40 mpi Exp $ */ +/* + * Martin Pieuchot <mpi@openbsd.org>, 2011. Public Domain. + */ + +#include <errno.h> +#include <unistd.h> +#include <semaphore.h> +#include <pthread.h> +#include "test.h" + + +void *waiter(void *arg); + +int +main(int argc, char **argv) +{ + pthread_t th; + sem_t sem; + + CHECKn(sem_wait(&sem)); + ASSERT(errno == EINVAL); + + CHECKr(sem_init(&sem, 0, 0)); + + CHECKr(pthread_create(&th, NULL, waiter, &sem)); + + sleep(1); + + CHECKn(sem_destroy(&sem)); + ASSERT(errno == EBUSY); + + CHECKr(sem_post(&sem)); + CHECKr(pthread_join(th, NULL)); + + CHECKe(sem_destroy(&sem)); + + SUCCEED; +} + +void * +waiter(void *arg) +{ + sem_t *semp = arg; + + CHECKr(sem_wait(semp)); + + return (NULL); +} |