fine grained locking and traversing linked lists

John-Mark Gurney gurney_j at resnet.uoregon.edu
Fri Sep 3 16:27:20 PDT 2004


Maksim Yevmenkin wrote this message on Fri, Sep 03, 2004 at 15:04 -0700:
> >If you implement this, I strongly recommend making the lists singally
> >linked to avoid the possiablity of this deadlock.
> 
> yes, i was thinking the same too. but double-linked list gives o(1) 
> deletion (which is very nice :)
> 
> so i guess the restrictions are:
> 
> 1) lock order: item "x", then item "x->previous" (if any), then item 
> "x->next" (if any)
> 
> 2) always scan list forward
> 
> 3) never use "x->previuos" field (except for "x" locking)
> 
> it can even be done with standard sys/queue.h macros :) i just ran 
> wintess test and it only complained about "acquiring duplicate lock of 
> same type".

This can still lead to a dead lock:

obja <-> objb <-> objc <-> objd

T1: lock (objb), lock(obja)
T2: lock (objc), lock(objb, but blocks for T1)
T1: lock (objc, blocks for T2)

So, one solution is to use a list lock, and a refcnt'd object.  Then
all of the next/previous pointers are protected by the list lock, and
the list gets one ref...  Since you're using refcnts, you can keep a
ref, unlock the object lock, lock the list lock, before relocking the
object lock if necessary...

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."


More information about the freebsd-current mailing list