summaryrefslogtreecommitdiff
path: root/regress/lib/libpthread/pthread_once/pthread_once.c
blob: 7a1d103f14e2a2c79698a94a433ba9776e3ed89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*	$OpenBSD: pthread_once.c,v 1.1 2017/10/16 01:46:09 guenther Exp $ */

/*
 * Scott Cheloha <scottcheloha@gmail.com>, 2017. Public Domain.
 */

#include <pthread.h>
#include <unistd.h>

#include "test.h"

pthread_once_t once_control = PTHREAD_ONCE_INIT;
int done = 0;

void
init_routine(void)
{
	static int first = 1;

	if (first) {
		first = 0;
		CHECKr(pthread_cancel(pthread_self()));
		pthread_testcancel();
	}
	done = 1;
}

void *
thread_main(void *arg)
{
	pthread_once(&once_control, init_routine);
	return NULL;
}

int
main(int argc, char **argv)
{
	pthread_t tid;

	CHECKr(pthread_create(&tid, NULL, thread_main, NULL));
	CHECKr(pthread_join(tid, NULL));
	ASSERT(!done);
	alarm(5);	/* if this rings we are probably deadlocked */
	CHECKr(pthread_once(&once_control, init_routine));
	alarm(0);
	ASSERT(done);
	SUCCEED;
}