From: Rich Felker <dalias@aerifal.cx>
To: "Ondřej Bílka" <neleai@seznam.cz>
Cc: Carlos O'Donell <carlos@redhat.com>,
Torvald Riegel <triegel@redhat.com>,
GLIBC Devel <libc-alpha@sourceware.org>,
libc-ports <libc-ports@sourceware.org>
Subject: Re: [PATCH] Unify pthread_once (bug 15215)
Date: Tue, 27 Aug 2013 02:29:00 -0000 [thread overview]
Message-ID: <20130827022912.GI20515@brightrain.aerifal.cx> (raw)
In-Reply-To: <20130826184150.GA8772@domone.kolej.mff.cuni.cz>
On Mon, Aug 26, 2013 at 08:41:50PM +0200, OndÅej BÃlka wrote:
> > No, pthread_once _calls_ tend to be once per access to an interface
> > that requires static data to have been initialized, so possibly very
> > often. On the other hand, pthread_once only invokes the init function
> > once per program instance. I don't see anything that would typically
> > happen once per thread, although I suppose you could optimize out
> > calls to pthread_once with tls:
> >
> Could happen often but dees it? Given need of doing locking you need to
> avoid it in performance critical code. With once per thread I meant an
> patterns:
>
> computation(){
> pthread_once(baz,init); // Do common initialization.
> pthread_create(foo,bar,routine);
> }
>
> or
>
> pthread_create(foo,bar,routine);
>
> with
>
> routine()
> {
> pthread_once(baz,init); // Do common initialization.
> ...
> }
These patterns arise is the library is making threads and using
pthread_once to initialize its static data before making the thread.
I'm thinking instead of the case where your library is being _called_
by multi-threaded code, and using pthread_once to ensure that its data
is safely initialized even if there are multiple threads which might
be racing to be the first caller.
> > static __thread int once_done = 0;
> > static pthread_once_t once;
> > if (!once_done) {
> > pthread_once(&once, init);
> > once_done = 1;
> > }
> >
> > This requires work at the application level, though, and whether it's
> > a net advantage depends a lot on whether multiple threads are likely
> > to be hammering pthread_once on the same once object, and whether the
> > arch has expensive acquire barriers and inexpensive TLS access.
> >
> Actually you can use following if you are concerned about that use cases:
>
> #define pthread_once2(x,y) ({ \
> static __thread int once = 0; \
> if (!once) \
> pthread_once(x,y); \
> once=1; \
> })
Indeed; actually, this could even be done in pthread.h, with some
slight variations, perhaps:
#define pthread_once(x,y) ({ \
pthread_once_t *__x = (x); \
static __thread pthread_once_t *__once; \
if (__once != __x) { \
pthread_once(__x,y); \
__once = __x; \
} \
})
Rich
next prev parent reply other threads:[~2013-08-27 2:29 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-08 14:44 Torvald Riegel
2013-05-08 17:51 ` Rich Felker
2013-05-08 20:47 ` Torvald Riegel
2013-05-08 21:25 ` Rich Felker
2013-05-09 8:39 ` Torvald Riegel
2013-05-09 14:02 ` Rich Felker
2013-05-09 15:14 ` Torvald Riegel
2013-05-09 15:56 ` Rich Felker
2013-05-10 8:31 ` Torvald Riegel
2013-05-10 13:22 ` Rich Felker
2013-05-23 4:15 ` Carlos O'Donell
2013-08-26 12:50 ` Ondřej Bílka
2013-08-26 16:45 ` Rich Felker
2013-08-26 18:41 ` Ondřej Bílka
2013-08-27 2:29 ` Rich Felker [this message]
2013-10-06 0:20 ` Torvald Riegel
2013-10-06 21:41 ` Torvald Riegel
2013-10-07 16:04 ` Joseph S. Myers
2013-10-07 21:53 ` Torvald Riegel
2014-03-31 11:44 ` Will Newton
2014-03-31 20:09 ` Torvald Riegel
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=20130827022912.GI20515@brightrain.aerifal.cx \
--to=dalias@aerifal.cx \
--cc=carlos@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=libc-ports@sourceware.org \
--cc=neleai@seznam.cz \
--cc=triegel@redhat.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).