[RFC] Implement cv_wait_nolock(), for emulation of SGI's sv_wait()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[RFC] Implement cv_wait_nolock(), for emulation of SGI's sv_wait()

Craig Rodrigues
Hi,

As part of the XFS for FreeBSD project, Alexander Kabaev
implemented a cv_wait_nolock() function for compatibility
with SGI's sv_wait() call:
http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=man&fname=/usr/share/catman/p_man/catD/SV_WAIT.z

sv_wait() waits on a synchronization variable, the lock must be held
before the call is entered, but the lock is not held when sv_wait() is
exited.

Is this patch OK to go into FreeBSD?
Comments?


Index: condvar.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/condvar.h,v
retrieving revision 1.12
diff -u -u -r1.12 condvar.h
--- condvar.h 5 May 2004 21:57:44 -0000 1.12
+++ condvar.h 7 Dec 2005 22:38:44 -0000
@@ -53,6 +53,7 @@
 void cv_destroy(struct cv *cvp);
 
 void cv_wait(struct cv *cvp, struct mtx *mp);
+void cv_wait_nolock(struct cv *cvp, struct mtx *mp);
 int cv_wait_sig(struct cv *cvp, struct mtx *mp);
 int cv_timedwait(struct cv *cvp, struct mtx *mp, int timo);
 int cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo);
Index: kern_condvar.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_condvar.c,v
retrieving revision 1.52
diff -u -u -r1.52 kern_condvar.c
--- kern_condvar.c 12 Oct 2004 18:36:19 -0000 1.52
+++ kern_condvar.c 7 Dec 2005 22:39:25 -0000
@@ -137,6 +137,46 @@
 }
 
 /*
+ * Wait on a condition variable.  This function differs from cv_wait by
+ * not aquiring the mutex after condition variable was signaled.
+ */
+void
+cv_wait_nolock(struct cv *cvp, struct mtx *mp)
+{
+ struct thread *td;
+
+ td = curthread;
+#ifdef KTRACE
+ if (KTRPOINT(td, KTR_CSW))
+ ktrcsw(1, 0);
+#endif
+ CV_ASSERT(cvp, mp, td);
+ WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
+    "Waiting on \"%s\"", cvp->cv_description);
+
+ if (cold || panicstr) {
+ /*
+ * During autoconfiguration, just give interrupts
+ * a chance, then just return.  Don't run any other
+ * thread or panic below, in case this is the idle
+ * process and already asleep.
+ */
+ return;
+ }
+
+ sleepq_lock(cvp);
+
+ cvp->cv_waiters++;
+ DROP_GIANT();
+ mtx_unlock(mp);
+
+ sleepq_add(cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR);
+ sleepq_wait(cvp);
+
+ PICKUP_GIANT();
+}
+
+/*
  * Wait on a condition variable, allowing interruption by signals.  Return 0 if
  * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
  * a signal was caught.  If ERESTART is returned the system call should be



--
Craig Rodrigues        
[hidden email]
_______________________________________________
[hidden email] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-arch
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Implement cv_wait_nolock(), for emulation of SGI's sv_wait()

Christoph Hellwig
On Wed, Dec 07, 2005 at 05:43:59PM -0500, Craig Rodrigues wrote:

> Hi,
>
> As part of the XFS for FreeBSD project, Alexander Kabaev
> implemented a cv_wait_nolock() function for compatibility
> with SGI's sv_wait() call:
> http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=man&fname=/usr/share/catman/p_man/catD/SV_WAIT.z
>
> sv_wait() waits on a synchronization variable, the lock must be held
> before the call is entered, but the lock is not held when sv_wait() is
> exited.

The name sounds odd.  I'd rather all it cv_wait_unlock.

_______________________________________________
[hidden email] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-arch
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: [RFC] Implement cv_wait_nolock(), for emulation of SGI's sv_wait()

John Baldwin
In reply to this post by Craig Rodrigues
On Wednesday 07 December 2005 05:43 pm, Craig Rodrigues wrote:

> Hi,
>
> As part of the XFS for FreeBSD project, Alexander Kabaev
> implemented a cv_wait_nolock() function for compatibility
> with SGI's sv_wait() call:
> http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=man&fna
>me=/usr/share/catman/p_man/catD/SV_WAIT.z
>
> sv_wait() waits on a synchronization variable, the lock must be held
> before the call is entered, but the lock is not held when sv_wait() is
> exited.
>
> Is this patch OK to go into FreeBSD?
> Comments?

As I said on IRC, I'm not a big fan of this or PDROP, but PDROP is in the
tree.  Note that you could implement sv_wait() using the existing primitives
via:

sv_wait()
{
        cv_wait()
        mtx_unlock()
}

If you do stick with cv_wait_nolock(), _please_ don't duplicate a whole bunch
of code by implement cv_wait() like so:

cv_wait()
{
        cv_wait_nolock()
        mtx_lock()
}

Note that currently you have a bug in the cold || panicstr case in that the
function doesn't drop the lock.

--
John Baldwin <[hidden email]>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =  http://www.FreeBSD.org
_______________________________________________
[hidden email] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-arch
To unsubscribe, send any mail to "[hidden email]"