Make Lazy initialization of TextInfo::m_IsAsciiCasingSameAsInvariant thread safe...
authorJose Manuel Heredia Hidalgo <joheredi@microsoft.com>
Thu, 1 Sep 2016 21:24:22 +0000 (14:24 -0700)
committerJan Kotas <jkotas@microsoft.com>
Thu, 1 Sep 2016 21:24:22 +0000 (14:24 -0700)
Replace bool? with Tristate enum to make it thread safe. Also use Tristate instead of TurkishCasing enum

Commit migrated from https://github.com/dotnet/coreclr/commit/3b9140c30e647f7ff5b42b01031de43268258639

src/coreclr/src/mscorlib/corefx/System/Globalization/TextInfo.Unix.cs
src/coreclr/src/mscorlib/corefx/System/Globalization/TextInfo.cs
src/coreclr/src/mscorlib/src/System/Globalization/TextInfo.cs

index bededed..8490057 100644 (file)
@@ -10,15 +10,8 @@ namespace System.Globalization
 {
     public partial class TextInfo
     {
-        enum TurkishCasing
-        {
-            NotInitialized,
-            NotNeeded,
-            Needed
-        }
-
         [NonSerialized]
-        private TurkishCasing _needsTurkishCasing = TurkishCasing.NotInitialized;
+        private Tristate _needsTurkishCasing = Tristate.NotInitialized;
 
         //////////////////////////////////////////////////////////////////////////
         ////
@@ -115,11 +108,11 @@ namespace System.Globalization
             }
             else
             {
-                if (_needsTurkishCasing == TurkishCasing.NotInitialized)
+                if (_needsTurkishCasing == Tristate.NotInitialized)
                 {
-                    _needsTurkishCasing = NeedsTurkishCasing(_textInfoName) ? TurkishCasing.Needed : TurkishCasing.NotNeeded;
+                    _needsTurkishCasing = NeedsTurkishCasing(_textInfoName) ? Tristate.True : Tristate.False;
                 }
-                if ( _needsTurkishCasing == TurkishCasing.Needed)
+                if (_needsTurkishCasing == Tristate.True)
                 {
                     Interop.GlobalizationInterop.ChangeCaseTurkish(src, srcLen, dstBuffer, dstBufferCapacity, bToUpper);
                 }
index 40fff9f..01525cd 100644 (file)
@@ -31,6 +31,13 @@ namespace System.Globalization
         ////                        Internal Information                        //
         ////--------------------------------------------------------------------//
 
+        private enum Tristate : byte
+        {
+            NotInitialized,
+            True,
+            False,
+        }
+
         ////
         ////  Variables.
         ////
@@ -57,7 +64,7 @@ namespace System.Globalization
         [NonSerialized]
         private String _textInfoName;     // Name of the text info we're using (ie: _cultureData.STEXTINFO)
         [NonSerialized]
-        private bool? _isAsciiCasingSameAsInvariant;
+        private Tristate _isAsciiCasingSameAsInvariant = Tristate.NotInitialized;
 
         // Invariant text info
         internal static TextInfo Invariant
@@ -355,13 +362,13 @@ namespace System.Globalization
         {
             get
             {
-                if (_isAsciiCasingSameAsInvariant == null)
+                if (_isAsciiCasingSameAsInvariant == Tristate.NotInitialized)
                 {
                     _isAsciiCasingSameAsInvariant = CultureInfo.GetCultureInfo(_textInfoName).CompareInfo.Compare("abcdefghijklmnopqrstuvwxyz",
                                                                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
-                                                                             CompareOptions.IgnoreCase) == 0;
+                                                                             CompareOptions.IgnoreCase) == 0 ? Tristate.True : Tristate.False;
                 }
-                return (bool)_isAsciiCasingSameAsInvariant;
+                return _isAsciiCasingSameAsInvariant == Tristate.True;
             }
         }
 
index 97e50f0..9b84486 100644 (file)
@@ -35,6 +35,12 @@ namespace System.Globalization {
         //                        Internal Information                        //
         //--------------------------------------------------------------------//
 
+        private enum Tristate : byte
+        {
+            NotInitialized,
+            True,
+            False,
+        }
 
         //
         //  Variables.
@@ -72,7 +78,7 @@ namespace System.Globalization {
         [NonSerialized]private String           m_textInfoName;     // Name of the text info we're using (ie: m_cultureData.STEXTINFO)
         [NonSerialized]private IntPtr           m_dataHandle;       // Sort handle
         [NonSerialized]private IntPtr           m_handleOrigin;
-        [NonSerialized]private bool?            m_IsAsciiCasingSameAsInvariant;
+        [NonSerialized]private Tristate         m_IsAsciiCasingSameAsInvariant = Tristate.NotInitialized;
 
 
         // Invariant text info
@@ -598,14 +604,14 @@ namespace System.Globalization {
         {
             get
             {
-                if (m_IsAsciiCasingSameAsInvariant == null)
+                if (m_IsAsciiCasingSameAsInvariant == Tristate.NotInitialized)
                 {
                     m_IsAsciiCasingSameAsInvariant =
                         CultureInfo.GetCultureInfo(m_textInfoName).CompareInfo.Compare("abcdefghijklmnopqrstuvwxyz",
                                                                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
-                                                                             CompareOptions.IgnoreCase) == 0;
+                                                                             CompareOptions.IgnoreCase) == 0 ? Tristate.True : Tristate.False;
                 }
-                return (bool)m_IsAsciiCasingSameAsInvariant;
+                return m_IsAsciiCasingSameAsInvariant == Tristate.True;
             }
         }