17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*b3d32f0cSBryan Cantrill /*
28*b3d32f0cSBryan Cantrill * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29*b3d32f0cSBryan Cantrill */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <sys/rwstlock.h>
327c478bd9Sstevel@tonic-gate #include <sys/errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * Alternate rwlock that is interruptible and can be released by a thread
407c478bd9Sstevel@tonic-gate * other than the one that acquired the lock.
417c478bd9Sstevel@tonic-gate *
427c478bd9Sstevel@tonic-gate * There is no priority inheritance mechanism for these locks.
43*b3d32f0cSBryan Cantrill * For RW_READER, writers have priority over readers, so reader starvation
44*b3d32f0cSBryan Cantrill * is possible; as with rwlocks, this behavior may be overridden by
45*b3d32f0cSBryan Cantrill * specifying RW_READER_STARVEWRITER.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate * Common code to grab a lock. There are three cases:
507c478bd9Sstevel@tonic-gate *
517c478bd9Sstevel@tonic-gate * (1) If RWST_TRYENTER is set, we try the lock without blocking.
527c478bd9Sstevel@tonic-gate * In this case we return 1 on success, 0 on failure.
537c478bd9Sstevel@tonic-gate *
547c478bd9Sstevel@tonic-gate * (2) If RWST_SIG is set, we block interruptibly until we get the lock.
557c478bd9Sstevel@tonic-gate * In this case we return 0 on success, EINTR if we're interrupted.
567c478bd9Sstevel@tonic-gate *
577c478bd9Sstevel@tonic-gate * (3) If neither flag is set, we block uninterruptibly until we get the lock.
587c478bd9Sstevel@tonic-gate * In this case we return 0 (we always succeed).
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate static int
rwst_enter_common(rwstlock_t * l,krw_t rw,int flags)617c478bd9Sstevel@tonic-gate rwst_enter_common(rwstlock_t *l, krw_t rw, int flags)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate hrtime_t sleep_time;
647c478bd9Sstevel@tonic-gate int writer;
657c478bd9Sstevel@tonic-gate intptr_t readers;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate mutex_enter(&l->rwst_lock);
68*b3d32f0cSBryan Cantrill if (rw == RW_READER || rw == RW_READER_STARVEWRITER) {
69*b3d32f0cSBryan Cantrill while (RWST_WRITE_HELD(l) ||
70*b3d32f0cSBryan Cantrill (rw != RW_READER_STARVEWRITER && RWST_WRITE_WANTED(l))) {
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate if (flags & RWST_TRYENTER) {
737c478bd9Sstevel@tonic-gate mutex_exit(&l->rwst_lock);
747c478bd9Sstevel@tonic-gate return (0);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate if (panicstr)
777c478bd9Sstevel@tonic-gate return (0);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate if (RWST_WRITE_HELD(l)) {
807c478bd9Sstevel@tonic-gate writer = 1;
817c478bd9Sstevel@tonic-gate readers = 0;
827c478bd9Sstevel@tonic-gate } else {
837c478bd9Sstevel@tonic-gate writer = 0;
847c478bd9Sstevel@tonic-gate readers = l->rwst_count;
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate sleep_time = -gethrtime();
877c478bd9Sstevel@tonic-gate if (!RWST_READ_WAIT(l, flags)) {
887c478bd9Sstevel@tonic-gate mutex_exit(&l->rwst_lock);
897c478bd9Sstevel@tonic-gate return (EINTR);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate sleep_time += gethrtime();
927c478bd9Sstevel@tonic-gate LOCKSTAT_RECORD4(LS_RW_ENTER_BLOCK, l, sleep_time, rw,
937c478bd9Sstevel@tonic-gate writer, readers);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate RWST_READ_ENTER(l);
967c478bd9Sstevel@tonic-gate LOCKSTAT_RECORD(LS_RW_ENTER_ACQUIRE, l, rw);
977c478bd9Sstevel@tonic-gate } else {
987c478bd9Sstevel@tonic-gate ASSERT(rw == RW_WRITER);
997c478bd9Sstevel@tonic-gate while (RWST_HELD(l)) {
1007c478bd9Sstevel@tonic-gate if (flags & RWST_TRYENTER) {
1017c478bd9Sstevel@tonic-gate mutex_exit(&l->rwst_lock);
1027c478bd9Sstevel@tonic-gate return (0);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate if (panicstr)
1057c478bd9Sstevel@tonic-gate return (0);
1067c478bd9Sstevel@tonic-gate if (RWST_WRITE_HELD(l)) {
1077c478bd9Sstevel@tonic-gate writer = 1;
1087c478bd9Sstevel@tonic-gate readers = 0;
1097c478bd9Sstevel@tonic-gate } else {
1107c478bd9Sstevel@tonic-gate writer = 0;
1117c478bd9Sstevel@tonic-gate readers = l->rwst_count;
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate sleep_time = -gethrtime();
1147c478bd9Sstevel@tonic-gate if (!RWST_WRITE_WAIT(l, flags)) {
1157c478bd9Sstevel@tonic-gate if (!RWST_WRITE_HELD(l) &&
1167c478bd9Sstevel@tonic-gate !RWST_WRITE_WANTED(l))
1177c478bd9Sstevel@tonic-gate RWST_READ_WAKE_ALL(l);
1187c478bd9Sstevel@tonic-gate mutex_exit(&l->rwst_lock);
1197c478bd9Sstevel@tonic-gate return (EINTR);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate sleep_time += gethrtime();
1227c478bd9Sstevel@tonic-gate LOCKSTAT_RECORD4(LS_RW_ENTER_BLOCK, l, sleep_time, rw,
1237c478bd9Sstevel@tonic-gate writer, readers);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate RWST_WRITE_ENTER(l);
1267c478bd9Sstevel@tonic-gate LOCKSTAT_RECORD(LS_RW_ENTER_ACQUIRE, l, rw);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate mutex_exit(&l->rwst_lock);
1297c478bd9Sstevel@tonic-gate return (flags & RWST_TRYENTER);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate void
rwst_exit(rwstlock_t * l)1337c478bd9Sstevel@tonic-gate rwst_exit(rwstlock_t *l)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate mutex_enter(&l->rwst_lock);
1367c478bd9Sstevel@tonic-gate if (RWST_WRITE_HELD(l)) {
1377c478bd9Sstevel@tonic-gate LOCKSTAT_RECORD(LS_RW_EXIT_RELEASE, l, RW_WRITER);
1387c478bd9Sstevel@tonic-gate RWST_WRITE_EXIT(l);
1397c478bd9Sstevel@tonic-gate } else {
1407c478bd9Sstevel@tonic-gate ASSERT(RWST_READ_HELD(l));
1417c478bd9Sstevel@tonic-gate LOCKSTAT_RECORD(LS_RW_EXIT_RELEASE, l, RW_READER);
1427c478bd9Sstevel@tonic-gate RWST_READ_EXIT(l);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate if (!RWST_WRITE_WANTED(l))
1457c478bd9Sstevel@tonic-gate RWST_READ_WAKE_ALL(l);
1467c478bd9Sstevel@tonic-gate else if (!RWST_HELD(l))
1477c478bd9Sstevel@tonic-gate RWST_WRITE_WAKE_ONE(l);
1487c478bd9Sstevel@tonic-gate mutex_exit(&l->rwst_lock);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate void
rwst_enter(rwstlock_t * l,krw_t rw)1527c478bd9Sstevel@tonic-gate rwst_enter(rwstlock_t *l, krw_t rw)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate (void) rwst_enter_common(l, rw, 0);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate int
rwst_enter_sig(rwstlock_t * l,krw_t rw)1587c478bd9Sstevel@tonic-gate rwst_enter_sig(rwstlock_t *l, krw_t rw)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate return (rwst_enter_common(l, rw, RWST_SIG));
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate int
rwst_tryenter(rwstlock_t * l,krw_t rw)1647c478bd9Sstevel@tonic-gate rwst_tryenter(rwstlock_t *l, krw_t rw)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate return (rwst_enter_common(l, rw, RWST_TRYENTER));
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate int
rwst_lock_held(rwstlock_t * l,krw_t rw)1707c478bd9Sstevel@tonic-gate rwst_lock_held(rwstlock_t *l, krw_t rw)
1717c478bd9Sstevel@tonic-gate {
172*b3d32f0cSBryan Cantrill if (rw != RW_WRITER)
1737c478bd9Sstevel@tonic-gate return (RWST_READ_HELD(l));
1747c478bd9Sstevel@tonic-gate ASSERT(rw == RW_WRITER);
1757c478bd9Sstevel@tonic-gate return (RWST_WRITE_OWNER(l));
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1797c478bd9Sstevel@tonic-gate void
rwst_init(rwstlock_t * l,char * name,krw_type_t krw_t,void * arg)1807c478bd9Sstevel@tonic-gate rwst_init(rwstlock_t *l, char *name, krw_type_t krw_t, void *arg)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate l->rwst_count = 0;
1837c478bd9Sstevel@tonic-gate mutex_init(&l->rwst_lock, NULL, MUTEX_DEFAULT, NULL);
1847c478bd9Sstevel@tonic-gate cv_init(&l->rwst_rcv, NULL, CV_DEFAULT, NULL);
1857c478bd9Sstevel@tonic-gate cv_init(&l->rwst_wcv, NULL, CV_DEFAULT, NULL);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate void
rwst_destroy(rwstlock_t * l)1897c478bd9Sstevel@tonic-gate rwst_destroy(rwstlock_t *l)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate ASSERT(l->rwst_count == 0);
1927c478bd9Sstevel@tonic-gate mutex_destroy(&l->rwst_lock);
1937c478bd9Sstevel@tonic-gate cv_destroy(&l->rwst_rcv);
1947c478bd9Sstevel@tonic-gate cv_destroy(&l->rwst_wcv);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate struct _kthread *
rwst_owner(rwstlock_t * l)1987c478bd9Sstevel@tonic-gate rwst_owner(rwstlock_t *l)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate return (RWST_OWNER(l));
2017c478bd9Sstevel@tonic-gate }
202