summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorVisa Hankala <visa@cvs.openbsd.org>2022-03-16 14:13:02 +0000
committerVisa Hankala <visa@cvs.openbsd.org>2022-03-16 14:13:02 +0000
commit4defa17785a08f6a628a5a171283148594c5ff0f (patch)
treef70a173813c596bd32bed13f53b1e6622b7290c2 /sys
parenta854baef1d2ef3a7acb26f7475302c84bc02b5fd (diff)
Add refcnt_shared() and refcnt_read()
refcnt_shared() checks whether the object has multiple references. When refcnt_shared() returns zero, the caller is the only reference holder. refcnt_read() returns a snapshot of the counter value. refcnt_shared() suggested by dlg@. OK dlg@ mvs@
Diffstat (limited to 'sys')
-rw-r--r--sys/kern/kern_synch.c14
-rw-r--r--sys/sys/refcnt.h4
2 files changed, 16 insertions, 2 deletions
diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c
index 27e36501ae9..e877acff824 100644
--- a/sys/kern/kern_synch.c
+++ b/sys/kern/kern_synch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_synch.c,v 1.183 2022/03/10 15:21:08 bluhm Exp $ */
+/* $OpenBSD: kern_synch.c,v 1.184 2022/03/16 14:13:01 visa Exp $ */
/* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */
/*
@@ -852,6 +852,18 @@ refcnt_finalize(struct refcnt *r, const char *wmesg)
}
}
+int
+refcnt_shared(struct refcnt *r)
+{
+ return (atomic_load_int(&r->r_refs) > 1);
+}
+
+unsigned int
+refcnt_read(struct refcnt *r)
+{
+ return (atomic_load_int(&r->r_refs));
+}
+
void
cond_init(struct cond *c)
{
diff --git a/sys/sys/refcnt.h b/sys/sys/refcnt.h
index 26423346e51..7f7b4e87d60 100644
--- a/sys/sys/refcnt.h
+++ b/sys/sys/refcnt.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: refcnt.h,v 1.5 2022/03/10 15:21:08 bluhm Exp $ */
+/* $OpenBSD: refcnt.h,v 1.6 2022/03/16 14:13:01 visa Exp $ */
/*
* Copyright (c) 2015 David Gwynne <dlg@openbsd.org>
@@ -37,6 +37,8 @@ void refcnt_take(struct refcnt *);
int refcnt_rele(struct refcnt *);
void refcnt_rele_wake(struct refcnt *);
void refcnt_finalize(struct refcnt *, const char *);
+int refcnt_shared(struct refcnt *);
+unsigned int refcnt_read(struct refcnt *);
#endif /* _KERNEL */