From owner-freebsd-current@FreeBSD.ORG Thu Feb 5 11:30:37 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4E98216A51D for ; Thu, 5 Feb 2004 11:30:37 -0800 (PST) Received: from mail2.speakeasy.net (mail2.speakeasy.net [216.254.0.202]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2AF9343D77 for ; Thu, 5 Feb 2004 11:30:00 -0800 (PST) (envelope-from jhb@FreeBSD.org) Received: (qmail 14630 invoked from network); 5 Feb 2004 19:28:51 -0000 Received: from dsl027-160-063.atl1.dsl.speakeasy.net (HELO server.baldwin.cx) ([216.27.160.63]) (envelope-sender ) encrypted SMTP for ; 5 Feb 2004 19:28:51 -0000 Received: from 10.50.40.205 (gw1.twc.weather.com [216.133.140.1]) by server.baldwin.cx (8.12.10/8.12.10) with ESMTP id i15JSXM6014155; Thu, 5 Feb 2004 14:28:47 -0500 (EST) (envelope-from jhb@FreeBSD.org) From: John Baldwin To: popsong old , freebsd-current@freebsd.org Date: Thu, 5 Feb 2004 14:18:54 -0500 User-Agent: KMail/1.5.4 References: <20040205022959.94712.qmail@web60607.mail.yahoo.com> In-Reply-To: <20040205022959.94712.qmail@web60607.mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200402051418.54247.jhb@FreeBSD.org> X-Spam-Checker-Version: SpamAssassin 2.55 (1.174.2.19-2003-05-19-exp) Subject: Re: three locks and lock order reversal? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 05 Feb 2004 19:30:37 -0000 On Wednesday 04 February 2004 09:29 pm, popsong old wrote: > Hi, > > The test code below will result witness's warning of "lock order > reversal": > > /* lock A then B */ > mtx_lock(&A_mtx); > mtx_lock(&B_mtx); > mtx_unlock(&B_mtx); > mtx_unlock(&A_mtx); > > /* lock B then C */ > mtx_lock(&B_mtx); > mtx_lock(&C_mtx); > mtx_unlock(&C_mtx); > mtx_unlock(&B_mtx); > > /* lock C then A, witness will complaint! */ > mtx_lock(&C_mtx); > mtx_lock(&A_mtx); > mtx_unlock(&A_mtx); > mtx_unlock(&C_mtx); > > But the code seems healthy and will not cause dead locking. So I guess > that the lock order relationship should not be transferrable. Or am I > missing something? If one thread does A then B, another thread does B then C, and a third thread does C then A you can deadlock if each thread gets the first lock and blocks on the second lock. Thread 1 wants B and holds A, thread 2 holds B and wants C, and thread 3 wants A and holds C. Thread 3 will not giveup C until it gets A. Thread 1 holds A and won't give it up until it gets B. Thread 2 holds B and won't give it up until it gets C which is held by thread 3. Hence, deadlock. -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve" = http://www.FreeBSD.org