about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2020-12-09 16:58:32 -0500
committerRich Felker <dalias@aerifal.cx>2020-12-09 16:58:32 -0500
commit37fcc13c12ade19c37a1a8ac80be4a14e21cff1e (patch)
treebf9b3460c0ca84c4abdaab1f276e6f6e6d676c1c
parentc53e9b239418eb3e0e8be256abd0f6ad7608bbcf (diff)
downloadmusl-37fcc13c12ade19c37a1a8ac80be4a14e21cff1e.tar.gz
musl-37fcc13c12ade19c37a1a8ac80be4a14e21cff1e.tar.xz
musl-37fcc13c12ade19c37a1a8ac80be4a14e21cff1e.zip
lift locale lock out of internal __get_locale
this allows the lock to be shared with setlocale, eliminates repeated
per-category lock/unlock in newlocale, and will allow the use of
pthread_once in newlocale to be dropped (to be done separately).
-rw-r--r--src/internal/locale_impl.h2
-rw-r--r--src/locale/locale_map.c13
-rw-r--r--src/locale/newlocale.c11
-rw-r--r--src/locale/setlocale.c11
4 files changed, 19 insertions, 18 deletions
diff --git a/src/internal/locale_impl.h b/src/internal/locale_impl.h
index 741a71c4..4431a92e 100644
--- a/src/internal/locale_impl.h
+++ b/src/internal/locale_impl.h
@@ -15,6 +15,8 @@ struct __locale_map {
 	const struct __locale_map *next;
 };
 
+extern hidden volatile int __locale_lock[1];
+
 extern hidden const struct __locale_map __c_dot_utf8;
 extern hidden const struct __locale_struct __c_locale;
 extern hidden const struct __locale_struct __c_dot_utf8_locale;
diff --git a/src/locale/locale_map.c b/src/locale/locale_map.c
index fa51f2e3..da61f7fc 100644
--- a/src/locale/locale_map.c
+++ b/src/locale/locale_map.c
@@ -28,8 +28,8 @@ static const char envvars[][12] = {
 	"LC_MESSAGES",
 };
 
-static volatile int lock[1];
-volatile int *const __locale_lockptr = lock;
+volatile int __locale_lock[1];
+volatile int *const __locale_lockptr = __locale_lock;
 
 const struct __locale_map *__get_locale(int cat, const char *val)
 {
@@ -63,14 +63,6 @@ const struct __locale_map *__get_locale(int cat, const char *val)
 	for (p=loc_head; p; p=p->next)
 		if (!strcmp(val, p->name)) return p;
 
-	LOCK(lock);
-
-	for (p=loc_head; p; p=p->next)
-		if (!strcmp(val, p->name)) {
-			UNLOCK(lock);
-			return p;
-		}
-
 	if (!libc.secure) path = getenv("MUSL_LOCPATH");
 	/* FIXME: add a default path? */
 
@@ -117,6 +109,5 @@ const struct __locale_map *__get_locale(int cat, const char *val)
 	 * requested name was "C" or "POSIX". */
 	if (!new && cat == LC_CTYPE) new = (void *)&__c_dot_utf8;
 
-	UNLOCK(lock);
 	return new;
 }
diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c
index d20a8489..8eee2e10 100644
--- a/src/locale/newlocale.c
+++ b/src/locale/newlocale.c
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <pthread.h>
 #include "locale_impl.h"
+#include "lock.h"
 
 static pthread_once_t default_locale_once;
 static struct __locale_struct default_locale, default_ctype_locale;
@@ -19,7 +20,7 @@ int __loc_is_allocated(locale_t loc)
 		&& loc != &default_locale && loc != &default_ctype_locale;
 }
 
-locale_t __newlocale(int mask, const char *name, locale_t loc)
+static locale_t do_newlocale(int mask, const char *name, locale_t loc)
 {
 	struct __locale_struct tmp;
 
@@ -55,4 +56,12 @@ locale_t __newlocale(int mask, const char *name, locale_t loc)
 	return loc;
 }
 
+locale_t __newlocale(int mask, const char *name, locale_t loc)
+{
+	LOCK(__locale_lock);
+	loc = do_newlocale(mask, name, loc);
+	UNLOCK(__locale_lock);
+	return loc;
+}
+
 weak_alias(__newlocale, newlocale);
diff --git a/src/locale/setlocale.c b/src/locale/setlocale.c
index 2bc7b500..360c4437 100644
--- a/src/locale/setlocale.c
+++ b/src/locale/setlocale.c
@@ -9,12 +9,11 @@ static char buf[LC_ALL*(LOCALE_NAME_MAX+1)];
 
 char *setlocale(int cat, const char *name)
 {
-	static volatile int lock[1];
 	const struct __locale_map *lm;
 
 	if ((unsigned)cat > LC_ALL) return 0;
 
-	LOCK(lock);
+	LOCK(__locale_lock);
 
 	/* For LC_ALL, setlocale is required to return a string which
 	 * encodes the current setting for all categories. The format of
@@ -36,7 +35,7 @@ char *setlocale(int cat, const char *name)
 				}
 				lm = __get_locale(i, part);
 				if (lm == LOC_MAP_FAILED) {
-					UNLOCK(lock);
+					UNLOCK(__locale_lock);
 					return 0;
 				}
 				tmp_locale.cat[i] = lm;
@@ -57,14 +56,14 @@ char *setlocale(int cat, const char *name)
 			s += l+1;
 		}
 		*--s = 0;
-		UNLOCK(lock);
+		UNLOCK(__locale_lock);
 		return same==LC_ALL ? (char *)part : buf;
 	}
 
 	if (name) {
 		lm = __get_locale(cat, name);
 		if (lm == LOC_MAP_FAILED) {
-			UNLOCK(lock);
+			UNLOCK(__locale_lock);
 			return 0;
 		}
 		libc.global_locale.cat[cat] = lm;
@@ -73,7 +72,7 @@ char *setlocale(int cat, const char *name)
 	}
 	char *ret = lm ? (char *)lm->name : "C";
 
-	UNLOCK(lock);
+	UNLOCK(__locale_lock);
 
 	return ret;
 }