Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 5 Feb 2004 14:18:54 -0500
From:      John Baldwin <jhb@FreeBSD.org>
To:        popsong old <oldpopsong@yahoo.com>, freebsd-current@freebsd.org
Subject:   Re: three locks and lock order reversal?
Message-ID:  <200402051418.54247.jhb@FreeBSD.org>
In-Reply-To: <20040205022959.94712.qmail@web60607.mail.yahoo.com>
References:  <20040205022959.94712.qmail@web60607.mail.yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <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?200402051418.54247.jhb>