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