1272343Sngie/* $NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2008 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\
31272343Sngie The NetBSD Foundation, inc. All rights reserved.");
32272343Sngie__RCSID("$NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
33272343Sngie
34272343Sngie#include <pthread.h>
35272343Sngie#include <signal.h>
36272343Sngie#include <stdio.h>
37272343Sngie#include <stdlib.h>
38272343Sngie
39272343Sngie#include <atf-c.h>
40272343Sngie
41272343Sngie#include "h_common.h"
42272343Sngie
43272343Sngiestatic pthread_once_t once = PTHREAD_ONCE_INIT;
44272343Sngiestatic pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45272343Sngiestatic int x;
46272343Sngie
47272343Sngie#define NTHREADS 25
48272343Sngie
49276478Sngie#ifdef __FreeBSD__
50276478Sngie#include <sys/time.h>
51276478Sngie#endif
52276478Sngie
53272343Sngiestatic void
54272343Sngieofunc(void)
55272343Sngie{
56272343Sngie	printf("Variable x has value %d\n", x);
57272343Sngie	x++;
58272343Sngie}
59272343Sngie
60272343SngieATF_TC(once1);
61272343SngieATF_TC_HEAD(once1, tc)
62272343Sngie{
63272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
64272343Sngie}
65272343SngieATF_TC_BODY(once1, tc)
66272343Sngie{
67272343Sngie
68272343Sngie	printf("1: Test 1 of pthread_once()\n");
69272343Sngie
70272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
71272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
72272343Sngie
73272343Sngie	printf("1: X has value %d\n",x );
74272343Sngie	ATF_REQUIRE_EQ(x, 1);
75272343Sngie}
76272343Sngie
77272343Sngiestatic void
78272343Sngieonce2_ofunc(void)
79272343Sngie{
80272343Sngie	x++;
81272343Sngie	printf("ofunc: Variable x has value %d\n", x);
82272343Sngie	x++;
83272343Sngie}
84272343Sngie
85272343Sngiestatic void *
86272343Sngieonce2_threadfunc(void *arg)
87272343Sngie{
88272343Sngie	int num;
89272343Sngie
90272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
91272343Sngie
92272343Sngie	num = *(int *)arg;
93272343Sngie	printf("Thread %d sees x with value %d\n", num, x);
94272343Sngie	ATF_REQUIRE_EQ(x, 2);
95272343Sngie
96272343Sngie	return NULL;
97272343Sngie}
98272343Sngie
99272343SngieATF_TC(once2);
100272343SngieATF_TC_HEAD(once2, tc)
101272343Sngie{
102272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
103272343Sngie}
104272343SngieATF_TC_BODY(once2, tc)
105272343Sngie{
106272343Sngie	pthread_t  threads[NTHREADS];
107272343Sngie	int id[NTHREADS];
108272343Sngie	int i;
109272343Sngie
110272343Sngie	printf("1: Test 2 of pthread_once()\n");
111272343Sngie
112272343Sngie	for (i=0; i < NTHREADS; i++) {
113272343Sngie		id[i] = i;
114272343Sngie		PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
115272343Sngie	}
116272343Sngie
117272343Sngie	for (i=0; i < NTHREADS; i++)
118272343Sngie		PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
119272343Sngie
120272343Sngie	printf("1: X has value %d\n",x );
121272343Sngie	ATF_REQUIRE_EQ(x, 2);
122272343Sngie}
123272343Sngie
124272343Sngiestatic void
125272343Sngieonce3_cleanup(void *m)
126272343Sngie{
127272343Sngie	pthread_mutex_t *mu = m;
128272343Sngie
129272343Sngie	PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
130272343Sngie}
131272343Sngie
132272343Sngiestatic void
133272343Sngieonce3_ofunc(void)
134272343Sngie{
135272343Sngie	pthread_testcancel();
136272343Sngie}
137272343Sngie
138272343Sngiestatic void *
139272343Sngieonce3_threadfunc(void *arg)
140272343Sngie{
141272343Sngie	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
142272343Sngie	pthread_cleanup_push(once3_cleanup, &mutex);
143272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
144272343Sngie	pthread_cleanup_pop(1);
145272343Sngie
146272343Sngie	return NULL;
147272343Sngie}
148272343Sngie
149272343Sngiestatic void
150272343Sngiehandler(int sig, siginfo_t *info, void *ctx)
151272343Sngie{
152272343Sngie	atf_tc_fail("Signal handler was called; "
153272343Sngie		"main thread deadlocked in pthread_once()");
154272343Sngie}
155272343Sngie
156272343SngieATF_TC(once3);
157272343SngieATF_TC_HEAD(once3, tc)
158272343Sngie{
159272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
160272343Sngie}
161272343SngieATF_TC_BODY(once3, tc)
162272343Sngie{
163272343Sngie	pthread_t thread;
164272343Sngie	struct sigaction act;
165272343Sngie	struct itimerval it;
166272343Sngie	printf("Test 3 of pthread_once() (test versus cancellation)\n");
167272343Sngie
168272343Sngie	act.sa_sigaction = handler;
169272343Sngie	sigemptyset(&act.sa_mask);
170272343Sngie	act.sa_flags = SA_SIGINFO;
171272343Sngie	sigaction(SIGALRM, &act, NULL);
172272343Sngie
173272343Sngie	timerclear(&it.it_value);
174272343Sngie	it.it_value.tv_usec = 500000;
175272343Sngie	timerclear(&it.it_interval);
176272343Sngie	setitimer(ITIMER_REAL, &it, NULL);
177272343Sngie
178272343Sngie	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
179272343Sngie	PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
180272343Sngie	PTHREAD_REQUIRE(pthread_cancel(thread));
181272343Sngie	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
182272343Sngie	PTHREAD_REQUIRE(pthread_join(thread, NULL));
183272343Sngie
184272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
185272343Sngie
186272343Sngie	/* Cancel timer */
187272343Sngie	timerclear(&it.it_value);
188272343Sngie	setitimer(ITIMER_REAL, &it, NULL);
189272343Sngie
190272343Sngie	printf("Test succeeded\n");
191272343Sngie}
192272343Sngie
193272343SngieATF_TP_ADD_TCS(tp)
194272343Sngie{
195272343Sngie	ATF_TP_ADD_TC(tp, once1);
196272343Sngie	ATF_TP_ADD_TC(tp, once2);
197272343Sngie	ATF_TP_ADD_TC(tp, once3);
198272343Sngie
199272343Sngie	return atf_no_error();
200272343Sngie}
201