mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: NRK <nrk@disroot.org>
Cc: musl@lists.openwall.com
Subject: Re: [musl] Question:Why musl call a_barrier in __pthread_once?
Date: Wed, 17 May 2023 09:12:40 -0400	[thread overview]
Message-ID: <20230517131240.GO4163@brightrain.aerifal.cx> (raw)
In-Reply-To: <20230517111728.sv3o2fa6d272cqbo@gen2.localdomain>

On Wed, May 17, 2023 at 05:17:28PM +0600, NRK wrote:
> On Wed, May 17, 2023 at 06:07:09PM +0800, 847567161 wrote:
> > Why musl add&nbsp;a_barrier() in if branch here?
> 
> It's explained in the comment:
> 
> | but ensure that effects of the init routine are visible to the caller.
> 
> > What happend if we remove it? Could you give me more details?
> 
> If you remove the barrier, then the calling code may not see the effects
> of the init function, especially on cpus with "relaxed" memory ordering.

Exactly. The whole purpose of pthread_once is to synchronize the
caller with the completion of the initialization function, which may
have happened in another thread.

> Which leads me to the question, are you familiar with cpu memory
> ordering? If not, I'd recommend reading this:
> https://research.swtch.com/hwmm
> 
> One more thing, what's the motivation behind trying to remove the
> barrier in the first place?

The barrier is expensive, and discourages use of
pthread_once/call_once where it would be a good primitive to do
thread-safe global init stuff.

There is an alternate algorithm for pthread_once that doesn't require
a barrier in the common case, which I've considered implementing. But
it does need efficient access to thread-local storage. At one time,
this was a kinda bad assumption (especially legacy mips is horribly
slow at TLS) but nowadays it's probably the right choice to make, and
we should check that out again...

Rich

  parent reply	other threads:[~2023-05-17 13:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-17 10:07 [musl] =?gb18030?B?UXVlc3Rpb26juldoeSBtdXNsIGNhbGyBMIQyYV9iYXJyaWVyIGluIF9fcHRocmVhZF9vbmNlPw==?= =?gb18030?B?ODQ3NTY3MTYx?=
2023-05-17 10:37 ` [musl] Question:Why musl call a_barrier in __pthread_once? alice
2023-05-17 11:17 ` NRK
2023-05-17 12:11   ` =?gb18030?B?UmU6IFttdXNsXSBRdWVzdGlvbqO6V2h5IG11c2wgY2FsbIEwhDJhX2JhcnJpZXIgaW4gX19wdGhyZWFkX29uY2U/?= =?gb18030?B?ODQ3NTY3MTYx?=
2023-05-17 13:12   ` Rich Felker [this message]
2023-05-18  2:49 =?gb18030?B?UmU6IFJlOiBbbXVzbF0gUXVlc3Rpb26juldoeSBtdXNsIGNhbGwgYV9iYXJyaWVyIGluIF9fcHRocmVhZF9vbmNlPw==?= =?gb18030?B?ODQ3NTY3MTYx?=
2023-05-18 12:23 ` Re: [musl] Question:Why musl call a_barrier in __pthread_once? Szabolcs Nagy
2023-05-18 13:29   ` Rich Felker
2023-05-18 14:01     ` Jₑₙₛ Gustedt
2023-05-18 14:08       ` 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=20230517131240.GO4163@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=nrk@disroot.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.
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).