1*640235e2SEnji Cooper /* $NetBSD: t_rwlock.c,v 1.2 2015/06/26 11:07:20 pooka Exp $ */ 257718be8SEnji Cooper 357718be8SEnji Cooper /* 457718be8SEnji Cooper * Copyright (c) 2008 The NetBSD Foundation, Inc. 557718be8SEnji Cooper * All rights reserved. 657718be8SEnji Cooper * 757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 857718be8SEnji Cooper * modification, are permitted provided that the following conditions 957718be8SEnji Cooper * are met: 1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 1557718be8SEnji Cooper * 1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE. 2757718be8SEnji Cooper */ 2857718be8SEnji Cooper 2957718be8SEnji Cooper /*- 3057718be8SEnji Cooper * Copyright (c)2004 YAMAMOTO Takashi, 3157718be8SEnji Cooper * All rights reserved. 3257718be8SEnji Cooper * 3357718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 3457718be8SEnji Cooper * modification, are permitted provided that the following conditions 3557718be8SEnji Cooper * are met: 3657718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 3757718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 3857718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 3957718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 4057718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 4157718be8SEnji Cooper * 4257718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 4357718be8SEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4457718be8SEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4557718be8SEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4657718be8SEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4757718be8SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4857718be8SEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4957718be8SEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5057718be8SEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5157718be8SEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5257718be8SEnji Cooper * SUCH DAMAGE. 5357718be8SEnji Cooper */ 5457718be8SEnji Cooper 5557718be8SEnji Cooper #include <sys/cdefs.h> 5657718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\ 5757718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved."); 58*640235e2SEnji Cooper __RCSID("$NetBSD: t_rwlock.c,v 1.2 2015/06/26 11:07:20 pooka Exp $"); 5957718be8SEnji Cooper 6057718be8SEnji Cooper #include <errno.h> 6157718be8SEnji Cooper #include <pthread.h> 6257718be8SEnji Cooper #include <stdlib.h> 6357718be8SEnji Cooper #include <string.h> 6457718be8SEnji Cooper 6557718be8SEnji Cooper #include <atf-c.h> 6657718be8SEnji Cooper 6757718be8SEnji Cooper #include "h_common.h" 6857718be8SEnji Cooper 6957718be8SEnji Cooper pthread_rwlock_t lk; 7057718be8SEnji Cooper 7157718be8SEnji Cooper struct timespec to; 7257718be8SEnji Cooper 73*640235e2SEnji Cooper static pthread_rwlock_t static_rwlock = PTHREAD_RWLOCK_INITIALIZER; 74*640235e2SEnji Cooper 7557718be8SEnji Cooper /* ARGSUSED */ 7657718be8SEnji Cooper static void * 7757718be8SEnji Cooper do_nothing(void *dummy) 7857718be8SEnji Cooper { 7957718be8SEnji Cooper return NULL; 8057718be8SEnji Cooper } 8157718be8SEnji Cooper 8257718be8SEnji Cooper ATF_TC(rwlock1); 8357718be8SEnji Cooper ATF_TC_HEAD(rwlock1, tc) 8457718be8SEnji Cooper { 8557718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks read/write locks"); 8657718be8SEnji Cooper } 8757718be8SEnji Cooper ATF_TC_BODY(rwlock1, tc) 8857718be8SEnji Cooper { 8957718be8SEnji Cooper int error; 9057718be8SEnji Cooper pthread_t t; 9157718be8SEnji Cooper 9257718be8SEnji Cooper PTHREAD_REQUIRE(pthread_create(&t, NULL, do_nothing, NULL)); 9357718be8SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_init(&lk, NULL)); 9457718be8SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk)); 9557718be8SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_rdlock(&lk)); 9657718be8SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk)); 9757718be8SEnji Cooper 9857718be8SEnji Cooper ATF_REQUIRE_EQ(pthread_rwlock_trywrlock(&lk), EBUSY); 9957718be8SEnji Cooper 10057718be8SEnji Cooper ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0, 10157718be8SEnji Cooper "%s", strerror(errno)); 10257718be8SEnji Cooper to.tv_sec++; 10357718be8SEnji Cooper error = pthread_rwlock_timedwrlock(&lk, &to); 10457718be8SEnji Cooper ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK, 10557718be8SEnji Cooper "%s", strerror(error)); 10657718be8SEnji Cooper 10757718be8SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_unlock(&lk)); 10857718be8SEnji Cooper 10957718be8SEnji Cooper ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0, 11057718be8SEnji Cooper "%s", strerror(errno)); 11157718be8SEnji Cooper to.tv_sec++; 11257718be8SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_timedwrlock(&lk, &to)); 11357718be8SEnji Cooper 11457718be8SEnji Cooper ATF_REQUIRE_EQ_MSG(clock_gettime(CLOCK_REALTIME, &to), 0, 11557718be8SEnji Cooper "%s", strerror(errno)); 11657718be8SEnji Cooper to.tv_sec++; 11757718be8SEnji Cooper error = pthread_rwlock_timedwrlock(&lk, &to); 11857718be8SEnji Cooper ATF_REQUIRE_MSG(error == ETIMEDOUT || error == EDEADLK, 11957718be8SEnji Cooper "%s", strerror(error)); 12057718be8SEnji Cooper } 12157718be8SEnji Cooper 122*640235e2SEnji Cooper ATF_TC(rwlock_static); 123*640235e2SEnji Cooper ATF_TC_HEAD(rwlock_static, tc) 124*640235e2SEnji Cooper { 125*640235e2SEnji Cooper atf_tc_set_md_var(tc, "descr", "rwlock w/ static initializer"); 126*640235e2SEnji Cooper } 127*640235e2SEnji Cooper ATF_TC_BODY(rwlock_static, tc) 128*640235e2SEnji Cooper { 129*640235e2SEnji Cooper 130*640235e2SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_rdlock(&static_rwlock)); 131*640235e2SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_unlock(&static_rwlock)); 132*640235e2SEnji Cooper PTHREAD_REQUIRE(pthread_rwlock_destroy(&static_rwlock)); 133*640235e2SEnji Cooper } 134*640235e2SEnji Cooper 13557718be8SEnji Cooper ATF_TP_ADD_TCS(tp) 13657718be8SEnji Cooper { 13757718be8SEnji Cooper ATF_TP_ADD_TC(tp, rwlock1); 138*640235e2SEnji Cooper ATF_TP_ADD_TC(tp, rwlock_static); 13957718be8SEnji Cooper 14057718be8SEnji Cooper return atf_no_error(); 14157718be8SEnji Cooper } 142