X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33E963858C2F
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1705894252;
	bh=ew1PhLRSdychQWi1JTAOV3GPzYL2bJeBKcBUcCTnx4w=;
	h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe:
	 List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
	 From;
	b=eS3sflREg8FM88QEZbjTMDAfZaP8Y3BWLUALup5wB42/hHLk6wYplwV/jUTUMnjl+
	 ee359Azijr2Ou5CS1CQsKiz5Mx6ZynPseiLDzSXB5HrWeAe0ftEMQTcQR8HlxBp/1Q
	 aiuw4vbNRptRR9keJfGCBn0bCdSiAq3htekF0J8I=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 84D8E3858C50
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 84D8E3858C50
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705894231; cv=none;
 b=wiY6Cqd/7rzYp2+IP2gej9SVrraN2tsxnkYD6VvDJQFP0DLzD3C4rKXsGysUFbA/C9q8QobHxP4rlFOrUhTeXhldocTn1EorppOpkMrxVhbnuEUlnkMkJaiEg+PHZsgxTa9L2cjHMuDVzd4xMmJ/H88tC2JJFud8MoapO05A7AA=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
 t=1705894231; c=relaxed/simple;
 bh=MEbo0FWSSzicFbtCBPRGFS6JfArfQpZt2cBydI2/u28=;
 h=Date:From:To:Subject:Message-Id:Mime-Version;
 b=pEbiwPEykBzRyiXSGEkdD+kdxmDUiCL0LpnQoV+B6e3L5IGxsPQyYB/TatFm3/7kEApow+p6GASOXGogDqVPnKpMxJ8v+7+EFa/RDkFo4iw7lljkQACuwNRBhIiLiGGqqgWZinGt0dKb67MA+vydTstk5gcNSrVhl/FXTUUm2WA=
ARC-Authentication-Results: i=1; server2.sourceware.org
Date: Mon, 22 Jan 2024 12:30:23 +0900
To: cygwin@cygwin.com
Subject: Re: Possiblly bug of cygwin1.dll
Message-Id: <20240122123023.b8eaac0e50d1e8856f44a115@nifty.ne.jp>
In-Reply-To: <87fryqizl3.fsf@>
References: <20240119224436.876a055f356f7c6796bc725b@nifty.ne.jp>
 <ZaqHGElhXZIc3NFX@calimero.vinschen.de>
 <20240120131825.4157c259fe058155137d6fe0@nifty.ne.jp>
 <20240120141349.cde31e62177a0405b0ee9934@nifty.ne.jp>
 <87v87ov03x.fsf@Gerda.invalid>
 <20240120212427.1e69fd3655ece73ecd508def@nifty.ne.jp>
 <20240121201051.795a4405576a97ab8729e273@nifty.ne.jp>
 <87fryqizl3.fsf@>
X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
Mime-Version: 1.0
X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, GIT_PATCH_0,
 KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS,
 TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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: Takashi Yano via Cygwin <cygwin@cygwin.com>
Reply-To: Takashi Yano <takashi.yano@nifty.ne.jp>
Content-Type: text/plain; charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit
Errors-To: cygwin-bounces+archive-cygwin=delorie.com@cygwin.com
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie.com@cygwin.com>

On Sun, 21 Jan 2024 14:30:00 +0100
ASSI wrote:
> Takashi Yano via Cygwin writes:
> > I found the cause. In pthread.h of cygwin, PTHREAD_ONCE_INIT is defined as:
> > #define PTHREAD_ONCE_INIT { PTHREAD_MUTEX_INITIALIZER, 0 }
> > however, libstdc++ initializes non-static pthread_once_t using this macro.
> 
> https://www.ibm.com/docs/en/aix/7.3?topic=p-pthread-once-init-macro
> https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_once.html
> 
> "The behavior of pthread_once() is undefined if once_control has
> automatic storage duration or is not initialized by PTHREAD_ONCE_INIT."
> 
> > I cannot find the POSIX statement that only static pthread_once_t can be
> > initialized using PTHREAD_ONCE_INIT. If I do not overlook something,
> > this is the problem of cygwin side, isn't it?
> 
> You can initialize just about anything with PTHREAD_ONCE_INIT, but you
> cannot expect the resulting structure to work as intended if there is
> more than instance per library / program, so the libstdc++ object should
> be a singleton, not automatic.
> 
> Still looks like ATWIL to me$B!D(B

Thanks for the information.

Anyway, I confirmed the two patches (one is for gcc, the other is for cygwin)
resolve the issue.

PATCH1: (for gcc)
Do not use PTHREAD_MUTEX_INITIALIZER etc. for non-static pthread_mutex_t.
diff -ur origsrc/gcc-13-20230902/libgcc/gthr-posix.h src/gcc-13-20230902/libgcc/gthr-posix.h
--- origsrc/gcc-13-20230902/libgcc/gthr-posix.h	2023-09-03 07:32:49.000000000 +0900
+++ src/gcc-13-20230902/libgcc/gthr-posix.h	2024-01-22 09:04:54.342189600 +0900
@@ -34,6 +34,12 @@
 
 #include <pthread.h>
 
+#ifdef __CYGWIN__
+#define _GTHREAD_USE_MUTEX_INIT_FUNC 1
+#define _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC 1
+#define _GTHREAD_USE_COND_INIT_FUNC 1
+#endif
+
 #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
      || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK))
 # include <unistd.h>


PATCH2: (for cygwin)
Avoid handle leak caused when non-static pthread_once_t is initialized
with PTHREAD_ONCE_INIT
diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index 7bb4f9fc8..127569160 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -2060,6 +2060,9 @@ pthread::once (pthread_once_t *once_control, void (*init_routine) (void))
     {
       init_routine ();
       once_control->state = 1;
+      pthread_mutex_unlock (&once_control->mutex);
+      while (pthread_mutex_destroy (&once_control->mutex) == EBUSY);
+      return 0;
     }
   /* Here we must remove our cancellation handler */
   pthread_mutex_unlock (&once_control->mutex);

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

-- 
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
