diff options
author | Daiki Ueno <dueno@redhat.com> | 2018-07-20 11:43:15 +0200 |
---|---|---|
committer | Daiki Ueno <ueno@gnu.org> | 2018-08-10 10:11:24 +0200 |
commit | ebfd7da82d7b9eea81067479861aac2d2c07cc29 (patch) | |
tree | a7327aa6a42a508018e971bcfacc27ef0e7297a5 /common/library.c | |
parent | dc4a6eaddbb36a344cc6a9c7eb12cab9df4899b0 (diff) |
common: Use thread-local storage class when possible
This eliminates the unconditional use of pthread_{get,set}specific()
and pthread_key_{create,delete}(), which glibc doesn't provide the stubs.
Diffstat (limited to 'common/library.c')
-rw-r--r-- | common/library.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/common/library.c b/common/library.c index cde8384..c1275f3 100644 --- a/common/library.c +++ b/common/library.c @@ -104,6 +104,21 @@ uninit_common (void) #ifdef OS_UNIX +#ifdef P11_TLS_KEYWORD +static p11_local * +_p11_library_get_thread_local (void) +{ + static P11_TLS_KEYWORD p11_local local; + static P11_TLS_KEYWORD bool local_initialized = false; + + if (!local_initialized) { + memset (&local, 0, sizeof (p11_local)); + local_initialized = true; + } + + return &local; +} +#else static pthread_key_t thread_local = 0; static p11_local * @@ -121,6 +136,7 @@ _p11_library_get_thread_local (void) return local; } +#endif static void count_forks (void) @@ -136,7 +152,9 @@ p11_library_init_impl (void) p11_debug ("initializing library"); P11_RECURSIVE_MUTEX_INIT (p11_library_mutex); P11_RECURSIVE_MUTEX_INIT (p11_virtual_mutex); +#ifndef P11_TLS_KEYWORD pthread_key_create (&thread_local, free); +#endif p11_message_storage = thread_local_message; #ifdef HAVE_STRERROR_L p11_message_locale = newlocale (LC_ALL_MASK, "POSIX", (locale_t) 0); @@ -156,15 +174,19 @@ p11_library_uninit (void) { uninit_common (); +#ifndef P11_TLS_KEYWORD /* Some cleanup to pacify valgrind */ free (pthread_getspecific (thread_local)); pthread_setspecific (thread_local, NULL); +#endif #ifdef HAVE_STRERROR_L freelocale (p11_message_locale); #endif p11_message_storage = dont_store_message; +#ifndef P11_TLS_KEYWORD pthread_key_delete (thread_local); +#endif p11_mutex_uninit (&p11_virtual_mutex); p11_mutex_uninit (&p11_library_mutex); } |