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
34314818Sngie#ifdef __FreeBSD__
35314818Sngie#include <sys/time.h> /* For itimer*, etc. */
36314818Sngie#endif
37272343Sngie#include <pthread.h>
38272343Sngie#include <signal.h>
39272343Sngie#include <stdio.h>
40272343Sngie#include <stdlib.h>
41272343Sngie
42272343Sngie#include <atf-c.h>
43272343Sngie
44272343Sngie#include "h_common.h"
45272343Sngie
46272343Sngiestatic pthread_once_t once = PTHREAD_ONCE_INIT;
47272343Sngiestatic pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
48272343Sngiestatic int x;
49272343Sngie
50272343Sngie#define NTHREADS 25
51272343Sngie
52272343Sngiestatic void
53272343Sngieofunc(void)
54272343Sngie{
55272343Sngie	printf("Variable x has value %d\n", x);
56272343Sngie	x++;
57272343Sngie}
58272343Sngie
59272343SngieATF_TC(once1);
60272343SngieATF_TC_HEAD(once1, tc)
61272343Sngie{
62272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
63272343Sngie}
64272343SngieATF_TC_BODY(once1, tc)
65272343Sngie{
66272343Sngie
67272343Sngie	printf("1: Test 1 of pthread_once()\n");
68272343Sngie
69272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
70272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
71272343Sngie
72272343Sngie	printf("1: X has value %d\n",x );
73272343Sngie	ATF_REQUIRE_EQ(x, 1);
74272343Sngie}
75272343Sngie
76272343Sngiestatic void
77272343Sngieonce2_ofunc(void)
78272343Sngie{
79272343Sngie	x++;
80272343Sngie	printf("ofunc: Variable x has value %d\n", x);
81272343Sngie	x++;
82272343Sngie}
83272343Sngie
84272343Sngiestatic void *
85272343Sngieonce2_threadfunc(void *arg)
86272343Sngie{
87272343Sngie	int num;
88272343Sngie
89272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
90272343Sngie
91272343Sngie	num = *(int *)arg;
92272343Sngie	printf("Thread %d sees x with value %d\n", num, x);
93272343Sngie	ATF_REQUIRE_EQ(x, 2);
94272343Sngie
95272343Sngie	return NULL;
96272343Sngie}
97272343Sngie
98272343SngieATF_TC(once2);
99272343SngieATF_TC_HEAD(once2, tc)
100272343Sngie{
101272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
102272343Sngie}
103272343SngieATF_TC_BODY(once2, tc)
104272343Sngie{
105272343Sngie	pthread_t  threads[NTHREADS];
106272343Sngie	int id[NTHREADS];
107272343Sngie	int i;
108272343Sngie
109272343Sngie	printf("1: Test 2 of pthread_once()\n");
110272343Sngie
111272343Sngie	for (i=0; i < NTHREADS; i++) {
112272343Sngie		id[i] = i;
113272343Sngie		PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
114272343Sngie	}
115272343Sngie
116272343Sngie	for (i=0; i < NTHREADS; i++)
117272343Sngie		PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
118272343Sngie
119272343Sngie	printf("1: X has value %d\n",x );
120272343Sngie	ATF_REQUIRE_EQ(x, 2);
121272343Sngie}
122272343Sngie
123272343Sngiestatic void
124272343Sngieonce3_cleanup(void *m)
125272343Sngie{
126272343Sngie	pthread_mutex_t *mu = m;
127272343Sngie
128272343Sngie	PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
129272343Sngie}
130272343Sngie
131272343Sngiestatic void
132272343Sngieonce3_ofunc(void)
133272343Sngie{
134272343Sngie	pthread_testcancel();
135272343Sngie}
136272343Sngie
137272343Sngiestatic void *
138272343Sngieonce3_threadfunc(void *arg)
139272343Sngie{
140272343Sngie	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
141272343Sngie	pthread_cleanup_push(once3_cleanup, &mutex);
142272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
143272343Sngie	pthread_cleanup_pop(1);
144272343Sngie
145272343Sngie	return NULL;
146272343Sngie}
147272343Sngie
148272343Sngiestatic void
149272343Sngiehandler(int sig, siginfo_t *info, void *ctx)
150272343Sngie{
151272343Sngie	atf_tc_fail("Signal handler was called; "
152272343Sngie		"main thread deadlocked in pthread_once()");
153272343Sngie}
154272343Sngie
155272343SngieATF_TC(once3);
156272343SngieATF_TC_HEAD(once3, tc)
157272343Sngie{
158272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
159272343Sngie}
160272343SngieATF_TC_BODY(once3, tc)
161272343Sngie{
162272343Sngie	pthread_t thread;
163272343Sngie	struct sigaction act;
164272343Sngie	struct itimerval it;
165272343Sngie	printf("Test 3 of pthread_once() (test versus cancellation)\n");
166272343Sngie
167272343Sngie	act.sa_sigaction = handler;
168272343Sngie	sigemptyset(&act.sa_mask);
169272343Sngie	act.sa_flags = SA_SIGINFO;
170272343Sngie	sigaction(SIGALRM, &act, NULL);
171272343Sngie
172272343Sngie	timerclear(&it.it_value);
173272343Sngie	it.it_value.tv_usec = 500000;
174272343Sngie	timerclear(&it.it_interval);
175272343Sngie	setitimer(ITIMER_REAL, &it, NULL);
176272343Sngie
177272343Sngie	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
178272343Sngie	PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
179272343Sngie	PTHREAD_REQUIRE(pthread_cancel(thread));
180272343Sngie	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
181272343Sngie	PTHREAD_REQUIRE(pthread_join(thread, NULL));
182272343Sngie
183272343Sngie	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
184272343Sngie
185272343Sngie	/* Cancel timer */
186272343Sngie	timerclear(&it.it_value);
187272343Sngie	setitimer(ITIMER_REAL, &it, NULL);
188272343Sngie
189272343Sngie	printf("Test succeeded\n");
190272343Sngie}
191272343Sngie
192272343SngieATF_TP_ADD_TCS(tp)
193272343Sngie{
194272343Sngie	ATF_TP_ADD_TC(tp, once1);
195272343Sngie	ATF_TP_ADD_TC(tp, once2);
196272343Sngie	ATF_TP_ADD_TC(tp, once3);
197272343Sngie
198272343Sngie	return atf_no_error();
199272343Sngie}
200