1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_rwlock.c,v 1.2 2015/06/26 11:07:20 pooka Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc * Copyright (c) 2008 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1711be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1811be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1911be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2011be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2111be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2211be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2311be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2411be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2511be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2611be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2711be35a1SLionel Sambuc */
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc /*-
3011be35a1SLionel Sambuc * Copyright (c)2004 YAMAMOTO Takashi,
3111be35a1SLionel Sambuc * All rights reserved.
3211be35a1SLionel Sambuc *
3311be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
3411be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
3511be35a1SLionel Sambuc * are met:
3611be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
3711be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
3811be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
3911be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
4011be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
4111be35a1SLionel Sambuc *
4211be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4311be35a1SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4411be35a1SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4511be35a1SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4611be35a1SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4711be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4811be35a1SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4911be35a1SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5011be35a1SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5111be35a1SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5211be35a1SLionel Sambuc * SUCH DAMAGE.
5311be35a1SLionel Sambuc */
5411be35a1SLionel Sambuc
5511be35a1SLionel Sambuc #include <sys/cdefs.h>
5611be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
5711be35a1SLionel Sambuc The NetBSD Foundation, inc. All rights reserved.");
58*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_rwlock.c,v 1.2 2015/06/26 11:07:20 pooka Exp $");
5911be35a1SLionel Sambuc
6011be35a1SLionel Sambuc #include <errno.h>
6111be35a1SLionel Sambuc #include <pthread.h>
6211be35a1SLionel Sambuc #include <stdlib.h>
6311be35a1SLionel Sambuc #include <string.h>
6411be35a1SLionel Sambuc
6511be35a1SLionel Sambuc #include <atf-c.h>
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc #include "h_common.h"
6811be35a1SLionel Sambuc
6911be35a1SLionel Sambuc pthread_rwlock_t lk;
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc struct timespec to;
7211be35a1SLionel Sambuc
73*0a6a1f1dSLionel Sambuc static pthread_rwlock_t static_rwlock = PTHREAD_RWLOCK_INITIALIZER;
74*0a6a1f1dSLionel Sambuc
7511be35a1SLionel Sambuc /* ARGSUSED */
7611be35a1SLionel Sambuc static void *
do_nothing(void * dummy)7711be35a1SLionel Sambuc do_nothing(void *dummy)
7811be35a1SLionel Sambuc {
7911be35a1SLionel Sambuc return NULL;
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc ATF_TC(rwlock1);
ATF_TC_HEAD(rwlock1,tc)8311be35a1SLionel Sambuc ATF_TC_HEAD(rwlock1, tc)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks read/write locks");
8611be35a1SLionel Sambuc }
ATF_TC_BODY(rwlock1,tc)8711be35a1SLionel Sambuc ATF_TC_BODY(rwlock1, tc)
8811be35a1SLionel Sambuc {
8911be35a1SLionel Sambuc int error;
9011be35a1SLionel Sambuc pthread_t t;
9111be35a1SLionel Sambuc
9211be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_create(&t, NULL, do_nothing, NULL));
9311be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_init(&lk, NULL));
9411be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk));
9511be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk));
9611be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk));
9711be35a1SLionel Sambuc
9811be35a1SLionel Sambuc ATF_REQUIRE_EQ(pthread_rwlock_trywrlock(&lk), EBUSY);
9911be35a1SLionel Sambuc
10011be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0,
10111be35a1SLionel Sambuc "%s", strerror(errno));
10211be35a1SLionel Sambuc to.tv_sec++;
10311be35a1SLionel Sambuc error = pthread_rwlock_timedwrlock(&lk, &to);
10411be35a1SLionel Sambuc ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK,
10511be35a1SLionel Sambuc "%s", strerror(error));
10611be35a1SLionel Sambuc
10711be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk));
10811be35a1SLionel Sambuc
10911be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0,
11011be35a1SLionel Sambuc "%s", strerror(errno));
11111be35a1SLionel Sambuc to.tv_sec++;
11211be35a1SLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_timedwrlock(&lk, &to));
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0,
11511be35a1SLionel Sambuc "%s", strerror(errno));
11611be35a1SLionel Sambuc to.tv_sec++;
11711be35a1SLionel Sambuc error = pthread_rwlock_timedwrlock(&lk, &to);
11811be35a1SLionel Sambuc ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK,
11911be35a1SLionel Sambuc "%s", strerror(error));
12011be35a1SLionel Sambuc }
12111be35a1SLionel Sambuc
122*0a6a1f1dSLionel Sambuc ATF_TC(rwlock_static);
ATF_TC_HEAD(rwlock_static,tc)123*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(rwlock_static, tc)
124*0a6a1f1dSLionel Sambuc {
125*0a6a1f1dSLionel Sambuc atf_tc_set_md_var(tc, "descr", "rwlock w/ static initializer");
126*0a6a1f1dSLionel Sambuc }
ATF_TC_BODY(rwlock_static,tc)127*0a6a1f1dSLionel Sambuc ATF_TC_BODY(rwlock_static, tc)
128*0a6a1f1dSLionel Sambuc {
129*0a6a1f1dSLionel Sambuc
130*0a6a1f1dSLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_rdlock(&static_rwlock));
131*0a6a1f1dSLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_unlock(&static_rwlock));
132*0a6a1f1dSLionel Sambuc PTHREAD_REQUIRE(pthread_rwlock_destroy(&static_rwlock));
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc
ATF_TP_ADD_TCS(tp)13511be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
13611be35a1SLionel Sambuc {
13711be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, rwlock1);
138*0a6a1f1dSLionel Sambuc ATF_TP_ADD_TC(tp, rwlock_static);
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc return atf_no_error();
14111be35a1SLionel Sambuc }
142