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
;
52 printf("Variable x has value %d\n", x
);
57 ATF_TC_HEAD(once1
, tc
)
59 atf_tc_set_md_var(tc
, "descr", "Checks pthread_once()");
61 ATF_TC_BODY(once1
, tc
)
64 printf("1: Test 1 of pthread_once()\n");
66 PTHREAD_REQUIRE(pthread_once(&once
, ofunc
));
67 PTHREAD_REQUIRE(pthread_once(&once
, ofunc
));
69 printf("1: X has value %d\n",x
);
77 printf("ofunc: Variable x has value %d\n", x
);
82 once2_threadfunc(void *arg
)
86 PTHREAD_REQUIRE(pthread_once(&once
, once2_ofunc
));
89 printf("Thread %d sees x with value %d\n", num
, x
);
96 ATF_TC_HEAD(once2
, tc
)
98 atf_tc_set_md_var(tc
, "descr", "Checks pthread_once()");
100 ATF_TC_BODY(once2
, tc
)
102 pthread_t threads
[NTHREADS
];
106 printf("1: Test 2 of pthread_once()\n");
108 for (i
=0; i
< NTHREADS
; i
++) {
110 PTHREAD_REQUIRE(pthread_create(&threads
[i
], NULL
, once2_threadfunc
, &id
[i
]));
113 for (i
=0; i
< NTHREADS
; i
++)
114 PTHREAD_REQUIRE(pthread_join(threads
[i
], NULL
));
116 printf("1: X has value %d\n",x
);
117 ATF_REQUIRE_EQ(x
, 2);
121 once3_cleanup(void *m
)
123 pthread_mutex_t
*mu
= m
;
125 PTHREAD_REQUIRE(pthread_mutex_unlock(mu
));
131 pthread_testcancel();
135 once3_threadfunc(void *arg
)
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);
146 handler(int sig
, siginfo_t
*info
, void *ctx
)
148 atf_tc_fail("Signal handler was called; "
149 "main thread deadlocked in pthread_once()");
153 ATF_TC_HEAD(once3
, tc
)
155 atf_tc_set_md_var(tc
, "descr", "Checks pthread_once()");
157 ATF_TC_BODY(once3
, tc
)
160 struct sigaction act
;
162 printf("Test 3 of pthread_once() (test versus cancellation)\n");
164 act
.sa_sigaction
= handler
;
165 sigemptyset(&act
.sa_mask
);
166 act
.sa_flags
= SA_SIGINFO
;
167 sigaction(SIGALRM
, &act
, NULL
);
169 timerclear(&it
.it_value
);
170 it
.it_value
.tv_usec
= 500000;
171 timerclear(&it
.it_interval
);
172 setitimer(ITIMER_REAL
, &it
, NULL
);
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
));
180 PTHREAD_REQUIRE(pthread_once(&once
, ofunc
));
183 timerclear(&it
.it_value
);
184 setitimer(ITIMER_REAL
, &it
, NULL
);
186 printf("Test succeeded\n");
191 ATF_TP_ADD_TC(tp
, once1
);
192 ATF_TP_ADD_TC(tp
, once2
);
193 ATF_TP_ADD_TC(tp
, once3
);
195 return atf_no_error();