]> git.michaelhowe.org Git - packages/o/openafs.git/commitdiff
windows-pthread-cleanup-20060424
authorJeffrey Altman <jaltman@secure-endpoints.com>
Mon, 24 Apr 2006 15:25:12 +0000 (15:25 +0000)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Mon, 24 Apr 2006 15:25:12 +0000 (15:25 +0000)
A number of crash reports have been filed with Microsoft in afspthread.dll.
The crashes are occuring as the processes are terminating.  In order to
attempt to address this problem this patch adds support for a DllMain()
entry point that is used to cleanup Thread Local Storage and the various
RX queues.  One theory is that processes are loading and unloading DLLs
that are linked to afspthread.dll leaving the pthread data in a very
inconsistent state after each unload.  By cleaning up the static data
during the unload, if afspthread is loading again it will be forced to
initialize the data once again.

src/WINNT/pthread/pthread.c
src/WINNT/pthread/pthread.def

index bf7fe7275a81f4f15558e47301a95836cddb39ae..37e088a13f35ed9b9db8723b1eeeeb7ed9d827ef 100644 (file)
@@ -260,6 +260,24 @@ static void create_once(void) {
     pthread_cache_done = 1;
 }
 
+static void cleanup_pthread_cache(void) {
+    thread_p cur = NULL, next = NULL;
+
+    if (pthread_cache_done) {
+       for(queue_Scan(&active_Q, cur, next, thread)) {
+           queue_Remove(cur);
+       }
+       for(queue_Scan(&cache_Q, cur, next, thread)) {
+           queue_Remove(cur);
+       }
+
+       pthread_mutex_destroy(&active_Q_mutex);
+       pthread_mutex_destroy(&cache_Q_mutex);
+
+       pthread_cache_done = 0;
+    }
+}      
+
 static void put_thread(thread_p old) {
  
     CloseHandle(old->t_handle);
@@ -360,6 +378,23 @@ static void tsd_free_all(char *tsd[PTHREAD_KEYS_MAX]) {
     } while(call_more_destructors);
 }
 
+static void cleanup_global_tsd(void)
+{
+    thread_p cur = NULL, next = NULL;
+
+    if (tsd_done) {
+       for(queue_Scan(&active_Q, cur, next, thread)) {
+           tsd_free_all(cur->tsd);
+       }
+
+       TlsFree(tsd_pthread_index);
+       tsd_pthread_index = 0xFFFFFFFF;
+       TlsFree(tsd_index);
+       tsd_index = 0xFFFFFFFF;
+       tsd_done = 0;
+    }
+}
+
 static DWORD WINAPI afs_pthread_create_stub(LPVOID param) {
     pthread_create_t *t = (pthread_create_t *) param;
     void *rc;
@@ -700,10 +735,27 @@ static pthread_once_t waiter_cache_once = PTHREAD_ONCE_INIT;
  
 static void init_waiter_cache(void) {
     InitializeCriticalSection(&waiter_cache_cs);
-    waiter_cache_init = 1;
     queue_Init(&waiter_cache);
+    waiter_cache_init = 1;
 }
  
+static void cleanup_waiter_cache(void)
+{
+    cond_waiters_t * cur = NULL, * next = NULL;
+
+    if (waiter_cache_init) {
+       for(queue_Scan(&waiter_cache, cur, next, cond_waiter)) {
+           queue_Remove(cur);
+
+           CloseHandle(cur->event);
+           free(cur);
+       }
+
+       DeleteCriticalSection(&waiter_cache_cs);
+       waiter_cache_init = 0;
+    }
+}
+
 static cond_waiters_t *get_waiter() {
     cond_waiters_t *new = NULL;
  
@@ -1257,3 +1309,37 @@ void pthread_exit(void *status) {
     RaiseException(PTHREAD_EXIT_EXCEPTION, 0, 0, NULL);
 
 }
+
+/*
+ * DllMain() -- Entry-point function called by the DllMainCRTStartup()
+ *     function in the MSVC runtime DLL (msvcrt.dll).
+ *
+ *     Note: the system serializes calls to this function.
+ */
+BOOL WINAPI
+DllMain(HINSTANCE dllInstHandle,/* instance handle for this DLL module */
+        DWORD reason,           /* reason function is being called */
+        LPVOID reserved)
+{                               /* reserved for future use */
+    switch (reason) {
+    case DLL_PROCESS_ATTACH:
+        /* library is being attached to a process */
+        /* disable thread attach/detach notifications */
+        (void)DisableThreadLibraryCalls(dllInstHandle);
+
+       pthread_once(&pthread_cache_once, create_once);
+       pthread_once(&global_tsd_once, tsd_once);
+       pthread_once(&waiter_cache_once, init_waiter_cache);
+       return TRUE;
+
+    case DLL_PROCESS_DETACH:
+       cleanup_waiter_cache();
+       cleanup_global_tsd();
+       cleanup_pthread_cache();
+        return TRUE;
+
+    default:
+        return FALSE;
+    }
+}
+
index 1dd1128810a7b0b3b5c9d0087900b07324ae76f1..45e33d7a288a58d5677f154709360ae0323058ce 100644 (file)
@@ -25,3 +25,4 @@ EXPORTS
        pthread_attr_getdetachstate                     @23
        pthread_attr_setdetachstate                     @24
        pthread_exit                                    @25
+       DllMain                                         @26