Use std::atomic for portable locale init counting

This commit is contained in:
Simon Richter 2016-05-28 12:46:22 -04:00 committed by Chris Pavlina
parent e795dafc4b
commit f7fdf43ea8
2 changed files with 6 additions and 8 deletions

View File

@ -64,14 +64,12 @@ EDA_COLOR_T g_GhostColor;
* is thrown, or not.
*/
int LOCALE_IO::m_c_count = 0;
std::atomic<unsigned int> LOCALE_IO::m_c_count(0);
LOCALE_IO::LOCALE_IO()
{
wxASSERT_MSG( m_c_count >= 0, wxT( "LOCALE_IO::m_c_count mismanaged." ) );
// use thread safe, atomic operation
if( __sync_fetch_and_add( &m_c_count, 1 ) == 0 )
if( m_c_count++ == 0 )
{
// Store the user locale name, to restore this locale later, in dtor
m_user_locale = setlocale( LC_ALL, 0 );
@ -83,13 +81,11 @@ LOCALE_IO::LOCALE_IO()
LOCALE_IO::~LOCALE_IO()
{
// use thread safe, atomic operation
if( __sync_sub_and_fetch( &m_c_count, 1 ) == 0 )
if( --m_c_count == 0 )
{
// revert to the user locale
setlocale( LC_ALL, m_user_locale.c_str() );
}
wxASSERT_MSG( m_c_count >= 0, wxT( "LOCALE_IO::m_c_count mismanaged." ) );
}

View File

@ -42,6 +42,8 @@
#include <richio.h>
#include <colors.h>
#include <atomic>
class wxAboutDialogInfo;
class SEARCH_STACK;
@ -199,7 +201,7 @@ private:
void setUserLocale( const char* aUserLocale );
// allow for nesting of LOCALE_IO instantiations
static int m_c_count;
static std::atomic<unsigned int> m_c_count;
// The locale in use before switching to the "C" locale
// (the locale can be set by user, and is not always the system locale)