public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: dumblob <dumblob@gmail.com>, Libc-help <libc-help@sourceware.org>
Subject: Re: glibc pthreads and optimized locks
Date: Mon, 7 Jun 2021 18:53:00 -0300	[thread overview]
Message-ID: <44a46000-0b6f-0c9b-54e7-8b9982044028@linaro.org> (raw)
In-Reply-To: <CAGnk-Z-U4oOmAcn=UfuS7mZ-cebnVQnkFjmQKzicZLAHNHf0TA@mail.gmail.com>



On 07/06/2021 10:30, dumblob via Libc-help wrote:
> Dear glibc experts,
> 
> Does anybody know whether glibc pthreads implement the fast lock
> algorithms as https://github.com/AlexeyAB/object_threadsafe does?
> 
> A detailed description of these algorithms is in
> https://www.codeproject.com/Articles/1183423/We-Make-a-std-shared-mutex-10-Times-Faster
> and some more optimizations in
> https://www.codeproject.com/Articles/1183446/Thread-safe-std-map-with-the-speed-of-lock-free-ma
> . Btw. I'm aware these articles discuss C++, but the same principles
> apply to locks in C.
> 
> I'm asking because pthreads are known to be rather slower in general
> and if such optimized variants of locks are not implemented, I'll
> propose implementing them in few specific apps which would benefit
> from the optimizations (e.g. the main implementation of the V
> language).

I haven't read al the links in details (the C++ code is specially quite 
hard to follow...), but as fair as I know the std::shared_ptr (and
this alternate implementation) maps more directly to pthread_rwlock_t
instead of pthread_mutex_t.

Torvald's Riegel optimized back it on glibc 2.25 and there were a couple 
of fixed back then.  He optimized the fast path so a read lock requires
only a single atomic addition if the lock is or was previously acquired 
by other readers while releasing the lock is a single CAS if there are 
no concurrent writers.  The main constraint is the pthread_rwlock_t is
it shall follow POSIX requirements regarding memory semantic, from the
implementation comment:

| /* A reader--writer lock that fulfills the POSIX requirements (but operations
|   on this lock are not necessarily full barriers, as one may interpret the
|   POSIX requirement about "synchronizing memory").  All critical sections are
|   in a total order, writers synchronize with prior writers and readers, and
|   readers synchronize with prior writers.

So I would suggest you to first evaluate the std::shared_ptr and this
'fast lock algorithms' against pthread_rwlock_t.  The pthread_rwlock_t
does block by calling futex, so it is not really lock-free.

You might try to provide a pthread_rwlock_t which lock-free, but then
you will need to check if this provides *all* the POSIX requirements.
I am not sure this would be possible without something really clever
and somewhat hacky as rseq support.

Otherwise, I don't think glibc is the correct place to support alternate
locking mechanism (you have multiple specialized libraries such as the
one you referred that I think is a better way to provide it).

One TODO you might want to explore is the use o Hardware Transaction
Memory on pthread_rwlock_t. It was previously supported, but with
Torvald newer implementation it was not added back.

      reply	other threads:[~2021-06-07 21:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-07 13:30 dumblob
2021-06-07 21:53 ` Adhemerval Zanella [this message]

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=44a46000-0b6f-0c9b-54e7-8b9982044028@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=dumblob@gmail.com \
    --cc=libc-help@sourceware.org \
    /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).