Add mutex tests from Nathan's testsuite.
This commit is contained in:
parent
eea2fc320e
commit
9b6c031a4f
@ -1,5 +1,5 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/20 20:12:18 christos Exp $
|
||||
# $NetBSD: Makefile,v 1.2 2003/01/30 18:05:25 thorpej Exp $
|
||||
|
||||
SUBDIR+= sem
|
||||
SUBDIR+= mutex1 mutex2 mutex3 mutex4 sem
|
||||
|
||||
.include <bsd.subdir.mk>
|
||||
|
15
regress/lib/libpthread/mutex1/Makefile
Normal file
15
regress/lib/libpthread/mutex1/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:05:25 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= mutex1
|
||||
SRCS= mutex1.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./mutex1
|
||||
|
||||
.include <bsd.prog.mk>
|
66
regress/lib/libpthread/mutex1/mutex1.c
Normal file
66
regress/lib/libpthread/mutex1/mutex1.c
Normal file
@ -0,0 +1,66 @@
|
||||
/* $NetBSD: mutex1.c,v 1.1 2003/01/30 18:05:26 thorpej Exp $ */
|
||||
|
||||
#include <assert.h>
|
||||
#include <err.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void *threadfunc(void *arg);
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int x,ret;
|
||||
pthread_t new;
|
||||
void *joinval;
|
||||
|
||||
printf("1: Mutex-test 1\n");
|
||||
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
x = 1;
|
||||
pthread_mutex_lock(&mutex);
|
||||
ret = pthread_create(&new, NULL, threadfunc, &x);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
printf("1: Before changing the value.\n");
|
||||
sleep(2);
|
||||
x = 20;
|
||||
printf("1: Before releasing the mutex.\n");
|
||||
sleep(2);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
printf("1: After releasing the mutex.\n");
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
printf("1: Thread joined. X was %d. Return value (int) was %d\n",
|
||||
x, *(int *)joinval);
|
||||
assert(x == 21);
|
||||
assert(*(int *)joinval == 21);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
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);
|
||||
assert(*param == 20);
|
||||
(*param)++;
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return param;
|
||||
}
|
15
regress/lib/libpthread/mutex2/Makefile
Normal file
15
regress/lib/libpthread/mutex2/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:05:26 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= mutex2
|
||||
SRCS= mutex2.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./mutex2
|
||||
|
||||
.include <bsd.prog.mk>
|
81
regress/lib/libpthread/mutex2/mutex2.c
Normal file
81
regress/lib/libpthread/mutex2/mutex2.c
Normal file
@ -0,0 +1,81 @@
|
||||
/* $NetBSD: mutex2.c,v 1.1 2003/01/30 18:05:26 thorpej Exp $ */
|
||||
|
||||
#include <assert.h>
|
||||
#include <err.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void *threadfunc(void *arg);
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
int x;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int count, count2, ret;
|
||||
pthread_t new;
|
||||
void *joinval;
|
||||
|
||||
printf("1: Mutex-test 2\n");
|
||||
|
||||
ret = pthread_mutex_init(&mutex, NULL);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_init");
|
||||
x = 0;
|
||||
count = count2 = 10000000;
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_lock (1)");
|
||||
ret = pthread_create(&new, NULL, threadfunc, &count2);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
|
||||
printf("1: Thread %p\n", pthread_self());
|
||||
ret = pthread_mutex_unlock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_unlock(1)");
|
||||
|
||||
|
||||
while (count--) {
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_lock(2)");
|
||||
x++;
|
||||
ret = pthread_mutex_unlock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_unlock(2)");
|
||||
}
|
||||
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
|
||||
x, (long)joinval);
|
||||
assert(x == 20000000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
threadfunc(void *arg)
|
||||
{
|
||||
int count = *(int *)arg;
|
||||
int ret;
|
||||
|
||||
printf("2: Second thread (%p). Count is %d\n", pthread_self(), count);
|
||||
|
||||
while (count--) {
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_lock(3)");
|
||||
x++;
|
||||
ret = pthread_mutex_unlock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_unlock(3)");
|
||||
}
|
||||
|
||||
return (void *)count;
|
||||
}
|
15
regress/lib/libpthread/mutex3/Makefile
Normal file
15
regress/lib/libpthread/mutex3/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:05:26 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= mutex3
|
||||
SRCS= mutex3.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./mutex3
|
||||
|
||||
.include <bsd.prog.mk>
|
78
regress/lib/libpthread/mutex3/mutex3.c
Normal file
78
regress/lib/libpthread/mutex3/mutex3.c
Normal file
@ -0,0 +1,78 @@
|
||||
/* $NetBSD: mutex3.c,v 1.1 2003/01/30 18:05:26 thorpej Exp $ */
|
||||
|
||||
#include <assert.h>
|
||||
#include <err.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void *threadfunc(void *arg);
|
||||
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
int x;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int count, count2, ret;
|
||||
pthread_t new;
|
||||
void *joinval;
|
||||
|
||||
printf("1: Mutex-test 3\n");
|
||||
|
||||
x = 0;
|
||||
count = count2 = 10000000;
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_lock (1)");
|
||||
ret = pthread_create(&new, NULL, threadfunc, &count2);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
|
||||
printf("1: Thread %p\n", pthread_self());
|
||||
ret = pthread_mutex_unlock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_unlock(1)");
|
||||
|
||||
|
||||
while (count--) {
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_lock(2)");
|
||||
x++;
|
||||
ret = pthread_mutex_unlock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_unlock(2)");
|
||||
}
|
||||
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
|
||||
x, (long)joinval);
|
||||
assert(x == 20000000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
threadfunc(void *arg)
|
||||
{
|
||||
int count = *(int *)arg;
|
||||
int ret;
|
||||
|
||||
printf("2: Second thread (%p). Count is %d\n", pthread_self(), count);
|
||||
|
||||
while (count--) {
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_lock(3)");
|
||||
x++;
|
||||
ret = pthread_mutex_unlock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_unlock(3)");
|
||||
}
|
||||
|
||||
return (void *)count;
|
||||
}
|
15
regress/lib/libpthread/mutex4/Makefile
Normal file
15
regress/lib/libpthread/mutex4/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:05:27 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= mutex4
|
||||
SRCS= mutex4.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./mutex4
|
||||
|
||||
.include <bsd.prog.mk>
|
83
regress/lib/libpthread/mutex4/mutex4.c
Normal file
83
regress/lib/libpthread/mutex4/mutex4.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* $NetBSD: mutex4.c,v 1.1 2003/01/30 18:05:27 thorpej Exp $ */
|
||||
|
||||
#include <assert.h>
|
||||
#include <err.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void *threadfunc(void *arg);
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int x,ret;
|
||||
pthread_t new;
|
||||
pthread_mutexattr_t mattr;
|
||||
void *joinval;
|
||||
|
||||
printf("1: Mutex-test 4\n");
|
||||
|
||||
pthread_mutexattr_init(&mattr);
|
||||
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
|
||||
|
||||
pthread_mutex_init(&mutex, &mattr);
|
||||
|
||||
pthread_mutexattr_destroy(&mattr);
|
||||
|
||||
x = 1;
|
||||
pthread_mutex_lock(&mutex);
|
||||
ret = pthread_create(&new, NULL, threadfunc, &x);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
|
||||
printf("1: Before recursively acquiring the mutex.\n");
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_mutex_lock recursive");
|
||||
|
||||
printf("1: Before releasing the mutex once.\n");
|
||||
sleep(2);
|
||||
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_mutex_unlock(&mutex);
|
||||
printf("1: After releasing the mutex twice.\n");
|
||||
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
printf("1: Thread joined. X was %d. Return value (int) was %d\n",
|
||||
x, *(int *)joinval);
|
||||
assert(x == 21);
|
||||
assert(*(int *)joinval == 21);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
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);
|
||||
(*param)++;
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return param;
|
||||
}
|
Loading…
Reference in New Issue
Block a user