Use turnstile to implement sx_lock

John Baldwin jhb at freebsd.org
Thu Dec 15 09:57:25 PST 2005


On Thursday 15 December 2005 11:17 am, rookie wrote:
> 2005/12/15, John Baldwin <jhb at freebsd.org>:
> > You have to add a second queue to the turnstile and make priority
> > propagation
> > still work, etc.  Mutexes would just use the exclusive queue all the time
> > whereas rwlocks would use both queues.  This is the hard part of the
> > rwlock project.  I've sort-of started on this but haven't gotten very far
> > at all in my jhb_lock p4 branch.
>
> I'm working on the same problem too and I found another solution
> beacause, in order to mantain a clean design, maybe modifying
> turnstiles code is not the better idea.
> If we have a thread trying to slock it just blocks if sx is held in
> "exclusive mode" by another thread so it's enough a simple turnstile
> as for other blocking locks and we have nice priorty propagation. The
> real problem is when we try to xlock. A xlocking thread blocks if sx
> is held in "shared mode" (even by different owners) so we might
> mantain a track of every slocking thread (through a tailqueue, a
> static array or whatever) in order to have a priority propagation for
> those. I think (I didn't implement yet) it can be done outside the
> turnstile context so it seems we don't necessary need to modify.
> Finally, we might consider one thing: turnstile has just one owner
> while we have (in the slocks) more than one and so what's the real
> owner?
> We could use a "head thread" per track which must be treacted
> carefully (EG: on slocking it we might switch the turnstile owner).
>
> What do you think about?

As per the description, I suggest go reading up on rwlocks in Solaris 
Internals.  The method they use is turnstiles with 2 queues, and for an 
rwlock that is read locked, they have the concept of the 'owner of record' 
which is basically the first thread to acquire a read lock, and you just bump 
their priority and no one else's.  When they drop the read lock, if someone 
else has it, you just have no one to propagate the priority to.  BTW, I don't 
think sx locks should use turnstiles, but a new rwlock primitive that has a 
similar API.  The reason is that sx locks can be held across sleep right now 
(and are often used that way), but a rwlock that uses turnstiles won't be 
sleepable, just as mutexes aren't sleepable.

-- 
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-smp mailing list