diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2017-08-12 23:27:45 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2017-08-12 23:27:45 +0000 |
commit | adf2042a098fbbb21b9f045926940d0b1bdc6836 (patch) | |
tree | e2ad674d32db29e51444dabb069d89512098d61d | |
parent | e94851480bfbdfb3d5bf0e12b3ffbc769f9a2c4d (diff) |
Add rw_assert_anylock(), for assering you have it either read or write locked
ok tedu@ mpi@
-rw-r--r-- | share/man/man9/rwlock.9 | 10 | ||||
-rw-r--r-- | sys/kern/kern_rwlock.c | 13 | ||||
-rw-r--r-- | sys/sys/rwlock.h | 4 |
3 files changed, 22 insertions, 5 deletions
diff --git a/share/man/man9/rwlock.9 b/share/man/man9/rwlock.9 index 46080d7486c..aa80466ff61 100644 --- a/share/man/man9/rwlock.9 +++ b/share/man/man9/rwlock.9 @@ -1,4 +1,4 @@ -.\" $OpenBSD: rwlock.9,v 1.18 2016/06/19 11:54:33 natano Exp $ +.\" $OpenBSD: rwlock.9,v 1.19 2017/08/12 23:27:44 guenther Exp $ .\" .\" Copyright (c) 2006 Pedro Martelletto <pedro@ambientworks.net> .\" All rights reserved. @@ -15,7 +15,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: June 19 2016 $ +.Dd $Mdocdate: August 12 2017 $ .Dt RWLOCK 9 .Os .Sh NAME @@ -29,6 +29,7 @@ .Nm rw_exit_write , .Nm rw_assert_wrlock , .Nm rw_assert_rdlock , +.Nm rw_assert_anylock , .Nm rw_assert_unlocked , .Nm rw_status , .Nm RWLOCK_INITIALIZER , @@ -58,6 +59,8 @@ .Ft void .Fn rw_assert_rdlock "struct rwlock *rwl" .Ft void +.Fn rw_assert_anylock "struct rwlock *rwl" +.Ft void .Fn rw_assert_unlocked "struct rwlock *rwl" .Ft int .Fn rw_status "struct rwlock *rwl" @@ -142,11 +145,12 @@ function releases a write lock. The .Fn rw_assert_wrlock , .Fn rw_assert_rdlock , +.Fn rw_assert_anylock , and .Fn rw_assert_unlocked functions check the status .Fa rwl , -panicking if it is not write-, read-, or unlocked, respectively. +panicking if it is not write-, read-, any-, or unlocked, respectively. .Pp .Nm rw_status returns the current state of the lock: diff --git a/sys/kern/kern_rwlock.c b/sys/kern/kern_rwlock.c index 1028c26a7da..e2c3caed0ab 100644 --- a/sys/kern/kern_rwlock.c +++ b/sys/kern/kern_rwlock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_rwlock.c,v 1.29 2017/08/10 19:19:18 mpi Exp $ */ +/* $OpenBSD: kern_rwlock.c,v 1.30 2017/08/12 23:27:44 guenther Exp $ */ /* * Copyright (c) 2002, 2003 Artur Grabowski <art@openbsd.org> @@ -342,6 +342,17 @@ rw_assert_rdlock(struct rwlock *rwl) } void +rw_assert_anylock(struct rwlock *rwl) +{ + switch (rw_status(rwl)) { + case RW_WRITE_OTHER: + panic("%s: lock held by different process", rwl->rwl_name); + case 0: + panic("%s: lock not held", rwl->rwl_name); + } +} + +void rw_assert_unlocked(struct rwlock *rwl) { if (rwl->rwl_owner != 0L) diff --git a/sys/sys/rwlock.h b/sys/sys/rwlock.h index 06c6a031c39..8b6808cec53 100644 --- a/sys/sys/rwlock.h +++ b/sys/sys/rwlock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rwlock.h,v 1.21 2017/04/20 13:33:00 visa Exp $ */ +/* $OpenBSD: rwlock.h,v 1.22 2017/08/12 23:27:44 guenther Exp $ */ /* * Copyright (c) 2002 Artur Grabowski <art@openbsd.org> * @@ -158,10 +158,12 @@ void _rw_exit_write(struct rwlock * LOCK_FL_VARS); #ifdef DIAGNOSTIC void rw_assert_wrlock(struct rwlock *); void rw_assert_rdlock(struct rwlock *); +void rw_assert_anylock(struct rwlock *); void rw_assert_unlocked(struct rwlock *); #else #define rw_assert_wrlock(rwl) ((void)0) #define rw_assert_rdlock(rwl) ((void)0) +#define rw_assert_anylock(rwl) ((void)0) #define rw_assert_unlocked(rwl) ((void)0) #endif |