Memory Barriers at pthread using CYGWIN

Takashi Yano takashi.yano@nifty.ne.jp
Tue Jun 20 12:53:00 GMT 2023


On Sat, 10 Jun 2023 13:08:04 +0900 (JST)
Takashi Yano wrote:
> "M?min A." wrote:
> > //windows cmd line
> > C:\cygwin64\home\maydin\test>cygcheck ./main.exe
> > C:\cygwin64\home\maydin\test\main.exe
> >   C:\cygwin64\bin\cygwin1.dll
> >     C:\WINDOWS\system32\KERNEL32.dll
> >       C:\WINDOWS\system32\ntdll.dll
> >       C:\WINDOWS\system32\KERNELBASE.dll
> > 
> > C:\cygwin64\home\maydin\test>ldd ./main.exe
> >         ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffa92350000)
> >         KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL
> > (0x7ffa90570000)
> >         KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll
> > (0x7ffa8ffb0000)
> >         cygwin1.dll => /usr/bin/cygwin1.dll (0x7ffa27960000)
> 
> Looks OK.
> 
> > //When cygwin terminal closed and cmd line command. Join throw fails.
> > C:\cygwin64\home\maydin\test>main.exe
> > Failed to join the thread t1.
> > Failed to join the thread t2.
> > r1 = 1, r2 = 1
> > 
> > //when cygwin terminal opened. The test is passed.
> > C:\cygwin64\home\maydin\test>main.exe
> > r1 = 1, r2 = 1
> > 
> > 
> > I think Cygwin terminal should be always open in order to execute a file in
> > windows. Am I right ?
> 
> It should not be. Weird enough.
> 
> Could you please provide a strace log file such as:
> strace -o faild.log ./main.exe
> ?

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?

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


More information about the Cygwin mailing list