1 /* $NetBSD: t_once.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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 $");
43 static pthread_once_t once = PTHREAD_ONCE_INIT;
44 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
56 printf("Variable x has value %d\n", x);
61 ATF_TC_HEAD(once1, tc)
63 atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
65 ATF_TC_BODY(once1, tc)
68 printf("1: Test 1 of pthread_once()\n");
70 PTHREAD_REQUIRE(pthread_once(&once, ofunc));
71 PTHREAD_REQUIRE(pthread_once(&once, ofunc));
73 printf("1: X has value %d\n",x );
81 printf("ofunc: Variable x has value %d\n", x);
86 once2_threadfunc(void *arg)
90 PTHREAD_REQUIRE(pthread_once(&once, once2_ofunc));
93 printf("Thread %d sees x with value %d\n", num, x);
100 ATF_TC_HEAD(once2, tc)
102 atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
104 ATF_TC_BODY(once2, tc)
106 pthread_t threads[NTHREADS];
110 printf("1: Test 2 of pthread_once()\n");
112 for (i=0; i < NTHREADS; i++) {
114 PTHREAD_REQUIRE(pthread_create(&threads[i], NULL, once2_threadfunc, &id[i]));
117 for (i=0; i < NTHREADS; i++)
118 PTHREAD_REQUIRE(pthread_join(threads[i], NULL));
120 printf("1: X has value %d\n",x );
121 ATF_REQUIRE_EQ(x, 2);
125 once3_cleanup(void *m)
127 pthread_mutex_t *mu = m;
129 PTHREAD_REQUIRE(pthread_mutex_unlock(mu));
135 pthread_testcancel();
139 once3_threadfunc(void *arg)
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);
150 handler(int sig, siginfo_t *info, void *ctx)
152 atf_tc_fail("Signal handler was called; "
153 "main thread deadlocked in pthread_once()");
157 ATF_TC_HEAD(once3, tc)
159 atf_tc_set_md_var(tc, "descr", "Checks pthread_once()");
161 ATF_TC_BODY(once3, tc)
164 struct sigaction act;
166 printf("Test 3 of pthread_once() (test versus cancellation)\n");
168 act.sa_sigaction = handler;
169 sigemptyset(&act.sa_mask);
170 act.sa_flags = SA_SIGINFO;
171 sigaction(SIGALRM, &act, NULL);
173 timerclear(&it.it_value);
174 it.it_value.tv_usec = 500000;
175 timerclear(&it.it_interval);
176 setitimer(ITIMER_REAL, &it, NULL);
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));
184 PTHREAD_REQUIRE(pthread_once(&once, ofunc));
187 timerclear(&it.it_value);
188 setitimer(ITIMER_REAL, &it, NULL);
190 printf("Test succeeded\n");
195 ATF_TP_ADD_TC(tp, once1);
196 ATF_TP_ADD_TC(tp, once2);
197 ATF_TP_ADD_TC(tp, once3);
199 return atf_no_error();