Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 17 Sep 2004 21:23:30 -0400
From:      John Baldwin <jhb@FreeBSD.org>
To:        Nate Lawson <nate@root.org>
Cc:        "M. Warner Losh" <imp@bsdimp.com>
Subject:   Re: cvs commit: src/sys/dev/md md.c
Message-ID:  <200409172123.30503.jhb@FreeBSD.org>
In-Reply-To: <414A4A79.8060101@root.org>
References:  <20040916204321.GE30151@darkness.comp.waw.pl> <200409162152.41670.jhb@FreeBSD.org> <414A4A79.8060101@root.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thursday 16 September 2004 10:22 pm, Nate Lawson wrote:
> John Baldwin wrote:
> > On Thursday 16 September 2004 08:59 pm, M. Warner Losh wrote:
> >>In message: <414A109E.4080601@samsco.org>
> >>
> >>            Scott Long <scottl@samsco.org> writes:
> >>: Or just use a semaphore.
> >>
> >>Or a condition variable.
> >
> > That doesn't help, a condition variable doesn't have state like a
> > semaphore. The correct fix is to do this:
> >
> > thread A
> > ----------
> > 	mtx_lock();
> > 	adjust_state();
> > 	mtx_unlock();
> > 	wakeup();
> >
> > thread B
> > ---------
> > 	mtx_lock();
> > 	while (state_still_needs_sleep())
> > 		msleep(...)
> > 	mtx_unlock()
> >
> > Proving that there is no race is left as an exercise to the reader. :)
>
> Isn't this exactly what I said?

You use if instead of while.  Truly pedantic coding would use while 
instead. :)  I'm not aware of any bugs in our current wakeup or condition 
variable implementations that would lead to spurious wakeups, but you never 
know (esp. with wakeup where the same thing gets slept on for 47 different 
reasons, e.g. the proc pointer) when you might get a wakeup that's really not 
for you.  Best to make sure that if you do you don't break.

-- 
John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =  http://www.FreeBSD.org



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