Remove setlocale thread safety.

This interface isn't standardized to be thread safe so nobody can rely on
it. Additionally this only thread secures setlocale, but not all the code
that might depend on the current locale, so it is pointless.
This commit is contained in:
Jonas 'Sortie' Termansen 2015-08-01 14:36:13 +02:00
parent 9d6bd9b830
commit 7185cb33c7
2 changed files with 7 additions and 22 deletions

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
Copyright(C) Jonas 'Sortie' Termansen 2012, 2015.
This file is part of the Sortix C Library.
@ -70,12 +70,7 @@ struct lconv
#define LC_ALL 6
#define LC_NUM_CATEGORIES LC_ALL
const char* sortix_setlocale(int category, const char* locale);
#if __USE_SORTIX && __SORTIX_STDLIB_REDIRECTS
const char* setlocale(int category, const char* locale) __asm__ ("sortix_setlocale");
#else
char* setlocale(int category, const char* locale);
#endif
struct lconv* localeconv(void);
#ifdef __cplusplus

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2014.
Copyright(C) Jonas 'Sortie' Termansen 2012, 2014, 2015.
This file is part of the Sortix C Library.
@ -22,45 +22,35 @@
*******************************************************************************/
#define __SORTIX_STDLIB_REDIRECTS 0
#include <errno.h>
#include <locale.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
static pthread_mutex_t locale_lock = PTHREAD_MUTEX_INITIALIZER;
static char* current_locales[LC_NUM_CATEGORIES] = { NULL };
extern "C" const char* sortix_setlocale(int category, const char* locale)
extern "C" char* setlocale(int category, const char* locale)
{
if ( category < 0 || LC_ALL < category )
return errno = EINVAL, (const char*) NULL;
return errno = EINVAL, (char*) NULL;
char* new_strings[LC_NUM_CATEGORIES];
int from = category != LC_ALL ? category : 0;
int to = category != LC_ALL ? category : LC_NUM_CATEGORIES - 1;
if ( !locale )
return current_locales[to] ? current_locales[to] : "C";
return current_locales[to] ? current_locales[to] : (char*) "C";
for ( int i = from; i <= to; i++ )
{
if ( !(new_strings[i] = strdup(locale)) )
{
for ( int n = from; n < i; n++ )
free(new_strings[n]);
return NULL;
return (char*) NULL;
}
}
pthread_mutex_lock(&locale_lock);
for ( int i = from; i <= to; i++ )
{
free(current_locales[i]);
current_locales[i] = new_strings[i];
}
pthread_mutex_unlock(&locale_lock);
return locale;
}
extern "C" char* setlocale(int category, const char* locale)
{
return (char*) sortix_setlocale(category, locale);
return (char*) locale;
}