From owner-cvs-src@FreeBSD.ORG Thu Sep 16 22:25:18 2004 Return-Path: Delivered-To: cvs-src@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0C10816A4CE; Thu, 16 Sep 2004 22:25:18 +0000 (GMT) Received: from khavrinen.lcs.mit.edu (khavrinen.lcs.mit.edu [128.30.28.20]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8228543D31; Thu, 16 Sep 2004 22:25:17 +0000 (GMT) (envelope-from wollman@khavrinen.lcs.mit.edu) Received: from khavrinen.lcs.mit.edu (localhost [IPv6:::1]) by khavrinen.lcs.mit.edu (8.12.9/8.12.9) with ESMTP id i8GMPF8g010484 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK CN=khavrinen.lcs.mit.edu issuer=SSL+20Client+20CA); Thu, 16 Sep 2004 18:25:16 -0400 (EDT) (envelope-from wollman@khavrinen.lcs.mit.edu) Received: (from wollman@localhost) by khavrinen.lcs.mit.edu (8.12.9/8.12.9/Submit) id i8GMPFFs010481; Thu, 16 Sep 2004 18:25:15 -0400 (EDT) (envelope-from wollman) Date: Thu, 16 Sep 2004 18:25:15 -0400 (EDT) From: Garrett Wollman Message-Id: <200409162225.i8GMPFFs010481@khavrinen.lcs.mit.edu> To: Nate Lawson 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> X-Spam-Score: -19.8 () IN_REP_TO,QUOTED_EMAIL_TEXT,REFERENCES,REPLY_WITH_QUOTES X-Scanned-By: MIMEDefang 2.37 cc: cvs-src@FreeBSD.ORG cc: src-committers@FreeBSD.ORG cc: cvs-all@FreeBSD.ORG Subject: Re: cvs commit: src/sys/dev/md md.c X-BeenThere: cvs-src@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: CVS commit messages for the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Sep 2004 22:25:18 -0000 < 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