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