Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Jun 2016 09:36:57 -0700
From:      John Baldwin <jhb@freebsd.org>
To:        freebsd-hackers@freebsd.org, Bulat Bulat <bltsrc@mail.ru>
Subject:   Re: Turnstile_purpose_ts_free
Message-ID:  <4842510.LxNNL3jKi4@ralph.baldwin.cx>
In-Reply-To: <1465114041.917499339@f133.i.mail.ru>
References:  <1465114041.917499339@f133.i.mail.ru>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sunday, June 05, 2016 11:07:21 AM Bulat Bulat via freebsd-hackers wrote:
>  Hi, guys.
> 
> Have little question about implementation of turnstiles in the kernel.
> That is, what is the purpose of ts_free list in turnstile structure? 
> Related file: kern/subr_turnstile.c
> 
> I understand that turnstile is used for tracking non-sleepable locks, tracking
> blocked threads on that lock, that
> when thread blocks on lock it adds its own turnstile to lock's turnstile's ts_free
> list and regrabs any available turnstile from that lock's turnstile's ts_free list when
> it is waking up.
> 
> Could you please explain, why algorithm does so, why goes through  hassle of
> putting turnstile into ts_free only to regrab it from ts_free later, as it stores pointer to own turnstile
> and could keep it there, using only when it is the first thread to be blocked on a lock.
> 
> There is definitely logic in using ts_free member, but it is a bit unclear for me.
> Would be glad to see your explanation, thanks.

The first thread to block on a lock donates its turnstile to that lock.
However, that turnstile must remain in place and associated with that lock
until all threads have been awakened.  This means that the last thread to be
awakened is the only thread that can take this turnstile.  Thus, suppose
you have two threads (T1 and T2) with turnstiles S1 and S2.  Suppose T1 blocks
on lock L first and T2 blocks second.  At that point, S1 is used as the
turnstile and S2 is in the ts_free list.  If T1 is awakened first, it can't
grab S1 as td_turnstile since T2 is still queued.  Instead, it grabs S2 from
ts_free.  Later when T2 is awakened it grabs S1.  The result in this case is 
that turnstiles are swapped between threads.

-- 
John Baldwin



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4842510.LxNNL3jKi4>