Re: three locks and lock order reversal?

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 5 Feb 2004 14:18:54 -0500
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_at_FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
"Power Users Use the Power to Serve"  =  http://www.FreeBSD.org
Received on Thu Feb 05 2004 - 10:30:37 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:41 UTC