Skip to content

Commit eebb643

Browse files
committed
sx: Add sx_has_waiters() macro
This macro will return non-zero if there are threads waiting for this lock; otherwise, it will return zero. The function assumes (but does not assert) that the caller already holds the lock and that it is interested in other threads waiting for it to release the lock. The motivation to add this is the implementation of `rwsem_is_contended()` in linuxkpi. This Linux function indicates the same thing to the caller: if other threads are waiting for this semaphore. The amdgpu DRM driver started to use `rwsem_is_contended()` in Linux 6.12. Reviewed by: bz, olce Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D56443
1 parent 65dc0e9 commit eebb643

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

share/man/man9/sx.9

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2525
.\" DAMAGE.
2626
.\"
27-
.Dd November 11, 2017
27+
.Dd Apri 30, 2026
2828
.Dt SX 9
2929
.Os
3030
.Sh NAME
@@ -46,6 +46,7 @@
4646
.Nm sx_sleep ,
4747
.Nm sx_xholder ,
4848
.Nm sx_xlocked ,
49+
.Nm sx_has_waiters ,
4950
.Nm sx_assert ,
5051
.Nm SX_SYSINIT ,
5152
.Nm SX_SYSINIT_FLAGS
@@ -88,6 +89,8 @@
8889
.Fn sx_xholder "struct sx *sx"
8990
.Ft int
9091
.Fn sx_xlocked "const struct sx *sx"
92+
.Ft int
93+
.Fn sx_has_waiters "const struct sx *sx"
9194
.Pp
9295
.Cd "options INVARIANTS"
9396
.Cd "options INVARIANT_SUPPORT"
@@ -268,6 +271,13 @@ is returned instead.
268271
will return non-zero if the current thread holds the exclusive lock;
269272
otherwise, it will return zero.
270273
.Pp
274+
.Fn sx_has_waiters
275+
will return non-zero if there are threads waiting for this lock;
276+
otherwise, it will return zero.
277+
The function assumes (but does not assert) that the caller already holds the
278+
lock and that it is interested in other threads waiting for it to release the
279+
lock.
280+
.Pp
271281
For ease of programming,
272282
.Fn sx_unlock
273283
is provided as a macro frontend to the respective functions,

sys/compat/linuxkpi/common/include/linux/rwsem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct rw_semaphore {
5151
#define down_read_nested(_rw, _sc) down_read(_rw)
5252
#define init_rwsem(_rw) linux_init_rwsem(_rw, rwsem_name("lnxrwsem"))
5353
#define down_write_nest_lock(sem, _rw) down_write(_rw)
54+
#define rwsem_is_contended(_rw) sx_has_waiters(&(_rw)->sx);
5455

5556
#ifdef WITNESS_ALL
5657
/* NOTE: the maximum WITNESS name is 64 chars */

sys/sys/sx.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ __sx_xunlock(struct sx *sx, struct thread *td, const char *file, int line)
259259
(void)0; /* ensure void type for expression */ \
260260
})
261261

262+
/* Return true if there are threads waiting to acquire this sx lock. */
263+
#define sx_has_waiters(sx) \
264+
((SX_READ_VALUE(sx) & SX_LOCK_WAITERS) != 0)
265+
262266
#define sx_unlock(sx) sx_unlock_((sx), LOCK_FILE, LOCK_LINE)
263267

264268
#define sx_sleep(chan, sx, pri, wmesg, timo) \

0 commit comments

Comments
 (0)