From 95ec58961a480c15fe780bbce6d6cd974f478407 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Wed, 6 Feb 2013 22:16:42 +0100 Subject: Only do shared object and DLL initialization in libraries Don't do library initialization on shared object load when not running in a library. We'll want to plug into this and do different things per library in the future. --- common/library.c | 60 +++++++++++--------------------------------------------- common/library.h | 2 ++ 2 files changed, 13 insertions(+), 49 deletions(-) (limited to 'common') diff --git a/common/library.c b/common/library.c index de340bb..0bc7e0c 100644 --- a/common/library.c +++ b/common/library.c @@ -176,18 +176,12 @@ p11_library_init_impl (void) pthread_key_create (&thread_local, free); } -#ifdef __GNUC__ -__attribute__((constructor)) -#endif void p11_library_init (void) { p11_library_init_once (); } -#ifdef __GNUC__ -__attribute__((destructor)) -#endif void p11_library_uninit (void) { @@ -233,17 +227,21 @@ p11_library_init (void) p11_debug ("initializing library"); p11_mutex_init (&p11_library_mutex); thread_local = TlsAlloc (); + if (thread_local == TLS_OUT_OF_INDEXES) + p11_debug ("couldn't setup tls"); } -static void -free_tls_value (LPVOID data) +void +p11_library_thread_cleanup (void) { p11_local *local = data; - if (local == NULL) - return; - if (local->last_error) - LocalFree (local->last_error); - LocalFree (data); + if (thread_local != TLS_OUT_OF_INDEXES) { + p11_debug ("thread stopped, freeing tls"); + local = TlsGetValue (thread_local); + if (local->last_error) + LocalFree (local->last_error); + LocalFree (local); + } } void @@ -261,40 +259,4 @@ p11_library_uninit (void) _p11_mutex_uninit (&p11_library_mutex); } - -BOOL WINAPI -DllMain (HINSTANCE instance, - DWORD reason, - LPVOID reserved) -{ - LPVOID data; - - switch (reason) { - case DLL_PROCESS_ATTACH: - p11_library_init (); - if (thread_local == TLS_OUT_OF_INDEXES) { - p11_debug ("couldn't setup tls"); - return FALSE; - } - break; - - case DLL_THREAD_DETACH: - if (thread_local != TLS_OUT_OF_INDEXES) { - p11_debug ("thread stopped, freeing tls"); - data = TlsGetValue (thread_local); - free_tls_value (data); - } - break; - - case DLL_PROCESS_DETACH: - p11_library_uninit (); - break; - - default: - break; - } - - return TRUE; -} - #endif /* OS_WIN32 */ diff --git a/common/library.h b/common/library.h index 338dc0f..b310cb9 100644 --- a/common/library.h +++ b/common/library.h @@ -79,6 +79,8 @@ void p11_library_init_impl (void); void p11_library_init (void); +void p11_library_thread_cleanup (void); + void p11_library_uninit (void); #endif /* P11_LIBRARY_H_ */ -- cgit v1.1