DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 44TBTTXF1630267
Authentication-Results: delorie.com;
	dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=SyqMp76M
X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 35623384F4BA
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1716982168;
	bh=soVQyKjo0CUvGjooYgCboLssoNpyZATFglJpNuBxuxc=;
	h=To:Cc:Subject:Date:In-Reply-To:References:List-Id:
	 List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe:
	 From:Reply-To:From;
	b=SyqMp76MwGtyPEKImSTEnFD8I3iXQOPCQJdSpdtAJSdjQXHlEF4ALqy9GKbC3P2Gr
	 T1EFL0hCcljCAECSD7K4+Gb8imcgil88UIN2pgZeHutEHSDRRFxcXzqfARWzEmImud
	 OvvNDvNhv4cVInAtKhFkDqxJNSYgrQAIfdP+TTjU=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D95DF3858288
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D95DF3858288
ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1716982148; cv=pass;
 b=FrCCrrI6/gyY3XJ/Hh/HLM0hWCI07O3Njfro1OCZjRiQiPbWNrIe2y/Nmdlo9CsPxroM35POv5bi8mM6zPkbKJBe0z+4CJh4hBkBhRRBiuod4qHNfN3T9C+6Mbchsm+3JXNid4pJjv0p0677h7EX26NskA4qKu6sO0PbDqKM7p8=
ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key;
 t=1716982148; c=relaxed/simple;
 bh=Bm+dV0CI2wzCPunObC22oNhC/90WocM8OK0+3NzxB8s=;
 h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID:
 MIME-Version;
 b=OrPUP8C5/hhWcfBA71wqyKZVoCPLZnaDOEqlddjRCSiMf9CvKal473E/hLxgBR961j1tI+MkTXD1wpoqeQHqirOysUKrshXZzMvYrQQxwcWHW1L3kLpYpDYTE8M59p/hV8irzTyq86p/GHLIH6SedfWbTrDBWfgBx1pzIzWD89o=
ARC-Authentication-Results: i=2; server2.sourceware.org
ARC-Seal: i=1; a=rsa-sha256; t=1716982144; cv=none;
 d=strato.com; s=strato-dkim-0002;
 b=hvFbe7h4QF3IW66owohWR595gYHUKxz9d18y/zqzYiT9fgOfydvTu3ctW4/K/Z9+pH
 bx6riqZvHiTmuT8pXpz0y7mBRAGPFwLDRCg2P4qcQByzCY1sWToSjR7FeS/Lwqq69Gbg
 cWGH6Cl8UI8XShDwuWW3BSes1V6ATyxArUlyXqZBAWXIKQgnFLa2fxll6btTrNDzduz6
 MLpOIXaJVupVs7hOF6gYozKXR5942e3o413bx1hJSnj2oMKbElPNeHIrpeMBu9+0Njs1
 MQlJGJOHhBQaNZKNh/fLTsOv8essyf7O2GdNsbaJs2PJHVwhP9TjVi9gq6jSx3oyK8aE
 E5RA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1716982144;
 s=strato-dkim-0002; d=strato.com;
 h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date:
 From:Subject:Sender;
 bh=77bwZXBmUR3QBwZ/PtQHGAn8UiFVOIy/3HtzZle/fLs=;
 b=fdJF0DCGH+MIhUkfSlvlqC4L3bXCIGtivOjvVMxFMq9BABlkfgGNDXo4MC1rX3NlRO
 D4bpLbPEg62KQ77vi68TmYahyvqyWeg9XS4hdtFEqI/xqqmAJ8WyawJ4JKd97FaUwVcA
 s7rfCAr9wxtZJtyAPF6awkWsvpOvxvqDReooBmU7+uZaNZnFQwbNrzizo9I0llpxGQn9
 xq73qOv1eBessHz0xc1CbhJ9sCTF6zRQINPpTys5olxAnXDtSdLWOJDqlA8iSdop/Vvq
 jrXfgFz1pV4lCjVIo42PHoGtT1x6h1JZQiigSlTfZWxgXt1QXPqv6JBs2OWi38/P/XYS
 yh7g==
ARC-Authentication-Results: i=1; strato.com;
    arc=none;
    dkim=none
X-RZG-CLASS-ID: mo00
X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpPAiqFt2JhGvFnRN6BsfqrAeOb8"
To: Takashi Yano <takashi.yano@nifty.ne.jp>
Cc: cygwin@cygwin.com
Subject: Re: multithreading broken in Cygwin 3.5.3
Date: Wed, 29 May 2024 13:29:04 +0200
Message-ID: <11603545.TeLrTgmnuU@nimes>
In-Reply-To: <20240529200448.fde3a5904e9b17cbdf12f20d@nifty.ne.jp>
References: <3631253.RrQHTzY0li@nimes> <4580924.dSzdyWo4uF@nimes>
 <20240529200448.fde3a5904e9b17cbdf12f20d@nifty.ne.jp>
MIME-Version: 1.0
X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00, DKIM_SIGNED,
 DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE,
 RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP,
 T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on
 server2.sourceware.org
X-BeenThere: cygwin@cygwin.com
X-Mailman-Version: 2.1.30
Precedence: list
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-request@cygwin.com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=subscribe>
From: Bruno Haible via Cygwin <cygwin@cygwin.com>
Reply-To: Bruno Haible <bruno@clisp.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: cygwin-bounces+archive-cygwin=delorie.com@cygwin.com
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie.com@cygwin.com>

Takashi Yano wrote:
> > My workaround implementation of pthread_once (in gnulib) looks like this:
> > 
> >   /* This would be the code, for
> >        typedef struct
> >          {
> >            pthread_mutex_t mutex;
> >            _Atomic unsigned int num_threads;
> >            _Atomic unsigned int done;
> >          }
> >        pthread_once_t;
> >    */
> >   if (once_control->done == 0)
> >     {
> >       once_control->num_threads += 1;
> >       pthread_mutex_lock (&once_control->mutex);
> >       if (once_control->done == 0)
> >         {
> >           (*initfunction) ();
> >           once_control->done = 1;
> >         }
> >       pthread_mutex_unlock (&once_control->mutex);
> >       if ((once_control->num_threads -= 1) == 0)
> >         pthread_mutex_destroy (&once_control->mutex);
> >     }
> > 
> > It makes sure that
> >   - The last thread that had been dealing with the mutex deletes
> >     the mutex.
> >   - Once the mutex is deleted, is it never again accessed. The
> >     entry test of the 'done' boolean ensures this.
> 
> Thanks for the advice.
> 
> The problem is that we cannot change the type of pthread_once_t
> for binary compatibility. My patch stops to use
> pthread_mutex_t mutex in pthread_once_t however it cannot be
> deleted despite it is not used at all.

This problem can be solved, by using
  - the upper 31 bits of 'state' as 'num_threads' and
  - the lowest bit of 'state' as 'done' bit.

I did this in gnulib:
https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/pthread-once.c;h=069e77e3f3fc2a6e6564f73ba1a3f86ddf9ba531;hb=HEAD#l41
and I have tested it with the test program 'test-pthread-once2' that
I mentioned in my original report.

You can take this code, since I licensed it under LGPLv2+. You can
also surely simplify it, since you are an expert with the Interlocked*
atomic operations (whereas I only wanted to use the GCC built-in
__sync* atomics).

Bruno




-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple
