diff -urp src.old/winsup/cygwin/init.cc src/winsup/cygwin/init.cc --- src.old/winsup/cygwin/init.cc Tue Sep 24 11:25:15 2002 +++ src/winsup/cygwin/init.cc Tue Sep 24 11:19:33 2002 @@ -29,17 +29,8 @@ WINAPI dll_entry (HANDLE h, DWORD reason api_fatal("Sig proc MT init failed\n"); break; case DLL_PROCESS_DETACH: - break; case DLL_THREAD_DETACH: -#if 0 - pthread *thisthread = (pthread *) - TlsGetValue (user_data->threadinterface->thread_self_dwTlsIndex); - if (thisthread) { - /* Some non-pthread call created this thread, - * but we need to clean it up */ - thisthread->exit (0); - } -#endif + pthread_key::runAllDestructors (); break; } return 1; diff -urp src.old/winsup/cygwin/thread.cc src/winsup/cygwin/thread.cc --- src.old/winsup/cygwin/thread.cc Tue Sep 24 11:25:15 2002 +++ src/winsup/cygwin/thread.cc Tue Sep 24 11:24:45 2002 @@ -244,6 +244,15 @@ pthread::self (const bool auto_init) } void +pthread::destructor (void *value) +{ + pthread *thread = (pthread *) value; + /* cleanup thread if thread is detached and not joined */ + if (thread && __pthread_equal(&thread->joiner, &thread)) + delete thread; +} + +void pthread::setTlsSelfPointer (pthread *thisThread) { MT_INTERFACE->thread_self_key.set (thisThread); @@ -352,20 +361,13 @@ pthread::exit (void *value_ptr) // run cleanup handlers pop_all_cleanup_handlers (); - pthread_key::runAllDestructors (); - mutex.Lock (); - // cleanup if thread is in detached state and not joined - if (__pthread_equal (&joiner, &thread ) ) - delete this; - else + if (!__pthread_equal(&joiner, &thread)) { return_ptr = value_ptr; mutex.UnLock (); } - - /* Prevent DLL_THREAD_DETACH Attempting to clean us up */ - setTlsSelfPointer (0); + // else: cleanup is now done in key destructor function if (InterlockedDecrement (&MT_INTERFACE->threadcount) == 0) ::exit (0); diff -urp src.old/winsup/cygwin/thread.h src/winsup/cygwin/thread.h --- src.old/winsup/cygwin/thread.h Tue Sep 24 11:25:15 2002 +++ src/winsup/cygwin/thread.h Tue Sep 24 11:24:49 2002 @@ -368,6 +368,8 @@ public: virtual void pop_cleanup_handler (int const execute); static pthread* self (const bool auto_init = true); + static void destructor (void *value); + static void *thread_init_wrapper (void *); virtual unsigned long getsequence_np(); @@ -519,7 +521,7 @@ public: concurrency (0), threadcount (1), pthread_prepare (NULL), pthread_child (NULL), pthread_parent (NULL), mutexs (NULL), conds (NULL), semaphores (NULL), - mainthread (), reent_key (NULL), thread_self_key (NULL) + mainthread (), reent_key (NULL), thread_self_key (pthread::destructor) { } };