From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 4D4FB1F55B for ; Sat, 23 May 2020 06:49:47 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 18CFA385DC1C; Sat, 23 May 2020 06:49:46 +0000 (GMT) Received: from port70.net (port70.net [81.7.13.123]) by sourceware.org (Postfix) with ESMTP id 3EAEF385DC1C for ; Sat, 23 May 2020 06:49:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3EAEF385DC1C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=port70.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nsz@port70.net Received: by port70.net (Postfix, from userid 1002) id A4793ABEC0B8; Sat, 23 May 2020 08:49:41 +0200 (CEST) Date: Sat, 23 May 2020 08:49:41 +0200 From: Szabolcs Nagy To: Rich Felker Subject: Re: [PATCH 2/2] manual: Document __libc_single_threaded Message-ID: <20200523064941.GD26190@port70.net> References: <877dx45low.fsf@oldenburg2.str.redhat.com> <20200522105458.GB29518@arm.com> <20200522150720.GR1079@brightrain.aerifal.cx> <20200522161413.GU1079@brightrain.aerifal.cx> <871rnb3nue.fsf@oldenburg2.str.redhat.com> <20200522172826.GW1079@brightrain.aerifal.cx> <87h7w727i4.fsf@oldenburg2.str.redhat.com> <20200522174932.GY1079@brightrain.aerifal.cx> <20200522192249.GC24880@arm.com> <20200522195326.GB1079@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200522195326.GB1079@brightrain.aerifal.cx> User-Agent: Mutt/1.10.1 (2018-07-13) X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Florian Weimer , libc-alpha@sourceware.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" * Rich Felker [2020-05-22 15:53:26 -0400]: > On Fri, May 22, 2020 at 08:22:50PM +0100, Szabolcs Nagy wrote: > > so i think the safest implementation never sets > > __libc_single_threaded back to true and second safest > > is one that only sets it back to true in pthread_join > > when there were no detached threads (or if using some > > os api it can verify that there really is only one > > kernel thread). > > > > if we want to allow kernel entities to be around > > but still tell the user when "as far as the libc > > is concerned there is only one thread", then i > > think __libc_single_threaded needs to be an extern > > call (that acts as a compiler barrier). > > Do relaxed order atomics provide a compiler barrier? If so, I think > SYS_membarrier combined with one could be sufficient However using > atomic type for a public interface like this is a bit of a > compatibility thorn (requires compiler and std level supporting it). > > Of course the same could be achieved with requirement to use a manual > compiler barrier (dummy asm) but I think it's error-prone to assume > the application would do it correctly. no, relaxed atomics do not order unrelated memory accesses. but a trick like you proposed for the musl internal need_locks may work: __libc_single_threaded can go back to 1 when there is only one thread left executing user code *and* that thread called a libc function that has an acquire barrier. (if thread exit has a release barrier then this should work) this can allow earlier single threaded detection than only considering pthread_join: e.g. stdio, malloc etc may do a check and update the global after an acquire barrier, however the compiler must not cache globals across libc calls for this to work.