From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 15008 invoked from network); 17 May 2023 13:12:58 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 17 May 2023 13:12:58 -0000 Received: (qmail 13343 invoked by uid 550); 17 May 2023 13:12:55 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 12284 invoked from network); 17 May 2023 13:12:54 -0000 Date: Wed, 17 May 2023 09:12:40 -0400 From: Rich Felker To: NRK Cc: musl@lists.openwall.com Message-ID: <20230517131240.GO4163@brightrain.aerifal.cx> References: <20230517111728.sv3o2fa6d272cqbo@gen2.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230517111728.sv3o2fa6d272cqbo@gen2.localdomain> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] =?utf-8?B?UXVlc3Rpb27vvJpXaHkg?= =?utf-8?Q?musl_call=C2=A0a=5Fbarrier?= in __pthread_once? 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 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