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