make the returning of DTFI & NFI in CultureInfo atomic (#7998)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Fri, 4 Nov 2016 20:15:17 +0000 (13:15 -0700)
committerJan Kotas <jkotas@microsoft.com>
Fri, 4 Nov 2016 20:15:17 +0000 (13:15 -0700)
we got some cases when more than one thread trying to get DTFI or NFI from the shared current culture object which can result returning different instances.
Although the returning object contents will be exactly but it'll be better to return same instance as apps may assume that. also returning the same instance will help reduce reinitializing more deeper fields when get requested.

src/mscorlib/corefx/System/Globalization/CultureInfo.cs
src/mscorlib/src/System/Globalization/CultureInfo.cs

index 7d44282..ceee47c 100644 (file)
@@ -1024,7 +1024,7 @@ namespace System.Globalization
                 {
                     NumberFormatInfo temp = new NumberFormatInfo(this.m_cultureData);
                     temp.isReadOnly = m_isReadOnly;
-                    numInfo = temp;
+                    Interlocked.CompareExchange(ref numInfo, temp, null);
                 }
                 return (numInfo);
             }
@@ -1056,8 +1056,7 @@ namespace System.Globalization
                     // Change the calendar of DTFI to the specified calendar of this CultureInfo.
                     DateTimeFormatInfo temp = new DateTimeFormatInfo(this.m_cultureData, this.Calendar);
                     temp._isReadOnly = m_isReadOnly;
-                    System.Threading.Interlocked.MemoryBarrier();
-                    dateTimeInfo = temp;
+                    Interlocked.CompareExchange(ref dateTimeInfo, temp, null);
                 }
                 return (dateTimeInfo);
             }
index baaf334..1ef3809 100644 (file)
@@ -1345,7 +1345,7 @@ namespace System.Globalization {
                 if (numInfo == null) {
                     NumberFormatInfo temp = new NumberFormatInfo(this.m_cultureData);
                     temp.isReadOnly = m_isReadOnly;
-                    numInfo = temp;
+                    Interlocked.CompareExchange(ref numInfo, temp, null);
                 }
                 return (numInfo);
             }
@@ -1379,8 +1379,7 @@ namespace System.Globalization {
                     DateTimeFormatInfo temp = new DateTimeFormatInfo(
                         this.m_cultureData, this.Calendar);
                     temp.m_isReadOnly = m_isReadOnly;
-                    System.Threading.Thread.MemoryBarrier();
-                    dateTimeInfo = temp;
+                    Interlocked.CompareExchange(ref dateTimeInfo, temp, null);
                 }
                 return (dateTimeInfo);
             }