This is part 1 of 3 patches for the MTinterface.

It provides a more general implementation of Robs initMainThread that
could be used also to assign thread handles for threads that are created
outside pthread (via CreateThread).

2002-09-24  Thomas Pfaff  <[EMAIL PROTECTED]>

        * thread.cc (MTinterface::init): Change initMainThread arguments.
        (pthread::initMainThread): Converted to method.
        (pthread::self): Initialize win32_obj_id via initThread for
        unknown thread. Mark thread as detached to force cleanup on thread
        terminiation.
        (pthread::thread_init_wrapper): Set thread_id.
        (pthread::initThread): Implement.
        * thread.h (pthread::initMainThread): Convert to method.
        (pthread::initThread): New method.
diff -urp src.old/winsup/cygwin/thread.cc src/winsup/cygwin/thread.cc
--- src.old/winsup/cygwin/thread.cc     Tue Sep 24 10:55:50 2002
+++ src/winsup/cygwin/thread.cc Tue Sep 24 10:55:21 2002
@@ -189,7 +189,7 @@ MTinterface::Init (int forked)
   concurrency = 0;
   threadcount = 1; /*1 current thread when Init occurs.*/
 
-  pthread::initMainThread (&mainthread, myself->hProcess);
+  mainthread.initMainThread ();
 
   if (forked)
     return;
@@ -237,14 +237,6 @@ MTinterface::fixup_after_fork (void)
 /* pthread calls */
 
 /* static methods */
-void
-pthread::initMainThread (pthread *mainThread, HANDLE win32_obj_id)
-{
-  mainThread->win32_obj_id = win32_obj_id;
-  mainThread->setThreadIdtoCurrent ();
-  setTlsSelfPointer (mainThread);
-}
-
 pthread *
 pthread::self ()
 {
@@ -257,7 +249,11 @@ pthread::self ()
       delete temp;
       return pthreadNull::getNullpthread ();
   }
+  temp->initThread ();
+  temp->attr.joinable = PTHREAD_CREATE_DETACHED;
+  temp->joiner = temp;
   temp->postcreate ();
+
   return temp;
 }
 
@@ -1414,12 +1410,13 @@ pthread::thread_init_wrapper (void *_arg
   if (!TlsSetValue (MT_INTERFACE->reent_index, &local_reent))
     system_printf ("local storage for thread couldn't be set");
 
+  thread->setThreadIdtoCurrent ();
   setTlsSelfPointer (thread);
 
   thread->mutex.Lock ();
   // if thread is detached force cleanup on exit
   if (thread->attr.joinable == PTHREAD_CREATE_DETACHED && thread->joiner == NULL)
-    thread->joiner = pthread::self ();
+    thread->joiner = thread;
   thread->mutex.UnLock ();
 
 #ifdef _CYG_THREAD_FAILSAFE
@@ -1455,6 +1452,32 @@ unsigned long
 pthread::getsequence_np ()
 {
   return getThreadId ();
+}
+
+void
+pthread::initMainThread ()
+{
+  precreate (NULL);
+  initThread ();
+}
+
+void
+pthread::initThread ()
+{
+  if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
+                        GetCurrentProcess (), &win32_obj_id,
+                        0, FALSE, DUPLICATE_SAME_ACCESS))
+
+    {
+      system_printf ("Failed to duplicate handle for thread");
+      win32_obj_id = NULL;
+    }
+
+  attr.joinable = PTHREAD_CREATE_JOINABLE;
+  joiner = NULL;
+
+  setThreadIdtoCurrent ();
+  setTlsSelfPointer (this);
 }
 
 int
diff -urp src.old/winsup/cygwin/thread.h src/winsup/cygwin/thread.h
--- src.old/winsup/cygwin/thread.h      Tue Sep 24 10:55:50 2002
+++ src/winsup/cygwin/thread.h  Tue Sep 24 10:55:21 2002
@@ -338,7 +338,6 @@ public:
    pthread ();
    virtual ~pthread ();
 
-   static void initMainThread(pthread *, HANDLE);
    static bool isGoodObject(pthread_t const *);
    static void atforkprepare();
    static void atforkparent();
@@ -372,6 +371,9 @@ public:
    static void *thread_init_wrapper (void *);
 
    virtual unsigned long getsequence_np();
+
+   void initMainThread ();
+   void initThread ();
 
 private:
     DWORD thread_id;

Reply via email to