← Back to team overview

kicad-developers team mailing list archive

[PATCH 01/12] Use std::atomic for portable locale init counting

 

---
 common/common.cpp | 10 +++-------
 include/common.h  |  4 +++-
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/common/common.cpp b/common/common.cpp
index 388a803..976e419 100644
--- a/common/common.cpp
+++ b/common/common.cpp
@@ -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." ) );
 }
 
 
diff --git a/include/common.h b/include/common.h
index 97bf56d..70e7379 100644
--- a/include/common.h
+++ b/include/common.h
@@ -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)

Follow ups

References