From: Chris Metcalf <cmetcalf@tilera.com>
To: Maxim Kuvyrkov <maxim_kuvyrkov@mentor.com>
Cc: "Joseph S. Myers" <joseph@codesourcery.com>,
GLIBC Devel <libc-alpha@sourceware.org>,
<libc-ports@sourceware.org>
Subject: Re: [PATCH] Optimize libc_lock_lock for MIPS XLP.
Date: Thu, 14 Jun 2012 12:39:00 -0000 [thread overview]
Message-ID: <4FD9DB74.8080905@tilera.com> (raw)
In-Reply-To: <FC4EF172-B43E-4298-A2E9-681FA28650DB@mentor.com>
On 6/14/2012 1:03 AM, Maxim Kuvyrkov wrote:
> These two patches (libc part and ports part) optimize libc_lock_lock() macro that GLIBC uses for locking internally to take advantage of fetch_and_add instruction that is available as an extension on certain processors, e.g., MIPS-architecture XLP.
>
> The libc_lock_lock macros implement boolean lock: 0 corresponds to unlocked state and non-zero corresponds to locked state.
Just to be clear, if you put this comment somewhere when you commit, you
should say locks are tristate, where 0 is unlocked, 1 is locked and
uncontended, and >1 is locked and contended.
> It is, therefore, possible to use fetch_and_add semantics to acquire lock in libc_lock_lock. For XLP this translates to a single LDADD instruction. This optimization allows architectures that can perform fetch_and_add faster than compare_and_exchange, such situation is indicated by defining the new macro "lll_add_lock".
>
> The unlocking counterpart doesn't require any change as it is already uses plain atomic_exchange operation, which, incidentally, also supported on XLP as a single instruction.
This seems like it would work well for a single thread acquiring the lock,
but I have some questions about it in the presence of multiple threads
trying to acquire the lock.
First, the generic __lll_lock_wait() code assumes the contended value is
exactly "2". So if two or more threads both try and fail to acquire the
lock, the value will be >2. This will cause the waiters to busywait,
spinning on atomic exchange instructions, rather than calling into
futex_wait(). I think it might be possible to change the generic code to
support the more general ">1" semantics of contended locks, but it might be
a bit less efficient, so you might end up wanting to provide overrides for
these functions on MIPS. Even on MIPS it might result in a certain amount
of spinning since you'd have to hit the race window correctly to feed the
right value of the lock to futex_wait.
Second, if a lock is held long enough for 4 billion threads to try to
acquire it and fail, you will end up with an unlocked lock. :-) I'm not
sure how likely this seems, but it is a potential issue. You might
consider, for example, doing a cmpxchg on the contended-lock path to try to
reset the lock value back to 2 again; if it fails, it's not a big deal,
since statistically I would expect the occasional thread to succeed, which
is all you need.
--
Chris Metcalf, Tilera Corp.
http://www.tilera.com
next prev parent reply other threads:[~2012-06-14 12:39 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-14 5:04 Maxim Kuvyrkov
2012-06-14 12:39 ` Chris Metcalf [this message]
2012-06-15 1:21 ` Maxim Kuvyrkov
2012-06-15 2:44 ` Chris Metcalf
2012-06-15 2:50 ` Maxim Kuvyrkov
2012-06-27 21:45 ` Maxim Kuvyrkov
2012-06-28 17:30 ` Chris Metcalf
2012-07-06 19:42 ` Tom de Vries
2012-08-14 4:00 ` Maxim Kuvyrkov
2012-08-14 19:33 ` Chris Metcalf
2012-08-14 21:30 ` Maxim Kuvyrkov
2012-08-14 21:40 ` Joseph S. Myers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4FD9DB74.8080905@tilera.com \
--to=cmetcalf@tilera.com \
--cc=joseph@codesourcery.com \
--cc=libc-alpha@sourceware.org \
--cc=libc-ports@sourceware.org \
--cc=maxim_kuvyrkov@mentor.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).