From: Jeffrey Altman Date: Tue, 2 May 2006 04:51:55 +0000 (+0000) Subject: STABLE14-windows-pthread-cleanup-20060424 X-Git-Tag: openafs-stable-1_4_1b~14 X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=ae7a614e26a15b4b6c8ba0be3afcac76df9476f1;p=packages%2Fo%2Fopenafs.git STABLE14-windows-pthread-cleanup-20060424 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. (cherry picked from commit edff1e8788a2cadfd6bb9e08f9cfdbfb491924a6) --- diff --git a/src/WINNT/pthread/pthread.c b/src/WINNT/pthread/pthread.c index 8531e61e0..5ad35e4c0 100644 --- a/src/WINNT/pthread/pthread.c +++ b/src/WINNT/pthread/pthread.c @@ -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; + } +} + diff --git a/src/WINNT/pthread/pthread.def b/src/WINNT/pthread/pthread.def index 1dd112881..45e33d7a2 100644 --- a/src/WINNT/pthread/pthread.def +++ b/src/WINNT/pthread/pthread.def @@ -25,3 +25,4 @@ EXPORTS pthread_attr_getdetachstate @23 pthread_attr_setdetachstate @24 pthread_exit @25 + DllMain @26