xref: /freebsd-src/contrib/netbsd-tests/lib/libpthread/t_rwlock.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1*57718be8SEnji Cooper /* $NetBSD: t_rwlock.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*
4*57718be8SEnji Cooper  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper  * are met:
10*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper  *
16*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
27*57718be8SEnji Cooper  */
28*57718be8SEnji Cooper 
29*57718be8SEnji Cooper /*-
30*57718be8SEnji Cooper  * Copyright (c)2004 YAMAMOTO Takashi,
31*57718be8SEnji Cooper  * All rights reserved.
32*57718be8SEnji Cooper  *
33*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
34*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
35*57718be8SEnji Cooper  * are met:
36*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
37*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
38*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
39*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
40*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
41*57718be8SEnji Cooper  *
42*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43*57718be8SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44*57718be8SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45*57718be8SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46*57718be8SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47*57718be8SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48*57718be8SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49*57718be8SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50*57718be8SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51*57718be8SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52*57718be8SEnji Cooper  * SUCH DAMAGE.
53*57718be8SEnji Cooper  */
54*57718be8SEnji Cooper 
55*57718be8SEnji Cooper #include <sys/cdefs.h>
56*57718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\
57*57718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
58*57718be8SEnji Cooper __RCSID("$NetBSD: t_rwlock.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $");
59*57718be8SEnji Cooper 
60*57718be8SEnji Cooper #include <errno.h>
61*57718be8SEnji Cooper #include <pthread.h>
62*57718be8SEnji Cooper #include <stdlib.h>
63*57718be8SEnji Cooper #include <string.h>
64*57718be8SEnji Cooper 
65*57718be8SEnji Cooper #include <atf-c.h>
66*57718be8SEnji Cooper 
67*57718be8SEnji Cooper #include "h_common.h"
68*57718be8SEnji Cooper 
69*57718be8SEnji Cooper pthread_rwlock_t lk;
70*57718be8SEnji Cooper 
71*57718be8SEnji Cooper struct timespec to;
72*57718be8SEnji Cooper 
73*57718be8SEnji Cooper /* ARGSUSED */
74*57718be8SEnji Cooper static void *
75*57718be8SEnji Cooper do_nothing(void *dummy)
76*57718be8SEnji Cooper {
77*57718be8SEnji Cooper 	return NULL;
78*57718be8SEnji Cooper }
79*57718be8SEnji Cooper 
80*57718be8SEnji Cooper ATF_TC(rwlock1);
81*57718be8SEnji Cooper ATF_TC_HEAD(rwlock1, tc)
82*57718be8SEnji Cooper {
83*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks read/write locks");
84*57718be8SEnji Cooper }
85*57718be8SEnji Cooper ATF_TC_BODY(rwlock1, tc)
86*57718be8SEnji Cooper {
87*57718be8SEnji Cooper 	int error;
88*57718be8SEnji Cooper 	pthread_t t;
89*57718be8SEnji Cooper 
90*57718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_create(&t, NULL, do_nothing, NULL));
91*57718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_rwlock_init(&lk, NULL));
92*57718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk));
93*57718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk));
94*57718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk));
95*57718be8SEnji Cooper 
96*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(pthread_rwlock_trywrlock(&lk), EBUSY);
97*57718be8SEnji Cooper 
98*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0,
99*57718be8SEnji Cooper 		"%s", strerror(errno));
100*57718be8SEnji Cooper 	to.tv_sec++;
101*57718be8SEnji Cooper 	error = pthread_rwlock_timedwrlock(&lk, &to);
102*57718be8SEnji Cooper 	ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK,
103*57718be8SEnji Cooper 		"%s", strerror(error));
104*57718be8SEnji Cooper 
105*57718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk));
106*57718be8SEnji Cooper 
107*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0,
108*57718be8SEnji Cooper 						"%s", strerror(errno));
109*57718be8SEnji Cooper 	to.tv_sec++;
110*57718be8SEnji Cooper 	PTHREAD_REQUIRE(pthread_rwlock_timedwrlock(&lk, &to));
111*57718be8SEnji Cooper 
112*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0,
113*57718be8SEnji Cooper 						"%s", strerror(errno));
114*57718be8SEnji Cooper 	to.tv_sec++;
115*57718be8SEnji Cooper 	error = pthread_rwlock_timedwrlock(&lk, &to);
116*57718be8SEnji Cooper 	ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK,
117*57718be8SEnji Cooper 		"%s", strerror(error));
118*57718be8SEnji Cooper }
119*57718be8SEnji Cooper 
120*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
121*57718be8SEnji Cooper {
122*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, rwlock1);
123*57718be8SEnji Cooper 
124*57718be8SEnji Cooper 	return atf_no_error();
125*57718be8SEnji Cooper }
126