Add condition variable tests from Nathan's testsuite.
This commit is contained in:
parent
13eede9b17
commit
afab0f25a7
@ -1,5 +1,5 @@
|
||||
# $NetBSD: Makefile,v 1.3 2003/01/30 18:23:09 thorpej Exp $
|
||||
# $NetBSD: Makefile,v 1.4 2003/01/30 18:53:44 thorpej Exp $
|
||||
|
||||
SUBDIR+= barrier1 mutex1 mutex2 mutex3 mutex4 sem
|
||||
SUBDIR+= barrier1 cond1 cond2 cond3 cond4 mutex1 mutex2 mutex3 mutex4 sem
|
||||
|
||||
.include <bsd.subdir.mk>
|
||||
|
15
regress/lib/libpthread/cond1/Makefile
Normal file
15
regress/lib/libpthread/cond1/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:53:45 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= cond1
|
||||
SRCS= cond1.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./cond1
|
||||
|
||||
.include <bsd.prog.mk>
|
74
regress/lib/libpthread/cond1/cond1.c
Normal file
74
regress/lib/libpthread/cond1/cond1.c
Normal file
@ -0,0 +1,74 @@
|
||||
/* $NetBSD: cond1.c,v 1.1 2003/01/30 18:53:46 thorpej Exp $ */
|
||||
|
||||
#include <err.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void *threadfunc(void *arg);
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int x,ret;
|
||||
pthread_t new;
|
||||
void *joinval;
|
||||
int sharedval;
|
||||
|
||||
printf("1: condition variable test 1\n");
|
||||
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
pthread_cond_init(&cond, NULL);
|
||||
|
||||
x = 20;
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
sharedval = 1;
|
||||
|
||||
ret = pthread_create(&new, NULL, threadfunc, &sharedval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
|
||||
printf("1: Before waiting.\n");
|
||||
do {
|
||||
sleep(2);
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
printf("1: After waiting, in loop.\n");
|
||||
} while (sharedval != 0);
|
||||
|
||||
printf("1: After the loop.\n");
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
printf("1: After releasing the mutex.\n");
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
printf("1: Thread joined.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
threadfunc(void *arg)
|
||||
{
|
||||
int *share = (int *) arg;
|
||||
|
||||
printf("2: Second thread.\n");
|
||||
|
||||
printf("2: Locking mutex\n");
|
||||
pthread_mutex_lock(&mutex);
|
||||
printf("2: Got mutex.\n");
|
||||
printf("Shared value: %d. Changing to 0.\n", *share);
|
||||
*share = 0;
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
pthread_cond_signal(&cond);
|
||||
|
||||
return NULL;
|
||||
}
|
15
regress/lib/libpthread/cond2/Makefile
Normal file
15
regress/lib/libpthread/cond2/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:53:46 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= cond2
|
||||
SRCS= cond2.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./cond2
|
||||
|
||||
.include <bsd.prog.mk>
|
75
regress/lib/libpthread/cond2/cond2.c
Normal file
75
regress/lib/libpthread/cond2/cond2.c
Normal file
@ -0,0 +1,75 @@
|
||||
/* $NetBSD: cond2.c,v 1.1 2003/01/30 18:53:46 thorpej Exp $ */
|
||||
|
||||
#include <err.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void *threadfunc(void *arg);
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int x,ret;
|
||||
pthread_t new;
|
||||
void *joinval;
|
||||
int sharedval;
|
||||
|
||||
printf("1: condition variable test 2\n");
|
||||
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
pthread_cond_init(&cond, NULL);
|
||||
|
||||
x = 20;
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
sharedval = 1;
|
||||
|
||||
ret = pthread_create(&new, NULL, threadfunc, &sharedval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
|
||||
printf("1: Before waiting.\n");
|
||||
do {
|
||||
sleep(2);
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
printf("1: After waiting, in loop.\n");
|
||||
} while (sharedval != 0);
|
||||
|
||||
printf("1: After the loop.\n");
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
printf("1: After releasing the mutex.\n");
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
printf("1: Thread joined.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
threadfunc(void *arg)
|
||||
{
|
||||
int *share = (int *) arg;
|
||||
|
||||
printf("2: Second thread.\n");
|
||||
|
||||
printf("2: Locking mutex\n");
|
||||
pthread_mutex_lock(&mutex);
|
||||
printf("2: Got mutex.\n");
|
||||
printf("Shared value: %d. Changing to 0.\n", *share);
|
||||
*share = 0;
|
||||
|
||||
/* Signal first, then unlock, for a different test than #1. */
|
||||
pthread_cond_signal(&cond);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return NULL;
|
||||
}
|
15
regress/lib/libpthread/cond3/Makefile
Normal file
15
regress/lib/libpthread/cond3/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:53:47 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= cond3
|
||||
SRCS= cond3.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./cond3
|
||||
|
||||
.include <bsd.prog.mk>
|
72
regress/lib/libpthread/cond3/cond3.c
Normal file
72
regress/lib/libpthread/cond3/cond3.c
Normal file
@ -0,0 +1,72 @@
|
||||
/* $NetBSD: cond3.c,v 1.1 2003/01/30 18:53:47 thorpej Exp $ */
|
||||
|
||||
#include <err.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void *threadfunc(void *arg);
|
||||
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int x,ret;
|
||||
pthread_t new;
|
||||
void *joinval;
|
||||
int sharedval;
|
||||
|
||||
printf("1: condition variable test 3\n");
|
||||
|
||||
x = 20;
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
sharedval = 1;
|
||||
|
||||
ret = pthread_create(&new, NULL, threadfunc, &sharedval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
|
||||
printf("1: Before waiting.\n");
|
||||
do {
|
||||
sleep(2);
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
printf("1: After waiting, in loop.\n");
|
||||
} while (sharedval != 0);
|
||||
|
||||
printf("1: After the loop.\n");
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
printf("1: After releasing the mutex.\n");
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
printf("1: Thread joined.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
threadfunc(void *arg)
|
||||
{
|
||||
int *share = (int *) arg;
|
||||
|
||||
printf("2: Second thread.\n");
|
||||
|
||||
printf("2: Locking mutex\n");
|
||||
pthread_mutex_lock(&mutex);
|
||||
printf("2: Got mutex.\n");
|
||||
printf("Shared value: %d. Changing to 0.\n", *share);
|
||||
*share = 0;
|
||||
|
||||
/* Signal first, then unlock, for a different test than #1. */
|
||||
pthread_cond_signal(&cond);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return NULL;
|
||||
}
|
15
regress/lib/libpthread/cond4/Makefile
Normal file
15
regress/lib/libpthread/cond4/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# $NetBSD: Makefile,v 1.1 2003/01/30 18:53:48 thorpej Exp $
|
||||
|
||||
WARNS=1
|
||||
|
||||
PROG= cond4
|
||||
SRCS= cond4.c
|
||||
|
||||
LDADD= -lpthread
|
||||
|
||||
NOMAN=
|
||||
|
||||
regress:
|
||||
./cond4
|
||||
|
||||
.include <bsd.prog.mk>
|
85
regress/lib/libpthread/cond4/cond4.c
Normal file
85
regress/lib/libpthread/cond4/cond4.c
Normal file
@ -0,0 +1,85 @@
|
||||
/* $NetBSD: cond4.c,v 1.1 2003/01/30 18:53:48 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;
|
||||
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
||||
int count, total, toggle;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
pthread_t new;
|
||||
void *joinval;
|
||||
int sharedval;
|
||||
|
||||
printf("1: condition variable test 4\n");
|
||||
|
||||
ret = pthread_mutex_lock(&mutex);
|
||||
if (ret)
|
||||
err(1, "pthread_mutex_lock(1)");
|
||||
|
||||
count = 5000000;
|
||||
toggle = 0;
|
||||
|
||||
ret = pthread_create(&new, NULL, threadfunc, &sharedval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_create");
|
||||
|
||||
printf("1: Before waiting.\n");
|
||||
while (count>0) {
|
||||
count--;
|
||||
total++;
|
||||
toggle = 1;
|
||||
pthread_cond_signal(&cond);
|
||||
do {
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
} while (toggle != 0);
|
||||
}
|
||||
printf("1: After the loop.\n");
|
||||
|
||||
toggle = 1;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
pthread_cond_signal(&cond);
|
||||
|
||||
printf("1: After releasing the mutex.\n");
|
||||
ret = pthread_join(new, &joinval);
|
||||
if (ret != 0)
|
||||
err(1, "pthread_join");
|
||||
|
||||
printf("1: Thread joined. Final count = %d, total = %d\n",
|
||||
count, total);
|
||||
assert(count == 0);
|
||||
assert(total == 5000000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *threadfunc(void *arg)
|
||||
{
|
||||
#if 0
|
||||
int *share = (int *) arg;
|
||||
#endif
|
||||
|
||||
printf("2: Second thread.\n");
|
||||
pthread_mutex_lock(&mutex);
|
||||
while (count>0) {
|
||||
count--;
|
||||
total++;
|
||||
toggle = 0;
|
||||
pthread_cond_signal(&cond);
|
||||
do {
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
} while (toggle != 1);
|
||||
}
|
||||
printf("2: After the loop.\n");
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return NULL;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user