mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Explaining cond var destroy [Re: [musl] C threads, v3.0]
Date: Tue, 5 Aug 2014 23:52:13 -0400	[thread overview]
Message-ID: <20140806035213.GR1674@brightrain.aerifal.cx> (raw)
In-Reply-To: <1407144603.8274.248.camel@eris.loria.fr>

On Mon, Aug 04, 2014 at 11:30:03AM +0200, Jens Gustedt wrote:
> diff --git a/src/thread/cnd_destroy.c b/src/thread/cnd_destroy.c
> new file mode 100644
> index 0000000..7d6f3a1
> --- /dev/null
> +++ b/src/thread/cnd_destroy.c
> @@ -0,0 +1,21 @@
> +#include "pthread_impl.h"
> +#include <threads.h>
> +
> +/* The behavior of cnd_destroy is undefined if cnd is still in
> +   use. The choice for pthread_cond_destroy in that situation is to
> +   wake up all users before destroying. I am not sure that we should
> +   do it like that here, too. Alternatives would be:
> +   - complain by using perror or equivalent
> +   - assert that there is no waiter
> +   - abort when there is a waiter
> +   - do nothing
> +   */
> +void cnd_destroy(cnd_t *c)
> +{
> +	int cnt;
> +	c->_c_destroy = 1;
> +	if (c->_c_waiters)
> +		__wake(&c->_c_seq, -1, 0);
> +	while ((cnt = c->_c_waiters))
> +		__wait(&c->_c_waiters, 0, cnt, 0);
> +}

The above comment is incorrect; I'll try to explain. At least for
POSIX cond vars, per POSIX, at least one thread that has called
pthread_cond_[timed]wait ceases to be a waiter as soon as
pthread_cond_signal is called, and all threads which have called
pthread_cond_[timed]wait ceast to be waiters as soon as
pthread_cond_broadcast is called. This means that, if a thread calling
pthread_cond_signal or pthread_cond_broadcast has updated the
predicate such that no threads will retry pthread_cond_[timed]wait or
remain as waiters, it may IMMEDIATELY call pthread_cond_destroy
without violating the constraint that pthread_cond_destroy can only be
called when there are no waiters.

Since waiters have additional work to do on the memory associated with
the pthread_cond_t object after the futex wait completes, and since we
do not want to force them to wake and finish this work as part of
pthread_cond_signal/broadcast (this would be expensive on every
signal), I've put the code to finish waiting for the waiters to wake
up in pthread_cond_destroy.

If you think this is a bad idea, I'd be willing to hear alternate
ideas. I'm not really happy with the cond var implementation (if
nothing else, the sequence number thing is an ugly hack and not 100%
robust, I think) and at some point I'd like to redesign it.

Rich


  parent reply	other threads:[~2014-08-06  3:52 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-04  9:30 C threads, v3.0 Jens Gustedt
2014-08-04  9:33 ` Jens Gustedt
2014-08-04 14:50 ` Rich Felker
2014-08-04 16:48   ` Jens Gustedt
2014-08-04 17:06     ` Rich Felker
2014-08-04 22:16       ` Jens Gustedt
2014-08-04 22:36         ` Rich Felker
2014-08-06  3:52 ` Rich Felker [this message]
2014-08-06  8:43   ` Explaining cond var destroy [Re: [musl] C threads, v3.0] Jens Gustedt
2014-08-06  9:41     ` Jens Gustedt
2014-08-06 10:03       ` Rich Felker
2014-08-06 10:32         ` Jens Gustedt
2014-08-06 16:15           ` Rich Felker
2014-08-06 16:56             ` Jens Gustedt
2014-08-06 17:32               ` Rich Felker
2014-08-06 20:55                 ` Jens Gustedt
2014-08-06 22:04                   ` Rich Felker
2014-08-06 22:43                     ` Jens Gustedt
2014-08-06 23:15                       ` Rich Felker
2014-08-07  7:50                         ` Jens Gustedt
2014-08-07 10:52                           ` Szabolcs Nagy
2014-08-07 11:03                             ` Jens Gustedt
2014-08-07 16:13                           ` Rich Felker
2014-08-07 16:47                             ` Jens Gustedt
2014-08-07 17:25                               ` Rich Felker
2014-08-08  9:20                                 ` Jens Gustedt
2014-08-08 16:53                                   ` Rich Felker
2014-08-08 19:14                                   ` Rich Felker
2014-08-08 20:48                                     ` Rich Felker
2014-08-09  6:47                                       ` Jens Gustedt
2014-08-12  2:50                                         ` Rich Felker
2014-08-12  7:04                                           ` Jens Gustedt
2014-08-12 16:01                                             ` Rich Felker
2014-08-12 19:09                                               ` Jens Gustedt
2014-08-12 21:18                                                 ` Rich Felker
2014-08-13  6:43                                                   ` Jens Gustedt
2014-08-13  7:19                                                     ` Jens Gustedt
2014-08-06  9:50     ` 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=20140806035213.GR1674@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).