mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: bug in pthread_cond_broadcast
Date: Tue, 12 Aug 2014 20:30:11 -0400	[thread overview]
Message-ID: <20140813003011.GH12888@brightrain.aerifal.cx> (raw)
In-Reply-To: <1407883819.15134.160.camel@eris.loria.fr>

On Wed, Aug 13, 2014 at 12:50:19AM +0200, Jens Gustedt wrote:
> > I'd like to find a fix that
> > would be acceptable in the 1.0.x branch and make that fix before
> > possibly re-doing the cond var implementation (a fix which wouldn't be
> > suitable for backporting).
> 
> Some thoughts:
> 
> Basically, in "unwait" there shouldn't be any reference to c-> .  No
> pending thread inside timedwait should ever have to access the
> pthread_cond_t, again, it might already heavily used by other threads.

As far as I can see, there must be: since "unwait" potentially
releases the association of the mutex with the cond var, "unwait" and
broadcast need to mutually exclude one another, so that broadcast can
know whether there are zero waiters (in which case the mutex can
legally be destroyed by the last waiter, and broadcast cannot access
it) or at least one waiter that cannot re-acquire the mutex until the
broadcast is finished.

The only way I can see around this "must" is to do away with requeue
entirely and have broadcast wake all waiters, never inspecting the
mutex at all. This is certainly a lot simpler (it's what we do for
process-shared cond vars anyway) but performance is much worse.

> The signalling or broacasting thread (waker) should do most of the
> bookkeeping on the waiters counts. This might be done by
> 
>  - lock _c_lock
> 
>  - if there are no waiters, unlock _c_lock and quit
> 
>  - requeue the wanted number of threads (1 or everybody) from the cnd
>    to the mtx. requeue tells us how many threads have been requeued,
>    and this lets us deduce the number of threads that have been woken
>    up.

If you requeue here, where does any wake happen?

>  - verify that all wanted waiters are in, otherwise repeat the requeue
>    operation. (this should be a rare event)

This step is not possible. One or more waiters could be in signal
handlers which interrupted the wait, in which case the futex wait will
not resume until the signal handler returns. Such a retry loop could
run forever (e.g. if the signal handler is waiting for an event that
will only be performed by the [cond-var-]signaling thread after the
operation finishes).

>  - do the bookkeeping: update the cond-waiters count and add the right
>    amount to the mtx-waiters
> 
>  - unlock _c_lock
> 
> On the waiter side, you'd have to distinguish a successful wakeup by a
> waker from a spurious wakeup. Only for the later the waiter has to do
> the bookkeeping. This can only happen as long as the waker is in the
> "requeue" loop.

I don't understand what you mean.

> The only disadvantage that I see with such a procedure is that the
> waker is holding _c_lock when going into the futex call. 

This is probably a small issue compared to everything else.

Rich


  parent reply	other threads:[~2014-08-13  0:30 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-11 23:58 Jens Gustedt
2014-08-12 16:50 ` Szabolcs Nagy
2014-08-12 17:19   ` Rich Felker
2014-08-12 18:18     ` Jens Gustedt
2014-08-12 21:20       ` Rich Felker
2014-08-12 22:50         ` Jens Gustedt
2014-08-12 23:33           ` Rich Felker
2014-08-13  4:11             ` Rich Felker
2014-08-13  0:30           ` Rich Felker [this message]
2014-08-13  7:00             ` Jens Gustedt
2014-08-13 12:34               ` Rich Felker

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=20140813003011.GH12888@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).