From: Jens Gustedt <jens.gustedt@inria.fr>
To: musl@lists.openwall.com
Subject: Re: Explaining cond var destroy [Re: [musl] C threads, v3.0]
Date: Wed, 13 Aug 2014 08:43:30 +0200 [thread overview]
Message-ID: <1407912210.4951.15.camel@eris.loria.fr> (raw)
In-Reply-To: <20140812211837.GC12888@brightrain.aerifal.cx>
[-- Attachment #1: Type: text/plain, Size: 3712 bytes --]
Am Dienstag, den 12.08.2014, 17:18 -0400 schrieb Rich Felker:
> On Tue, Aug 12, 2014 at 09:09:21PM +0200, Jens Gustedt wrote:
> > Rich,
> > thanks a lot for looking into the code.
> >
> > Am Dienstag, den 12.08.2014, 12:01 -0400 schrieb Rich Felker:
> > > As far as I can tell, the only thing that's saving you from sending
> > > futex wakes after free is that you're just using spinlocks.
> >
> > No, I don't think so. These protect critical sections at the beginning
> > of the cnd_t calls. The cnd_*wait calls hold the mutex at that time
> > anyhow, so even if these would be implemented with mutexes (an extra
> > one per cnd_t to protect the critical section) this wouldn't cause
> > late wakes, I think.
>
> I was talking about the unref-and-free code that's using spinlocks.
I don't understand, the unref-and-free code doesn't use spinlocks. It
just uses an atomic_fetch_sub of 1 to determine if it is the last
user. If it is not, it does nothing and returns.
(This is indeed supposing that atomic_fetch_sub is "lock-free" in the
sense of the C standard, which basically means that it has no
observable state from a signal handler. All architectures I know of
have that property, but my knowledge is limited.)
> If
> it were using mutexes that don't protect against making futex wake
> calls after the atomic unlock, a previous unref could send the wake
> after the final one freed the object. So in effect, if you use a mutex
> here, I think the wake-after-free issue has just been moved to a
> different object, not solved.
>
> > > This is an
> > > extremely expensive solution: While contention is rare, as soon as you
> > > do hit contention, if there are many threads they all pile on and
> > > start spinning, and the time to obtain a lock (and cpu time/energy
> > > spent waiting) grows extremely high. And of course it becomes infinite
> > > if you have any threads of differing priorities and the low-priority
> > > thread has the lock...
> >
> > I think you dramatize a bit :)
>
> Perhaps. :)
>
> > It is very unlikely that a thread that reaches the critical section is
> > unscheduled *during* that critical section. If it is unscheduled, you
> > are right, the wait can be long. But that event is very unlikely, so
> > the average time inside the critical section is still short, with a
> > probability distribution that is a bit skewed because of the
> > outliers.
>
> Yes. The general pathology of spinlocks is that they give extremely
> high latency and cpu load in an extremely low probability worst-case.
>
> > (And then there is no concept of different scheduling priorities for C
> > threads, all of them are equal.)
>
> Indeed, but there's no reason these functions couldn't end up getting
> called from a POSIX program using a C11 library. This is the normal
> expected usage for mutexes (i.e. you're writing a library that needs
> to be thread-safe but you don't want to depend on POSIX -- in practice
> the calling application is unlikely to be using C11 thrd_create
> because it sucks :) and perhaps less likely but definitely not
> impossible for cond vars.
Hm, C threads are meant primarily as a portable and simple user space
tool and as a means to provide a model of parallelism as far as the C
standard is concerned. And that model is flat and has no hierarchy
among threads.
Jens
--
:: INRIA Nancy Grand Est ::: AlGorille ::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536 ::
:: :::::::::::::::::::::: gsm France : +33 651400183 ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
next prev parent reply other threads:[~2014-08-13 6:43 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 ` Explaining cond var destroy [Re: [musl] C threads, v3.0] Rich Felker
2014-08-06 8:43 ` 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 [this message]
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=1407912210.4951.15.camel@eris.loria.fr \
--to=jens.gustedt@inria.fr \
--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).