[TimeZone] Fixed race condition with renewing the CurrentTimeZone object.
[mono.git] / mcs / class / corlib / System / TimeZone.cs
index 1a682476d5d7a46a8891b99983b78e4c2d6af72c..cb7caeff3f87218bf1e8e17f0685ff05a3bf41e7 100644 (file)
@@ -55,6 +55,8 @@ namespace System
                // Fields
                static TimeZone currentTimeZone;
 
+               [NonSerialized]
+               static object tz_lock = new object ();
                [NonSerialized]
                static long timezone_check;
 
@@ -67,13 +69,18 @@ namespace System
                public static TimeZone CurrentTimeZone {
                        get {
                                long now = DateTime.GetNow ();
+                               TimeZone tz;
                                
-                               if (currentTimeZone == null || (now - timezone_check) > TimeSpan.TicksPerMinute) {
-                                       currentTimeZone = new CurrentSystemTimeZone (now);
-                                       timezone_check = now;
+                               lock (tz_lock) {
+                                       if (currentTimeZone == null || (now - timezone_check) > TimeSpan.TicksPerMinute) {
+                                               currentTimeZone = new CurrentSystemTimeZone (now);
+                                               timezone_check = now;
+                                       }
+                                       
+                                       tz = currentTimeZone;
                                }
                                
-                               return currentTimeZone;
+                               return tz;
                        }
                }