From 3361620d83aa159acda9f6f727939f9e42a818b7 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 8 Jan 2014 23:10:01 +0100 Subject: [PATCH] Thread-secure setlocale(3). --- libc/locale/setlocale.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libc/locale/setlocale.cpp b/libc/locale/setlocale.cpp index fd63c633..4d6b99b9 100644 --- a/libc/locale/setlocale.cpp +++ b/libc/locale/setlocale.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2014. This file is part of the Sortix C Library. @@ -25,9 +25,11 @@ #define __SORTIX_STDLIB_REDIRECTS 0 #include #include +#include #include #include +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) @@ -48,11 +50,13 @@ extern "C" const char* sortix_setlocale(int category, const char* locale) return 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; }