>From 50ccda39b28fe3c8bc4de61e36184ddbf0f3e3cd Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 8 Aug 2020 22:11:50 +0200 Subject: [PATCH 4/4] localename: Use module 'thread-optim'. * lib/localename.c: Include thread-optim.h. (struniq): Use IF_MT macro. * modules/localename (Depends-on): Add thread-optim. --- ChangeLog | 5 +++++ lib/localename.c | 40 ++++++++++++++++++++++------------------ modules/localename | 1 + 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1ee85d6..d9a78c4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2020-08-08 Bruno Haible + localename: Use module 'thread-optim'. + * lib/localename.c: Include thread-optim.h. + (struniq): Use IF_MT macro. + * modules/localename (Depends-on): Add thread-optim. + clean-temp: Use module 'thread-optim'. * lib/clean-temp.c: Include thread-optim.h. (register_temporary_file, unregister_temporary_file, create_temp_dir, diff --git a/lib/localename.c b/lib/localename.c index dc60b07..2e76c11 100644 --- a/lib/localename.c +++ b/lib/localename.c @@ -35,6 +35,7 @@ #include "flexmember.h" #include "setlocale_null.h" +#include "thread-optim.h" /* We cannot support uselocale() on platforms where the locale_t type is fake. See intl-thread-locale.m4 for details. */ @@ -2697,24 +2698,27 @@ struniq (const char *string) /* Out of memory. Return a statically allocated string. */ return "C"; memcpy (new_node->contents, string, size); - /* Lock while inserting new_node. */ - gl_lock_lock (struniq_lock); - /* Check whether another thread already added the string while we were - waiting on the lock. */ - for (p = struniq_hash_table[slot]; p != NULL; p = p->next) - if (strcmp (p->contents, string) == 0) - { - free (new_node); - new_node = p; - goto done; - } - /* Really insert new_node into the hash table. Fill new_node entirely first, - because other threads may be iterating over the linked list. */ - new_node->next = struniq_hash_table[slot]; - struniq_hash_table[slot] = new_node; - done: - /* Unlock after new_node is inserted. */ - gl_lock_unlock (struniq_lock); + { + IF_MT_DECL; + /* Lock while inserting new_node. */ + IF_MT gl_lock_lock (struniq_lock); + /* Check whether another thread already added the string while we were + waiting on the lock. */ + for (p = struniq_hash_table[slot]; p != NULL; p = p->next) + if (strcmp (p->contents, string) == 0) + { + free (new_node); + new_node = p; + goto done; + } + /* Really insert new_node into the hash table. Fill new_node entirely + first, because other threads may be iterating over the linked list. */ + new_node->next = struniq_hash_table[slot]; + struniq_hash_table[slot] = new_node; + done: + /* Unlock after new_node is inserted. */ + IF_MT gl_lock_unlock (struniq_lock); + } return new_node->contents; } diff --git a/modules/localename b/modules/localename index c528acb..28710d0 100644 --- a/modules/localename +++ b/modules/localename @@ -19,6 +19,7 @@ strdup lock langinfo setlocale-null +thread-optim configure.ac: gl_LOCALENAME -- 2.7.4