Dramatically improve windows session-save-time by avoiding recursive calls to set_locale.

This commit is contained in:
Ben Loftis 2015-01-18 09:43:05 -06:00
parent 219a09496f
commit d3227ac0d0
2 changed files with 22 additions and 13 deletions

View File

@ -25,24 +25,29 @@
using namespace PBD;
LocaleGuard::LocaleGuard (const char* str)
{
old = setlocale (LC_NUMERIC, NULL);
// try to avoid calling setlocale() recursively. this is not thread-safe.
std::string PBD::LocaleGuard::current;
if (old) {
old = strdup (old);
if (strcmp (old, str)) {
setlocale (LC_NUMERIC, str);
}
}
LocaleGuard::LocaleGuard (const char* str)
: old(0)
{
if (current != str) {
old = strdup (setlocale (LC_NUMERIC, NULL));
if (strcmp (old, str)) {
if (setlocale (LC_NUMERIC, str))
current = str;
}
}
}
LocaleGuard::~LocaleGuard ()
{
setlocale (LC_NUMERIC, old);
if (old) {
if (setlocale (LC_NUMERIC, old))
current = old;
if (old) {
free (const_cast<char*>(old));
}
free ((char*)old);
}
}

View File

@ -22,12 +22,16 @@
#include "pbd/libpbd_visibility.h"
#include <string>
namespace PBD {
struct LIBPBD_API LocaleGuard {
LocaleGuard (const char*);
~LocaleGuard ();
const char* old;
static std::string current;
};
}