xref: /minix3/tests/lib/libpthread/t_once.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include <sys/cdefs.h>
30*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
31*11be35a1SLionel Sambuc  The NetBSD Foundation, inc. All rights reserved.");
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <pthread.h>
35*11be35a1SLionel Sambuc #include <signal.h>
36*11be35a1SLionel Sambuc #include <stdio.h>
37*11be35a1SLionel Sambuc #include <stdlib.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include <atf-c.h>
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc #include "h_common.h"
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc static pthread_once_t once = PTHREAD_ONCE_INIT;
44*11be35a1SLionel Sambuc static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
45*11be35a1SLionel Sambuc static int x;
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc #define NTHREADS 25
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc static void
ofunc(void)50*11be35a1SLionel Sambuc ofunc(void)
51*11be35a1SLionel Sambuc {
52*11be35a1SLionel Sambuc 	printf("Variable x has value %d\n", x);
53*11be35a1SLionel Sambuc 	x++;
54*11be35a1SLionel Sambuc }
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc ATF_TC(once1);
ATF_TC_HEAD(once1,tc)57*11be35a1SLionel Sambuc ATF_TC_HEAD(once1, tc)
58*11be35a1SLionel Sambuc {
59*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
60*11be35a1SLionel Sambuc }
ATF_TC_BODY(once1,tc)61*11be35a1SLionel Sambuc ATF_TC_BODY(once1, tc)
62*11be35a1SLionel Sambuc {
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc 	printf("1: Test 1 of pthread_once()\n");
65*11be35a1SLionel Sambuc 
66*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
67*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc 	printf("1: X has value %d\n",x );
70*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(x, 1);
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc static void
once2_ofunc(void)74*11be35a1SLionel Sambuc once2_ofunc(void)
75*11be35a1SLionel Sambuc {
76*11be35a1SLionel Sambuc 	x++;
77*11be35a1SLionel Sambuc 	printf("ofunc: Variable x has value %d\n", x);
78*11be35a1SLionel Sambuc 	x++;
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc static void *
once2_threadfunc(void * arg)82*11be35a1SLionel Sambuc once2_threadfunc(void *arg)
83*11be35a1SLionel Sambuc {
84*11be35a1SLionel Sambuc 	int num;
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc 	num = *(int *)arg;
89*11be35a1SLionel Sambuc 	printf("Thread %d sees x with value %d\n", num, x);
90*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(x, 2);
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc 	return NULL;
93*11be35a1SLionel Sambuc }
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc ATF_TC(once2);
ATF_TC_HEAD(once2,tc)96*11be35a1SLionel Sambuc ATF_TC_HEAD(once2, tc)
97*11be35a1SLionel Sambuc {
98*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
99*11be35a1SLionel Sambuc }
ATF_TC_BODY(once2,tc)100*11be35a1SLionel Sambuc ATF_TC_BODY(once2, tc)
101*11be35a1SLionel Sambuc {
102*11be35a1SLionel Sambuc 	pthread_t  threads[NTHREADS];
103*11be35a1SLionel Sambuc 	int id[NTHREADS];
104*11be35a1SLionel Sambuc 	int i;
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc 	printf("1: Test 2 of pthread_once()\n");
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc 	for (i=0; i < NTHREADS; i++) {
109*11be35a1SLionel Sambuc 		id[i] = i;
110*11be35a1SLionel Sambuc 		PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
111*11be35a1SLionel Sambuc 	}
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 	for (i=0; i < NTHREADS; i++)
114*11be35a1SLionel Sambuc 		PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc 	printf("1: X has value %d\n",x );
117*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(x, 2);
118*11be35a1SLionel Sambuc }
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc static void
once3_cleanup(void * m)121*11be35a1SLionel Sambuc once3_cleanup(void *m)
122*11be35a1SLionel Sambuc {
123*11be35a1SLionel Sambuc 	pthread_mutex_t *mu = m;
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
126*11be35a1SLionel Sambuc }
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc static void
once3_ofunc(void)129*11be35a1SLionel Sambuc once3_ofunc(void)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc 	pthread_testcancel();
132*11be35a1SLionel Sambuc }
133*11be35a1SLionel Sambuc 
134*11be35a1SLionel Sambuc static void *
once3_threadfunc(void * arg)135*11be35a1SLionel Sambuc once3_threadfunc(void *arg)
136*11be35a1SLionel Sambuc {
137*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
138*11be35a1SLionel Sambuc 	pthread_cleanup_push(once3_cleanup, &mutex);
139*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
140*11be35a1SLionel Sambuc 	pthread_cleanup_pop(1);
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc 	return NULL;
143*11be35a1SLionel Sambuc }
144*11be35a1SLionel Sambuc 
145*11be35a1SLionel Sambuc static void
handler(int sig,siginfo_t * info,void * ctx)146*11be35a1SLionel Sambuc handler(int sig, siginfo_t *info, void *ctx)
147*11be35a1SLionel Sambuc {
148*11be35a1SLionel Sambuc 	atf_tc_fail("Signal handler was called; "
149*11be35a1SLionel Sambuc 		"main thread deadlocked in pthread_once()");
150*11be35a1SLionel Sambuc }
151*11be35a1SLionel Sambuc 
152*11be35a1SLionel Sambuc ATF_TC(once3);
ATF_TC_HEAD(once3,tc)153*11be35a1SLionel Sambuc ATF_TC_HEAD(once3, tc)
154*11be35a1SLionel Sambuc {
155*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
156*11be35a1SLionel Sambuc }
ATF_TC_BODY(once3,tc)157*11be35a1SLionel Sambuc ATF_TC_BODY(once3, tc)
158*11be35a1SLionel Sambuc {
159*11be35a1SLionel Sambuc 	pthread_t thread;
160*11be35a1SLionel Sambuc 	struct sigaction act;
161*11be35a1SLionel Sambuc 	struct itimerval it;
162*11be35a1SLionel Sambuc 	printf("Test 3 of pthread_once() (test versus cancellation)\n");
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc 	act.sa_sigaction = handler;
165*11be35a1SLionel Sambuc 	sigemptyset(&act.sa_mask);
166*11be35a1SLionel Sambuc 	act.sa_flags = SA_SIGINFO;
167*11be35a1SLionel Sambuc 	sigaction(SIGALRM, &act, NULL);
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc 	timerclear(&it.it_value);
170*11be35a1SLionel Sambuc 	it.it_value.tv_usec = 500000;
171*11be35a1SLionel Sambuc 	timerclear(&it.it_interval);
172*11be35a1SLionel Sambuc 	setitimer(ITIMER_REAL, &it, NULL);
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
175*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
176*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_cancel(thread));
177*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
178*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
181*11be35a1SLionel Sambuc 
182*11be35a1SLionel Sambuc 	/* Cancel timer */
183*11be35a1SLionel Sambuc 	timerclear(&it.it_value);
184*11be35a1SLionel Sambuc 	setitimer(ITIMER_REAL, &it, NULL);
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc 	printf("Test succeeded\n");
187*11be35a1SLionel Sambuc }
188*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)189*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
190*11be35a1SLionel Sambuc {
191*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, once1);
192*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, once2);
193*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, once3);
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc 	return atf_no_error();
196*11be35a1SLionel Sambuc }
197