From 0f4a0409cb0fa10631b9fe2241a2c8083f37daf5 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 22 Jan 2009 12:07:16 +0100 Subject: [PATCH] Cache the C locale object. --- ChangeLog | 9 +++++++++ lib/c-strtod.c | 32 ++++++++++++++++++++++---------- m4/c-strtod.m4 | 6 +++++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index a6e5634d3c..e5d72388cf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2009-01-22 Bruno Haible + + Cache the C locale object. + * lib/c-strtod.c (c_locale_cache): New variable. + (c_locale): New function. + (C_STRTOD): Use it, and don't call freelocale. + * m4/c-strtod.m4 (gl_C_STRTOD, gl_C_STRTOLD): Require AC_C_INLINE. + Suggested by Paolo Bonzini. + 2009-01-21 Bruno Haible * lib/getloadavg.c (getloadavg): Check c_strtod result against error diff --git a/lib/c-strtod.c b/lib/c-strtod.c index 0f97528a86..3d0fae9956 100644 --- a/lib/c-strtod.c +++ b/lib/c-strtod.c @@ -44,6 +44,25 @@ # define STRTOD strtod #endif +#ifdef LC_ALL_MASK + +/* Cache for the C locale object. + Marked volatile so that different threads see the same value + (avoids locking). */ +static volatile locale_t c_locale_cache; + +/* Return the C locale object, or (locale_t) 0 with errno set + if it cannot be created. */ +static inline locale_t +c_locale (void) +{ + if (!c_locale_cache) + c_locale_cache = newlocale (LC_ALL_MASK, "C", (locale_t) 0); + return c_locale_cache; +} + +#endif + DOUBLE C_STRTOD (char const *nptr, char **endptr) { @@ -51,18 +70,11 @@ C_STRTOD (char const *nptr, char **endptr) #ifdef LC_ALL_MASK - locale_t c_locale; - int saved_errno; - - c_locale = newlocale (LC_ALL_MASK, "C", (locale_t) 0); - if (!c_locale) + locale_t locale = c_locale (); + if (!locale) return 0; /* errno is set here */ - r = STRTOD_L (nptr, endptr, c_locale); - - saved_errno = errno; - freelocale (c_locale); - errno = saved_errno; + r = STRTOD_L (nptr, endptr, locale); #else diff --git a/m4/c-strtod.m4 b/m4/c-strtod.m4 index 0514f2d497..ba954354f8 100644 --- a/m4/c-strtod.m4 +++ b/m4/c-strtod.m4 @@ -1,4 +1,4 @@ -# c-strtod.m4 serial 10 +# c-strtod.m4 serial 11 # Copyright (C) 2004, 2005, 2006, 2009 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation @@ -39,6 +39,8 @@ AC_DEFUN([gl_C_STRTOD], dnl Prerequisites of lib/c-strtod.c. AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) + + AC_REQUIRE([AC_C_INLINE]) : ]) @@ -49,5 +51,7 @@ AC_DEFUN([gl_C_STRTOLD], dnl Prerequisites of lib/c-strtold.c. AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) AC_REQUIRE([gl_C99_STRTOLD]) + + AC_REQUIRE([AC_C_INLINE]) : ]) -- 2.30.2