xref: /netbsd/tests/lib/libpthread/t_once.c (revision 6550d01e)
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 static void
50 ofunc(void)
51 {
52 	printf("Variable x has value %d\n", x);
53 	x++;
54 }
55 
56 ATF_TC(once1);
57 ATF_TC_HEAD(once1, tc)
58 {
59 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
60 }
61 ATF_TC_BODY(once1, tc)
62 {
63 
64 	printf("1: Test 1 of pthread_once()\n");
65 
66 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
67 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
68 
69 	printf("1: X has value %d\n",x );
70 	ATF_REQUIRE_EQ(x, 1);
71 }
72 
73 static void
74 once2_ofunc(void)
75 {
76 	x++;
77 	printf("ofunc: Variable x has value %d\n", x);
78 	x++;
79 }
80 
81 static void *
82 once2_threadfunc(void *arg)
83 {
84 	int num;
85 
86 	PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
87 
88 	num = *(int *)arg;
89 	printf("Thread %d sees x with value %d\n", num, x);
90 	ATF_REQUIRE_EQ(x, 2);
91 
92 	return NULL;
93 }
94 
95 ATF_TC(once2);
96 ATF_TC_HEAD(once2, tc)
97 {
98 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
99 }
100 ATF_TC_BODY(once2, tc)
101 {
102 	pthread_t  threads[NTHREADS];
103 	int id[NTHREADS];
104 	int i;
105 
106 	printf("1: Test 2 of pthread_once()\n");
107 
108 	for (i=0; i < NTHREADS; i++) {
109 		id[i] = i;
110 		PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
111 	}
112 
113 	for (i=0; i < NTHREADS; i++)
114 		PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
115 
116 	printf("1: X has value %d\n",x );
117 	ATF_REQUIRE_EQ(x, 2);
118 }
119 
120 static void
121 once3_cleanup(void *m)
122 {
123 	pthread_mutex_t *mu = m;
124 
125 	PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
126 }
127 
128 static void
129 once3_ofunc(void)
130 {
131 	pthread_testcancel();
132 }
133 
134 static void *
135 once3_threadfunc(void *arg)
136 {
137 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
138 	pthread_cleanup_push(once3_cleanup, &mutex);
139 	PTHREAD_REQUIRE(pthread_once(&once, once3_ofunc));
140 	pthread_cleanup_pop(1);
141 
142 	return NULL;
143 }
144 
145 static void
146 handler(int sig, siginfo_t *info, void *ctx)
147 {
148 	atf_tc_fail("Signal handler was called; "
149 		"main thread deadlocked in pthread_once()");
150 }
151 
152 ATF_TC(once3);
153 ATF_TC_HEAD(once3, tc)
154 {
155 	atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
156 }
157 ATF_TC_BODY(once3, tc)
158 {
159 	pthread_t thread;
160 	struct sigaction act;
161 	struct itimerval it;
162 	printf("Test 3 of pthread_once() (test versus cancellation)\n");
163 
164 	act.sa_sigaction = handler;
165 	sigemptyset(&act.sa_mask);
166 	act.sa_flags = SA_SIGINFO;
167 	sigaction(SIGALRM, &act, NULL);
168 
169 	timerclear(&it.it_value);
170 	it.it_value.tv_usec = 500000;
171 	timerclear(&it.it_interval);
172 	setitimer(ITIMER_REAL, &it, NULL);
173 
174 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
175 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, once3_threadfunc, NULL));
176 	PTHREAD_REQUIRE(pthread_cancel(thread));
177 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
178 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
179 
180 	PTHREAD_REQUIRE(pthread_once(&once, ofunc));
181 
182 	/* Cancel timer */
183 	timerclear(&it.it_value);
184 	setitimer(ITIMER_REAL, &it, NULL);
185 
186 	printf("Test succeeded\n");
187 }
188 
189 ATF_TP_ADD_TCS(tp)
190 {
191 	ATF_TP_ADD_TC(tp, once1);
192 	ATF_TP_ADD_TC(tp, once2);
193 	ATF_TP_ADD_TC(tp, once3);
194 
195 	return atf_no_error();
196 }
197