X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 95BC33858431
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1687477827;
	bh=BFTjjrmVDnrtJIAhWXanJ0PuDzHBEPk0j0HuzY0wXBc=;
	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=XJ/i6wLNO5MNCA+cG7YxeVwxTg3g1Vi56L1F6MxwgZeAZ1+ui6pKq7m37kPWxuFVg
	 MCJWaA7BLvdpXbOrRJ0OGpt5VbOaF+I0DeCh0YK/jNf/yrkax+tqrS6npCUfOg4PMw
	 2aKuNAKh83S4kn9y4bYCn0oXa/F7TbfgN5D3vUDk=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0CF7B3858D37
Date: Fri, 23 Jun 2023 08:49:44 +0900
To: cygwin@cygwin.com
Subject: Re: Memory Barriers at pthread using CYGWIN
Message-Id: <20230623084944.1022e7658a0e8c45c23d5ab7@nifty.ne.jp>
In-Reply-To: <ZJQ1JYyz9Wu+x5XA@calimero.vinschen.de>
References: <CAPxKPA6zdV5UUdwcnhk9Sw159UQfVSsMjoiC4Sx3w-ojzw9Biw@mail.gmail.com>
 <CAPxKPA48sKPqwGc-jU7UQvQhsbT0N6A9LJXCAZeaa1rjeRtuNg@mail.gmail.com>
 <96718c68-af84-45a5-9332-337ec4b2f04a@email.android.com>
 <CAPxKPA6bRfF19pXrYT04TqbxettyYf=ypgfdySysqAJyB9BfxA@mail.gmail.com>
 <64831a27.170a0220.1f805.81bfSMTPIN_ADDED_BROKEN@mx.google.com>
 <1503743851.9556.1686370084334@aps7mgn-06>
 <20230620215300.def2e786b8fdce10b6317381@nifty.ne.jp>
 <20230622191959.a8ff48a0e1f1221c6de52a87@nifty.ne.jp>
 <ZJQ1JYyz9Wu+x5XA@calimero.vinschen.de>
X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
Mime-Version: 1.0
X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, GIT_PATCH_0,
 KAM_DMARC_STATUS, NICE_REPLY_A, 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.29
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="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>

On Thu, 22 Jun 2023 13:48:53 +0200
Corinna Vinschen wrote:
> On Jun 22 19:19, Takashi Yano via Cygwin wrote:
> > Any suggestions?
> > 
> > On Tue, 20 Jun 2023 21:53:00 +0900
> > Takashi Yano wrote:
> > > I looked into this problem, and I think this is a problem regarding
> > > _my_tls initialization order, so far. This seems to happen in LDAP
> > > environment.
> > > 
> > > My assumption is:
> > > 
> > > If the program is the first program which load cygwin1.dll, ldap
> > > connection seems to be made before pthread::init_mainthread().
> > > In cyg_ldap.cc, cyg_ldap::connect(), cyg_ldap::search() or
> > > cyg_ldap::next_page() calls cygwait() in which pthread::self()
> > > is called.
> > > 
> > > Then, _my_tls.tid is initialized with null_pthread, therefore,
> > > _my_tls.tid is not set in pthread::init_mainthread().
> > > 
> > > This causes pthread_join() failure at:
> > > winsup/cygwin/thread.cc: line 2196
> > >    if (!is_good_object (&joiner))
> > >      return EINVAL;
> > > 
> > > 
> > > The first idea to fix this issue is remove set_tls_self_pointer()
> > > call from pthread::self().
> > > 
> > > diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
> > > index 5c1284a93..a0f2d5546 100644
> > > --- a/winsup/cygwin/thread.cc
> > > +++ b/winsup/cygwin/thread.cc
> > > @@ -392,10 +392,7 @@ pthread::self ()
> > >  {
> > >    pthread *thread = _my_tls.tid;
> > >    if (!thread)
> > > -    {
> > > -      thread = pthread_null::get_null_pthread ();
> > > -      thread->set_tls_self_pointer ();
> > > -    }
> > > +    thread = pthread_null::get_null_pthread ();
> > >    return thread;
> > >  }
> > >  
> > > 
> > > The secnd approach is to re-initialize _my_tls.tid in
> > > pthread::init_mainthread() if _my_tls.tid is null_pthread.
> > > 
> > > diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
> > > index 5c1284a93..f614e01c4 100644
> > > --- a/winsup/cygwin/thread.cc
> > > +++ b/winsup/cygwin/thread.cc
> > > @@ -364,7 +364,7 @@ void
> > >  pthread::init_mainthread ()
> > >  {
> > >    pthread *thread = _my_tls.tid;
> > > -  if (!thread)
> > > +  if (!thread || thread == pthread_null::get_null_pthread ())
> > >      {
> > >        thread = new pthread ();
> > >        if (!thread)
> > > 
> > > 
> > > Which is the better approach, do you think?
> > > Or any other idea?
> 
> The second approach looks good to me.  There was a reason to call
> set_tls_self_pointer from pthread::self, I guess.  Resetting in
> init_mainthread should have the least potential for side-effects.

Thanks. I submitted the patch to cygwin-patches@cygwin.com.
Please let me know if I can push it.

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