Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Sep 2004 18:25:15 -0400 (EDT)
From:      Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
To:        Nate Lawson <nate@root.org>
Cc:        cvs-all@FreeBSD.ORG
Subject:   Re: cvs commit: src/sys/dev/md md.c
Message-ID:  <200409162225.i8GMPFFs010481@khavrinen.lcs.mit.edu>
In-Reply-To: <414A1073.8010404@root.org>
References:  <20040916185923.2F92316A552@hub.freebsd.org> <4149EC27.9080200@root.org> <20040916204321.GE30151@darkness.comp.waw.pl> <414A1073.8010404@root.org>

next in thread | previous in thread | raw e-mail | index | archive | help
<<On Thu, 16 Sep 2004 15:15:15 -0700, Nate Lawson <nate@root.org> said:

> You should be checking the work condition in thread 2 while holding the 
> mutex but before going to sleep.  Adding work to the queue happens in 
> thread 1 where you write "..." and that is done with the mutex held so 
> there is no race.  The full diagram with this detail included is:

Of course, getting this right is complicated enough that we have an
entire abstraction to assist.

> thread1               thread2
> -----------------------------
> mtx_lock(mtx)
> add work to queue
> mtx_unlock(mtx)
>                        mtx_lock(mtx)
> wakeup(ptr)
>                        check queue for work item
>                        if (!work item)
>                            msleep(ptr, mtx)
>                        else
>                            dequeue work item and loop

mtx_lock(mtx)
add work to queue
cv_signal(worktodo)
mtx_unlock(mtx)
			mtx_lock(mtx)
			for (;;) {
				check queue for work item
				if (!work item)
					cv_wait(cv, mtx)
				else {
					dequeue work item
					do work
				}
			}
			mtx_unlock(mtx)


-GAWollman



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