Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Aug 2020 23:00:00 +0000 (UTC)
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r363871 - in head/sys: kern sys
Message-ID:  <202008042300.074N00Do021197@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mjg
Date: Tue Aug  4 23:00:00 2020
New Revision: 363871
URL: https://svnweb.freebsd.org/changeset/base/363871

Log:
  mtx: add mtx_wait_unlocked

Modified:
  head/sys/kern/kern_mutex.c
  head/sys/sys/mutex.h

Modified: head/sys/kern/kern_mutex.c
==============================================================================
--- head/sys/kern/kern_mutex.c	Tue Aug  4 21:58:43 2020	(r363870)
+++ head/sys/kern/kern_mutex.c	Tue Aug  4 23:00:00 2020	(r363871)
@@ -1275,6 +1275,35 @@ mtx_spin_wait_unlocked(struct mtx *m)
 	}
 }
 
+void
+mtx_wait_unlocked(struct mtx *m)
+{
+	struct thread *owner;
+	uintptr_t v;
+
+	KASSERT(m->mtx_lock != MTX_DESTROYED,
+	    ("%s() of destroyed mutex %p", __func__, m));
+	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
+	    ("%s() not a sleep mutex %p (%s)", __func__, m,
+	    m->lock_object.lo_name));
+	KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
+	    m->lock_object.lo_name));
+
+	for (;;) {
+		v = atomic_load_acq_ptr(&m->mtx_lock);
+		if (v == MTX_UNOWNED) {
+			break;
+		}
+		owner = lv_mtx_owner(v);
+		if (!TD_IS_RUNNING(owner)) {
+			mtx_lock(m);
+			mtx_unlock(m);
+			break;
+		}
+		cpu_spinwait();
+	}
+}
+
 #ifdef DDB
 void
 db_show_mtx(const struct lock_object *lock)

Modified: head/sys/sys/mutex.h
==============================================================================
--- head/sys/sys/mutex.h	Tue Aug  4 21:58:43 2020	(r363870)
+++ head/sys/sys/mutex.h	Tue Aug  4 23:00:00 2020	(r363871)
@@ -106,6 +106,7 @@ void	__mtx_unlock_sleep(volatile uintptr_t *c, uintptr
 void	__mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v);
 void	__mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v);
 #endif
+void	mtx_wait_unlocked(struct mtx *m);
 
 #ifdef SMP
 #if LOCK_DEBUG > 0



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202008042300.074N00Do021197>