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).
This commit is contained in:
Rich Felker 2020-12-09 16:58:32 -05:00
parent c53e9b2394
commit 37fcc13c12
4 changed files with 19 additions and 18 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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);

View file

@ -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;
}