Add tests for do-once control from Nathan's testsuite.

This commit is contained in:
thorpej 2003-01-30 19:31:59 +00:00
parent 34a458238c
commit a1c259efd5
5 changed files with 128 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# $NetBSD: Makefile,v 1.5 2003/01/30 18:57:06 thorpej Exp $
# $NetBSD: Makefile,v 1.6 2003/01/30 19:31:59 thorpej Exp $
SUBDIR+= barrier1 cond1 cond2 cond3 cond4 cond5 \
mutex1 mutex2 mutex3 mutex4 sem
mutex1 mutex2 mutex3 mutex4 once1 once2 sem
.include <bsd.subdir.mk>

View File

@ -0,0 +1,15 @@
# $NetBSD: Makefile,v 1.1 2003/01/30 19:31:59 thorpej Exp $
WARNS=1
PROG= once1
SRCS= once1.c
LDADD= -lpthread
NOMAN=
regress:
./once1
.include <bsd.prog.mk>

View File

@ -0,0 +1,35 @@
/* $NetBSD: once1.c,v 1.1 2003/01/30 19:32:00 thorpej Exp $ */
#include <assert.h>
#include <err.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_once_t once = PTHREAD_ONCE_INIT;
static int x;
void ofunc(void);
int
main(int argc, char *argv[])
{
printf("1: Test 1 of pthread_once()\n");
pthread_once(&once, ofunc);
pthread_once(&once, ofunc);
printf("1: X has value %d\n",x );
assert(x == 1);
return 0;
}
void
ofunc(void)
{
printf("Variable x has value %d\n", x);
x++;
}

View File

@ -0,0 +1,15 @@
# $NetBSD: Makefile,v 1.1 2003/01/30 19:32:00 thorpej Exp $
WARNS=1
PROG= once2
SRCS= once2.c
LDADD= -lpthread
NOMAN=
regress:
./once2
.include <bsd.prog.mk>

View File

@ -0,0 +1,61 @@
/* $NetBSD: once2.c,v 1.1 2003/01/30 19:32:00 thorpej Exp $ */
#include <assert.h>
#include <err.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_once_t once = PTHREAD_ONCE_INIT;
static int x;
#define NTHREADS 25
void ofunc(void);
void* threadfunc(void *);
int
main(int argc, char *argv[])
{
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_create(&threads[i], NULL, threadfunc, &id[i]);
}
for (i=0; i < NTHREADS; i++)
pthread_join(threads[i], NULL);
printf("1: X has value %d\n",x );
assert(x == 2);
return 0;
}
void
ofunc(void)
{
x++;
printf("ofunc: Variable x has value %d\n", x);
x++;
}
void *
threadfunc(void *arg)
{
int num;
pthread_once(&once, ofunc);
num = *(int *)arg;
printf("Thread %d sees x with value %d\n", num, x);
assert(x == 2);
return NULL;
}