three locks and lock order reversal?
John Baldwin
jhb at FreeBSD.org
Thu Feb 5 11:30:37 PST 2004
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
More information about the freebsd-current
mailing list