[TimeZone] Fixed race condition with renewing the CurrentTimeZone object.
authorJeffrey Stedfast <jeff@xamarin.com>
Thu, 29 Sep 2011 14:56:13 +0000 (10:56 -0400)
committerJeffrey Stedfast <jeff@xamarin.com>
Thu, 29 Sep 2011 14:56:13 +0000 (10:56 -0400)
Fixes bug #1055.

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;
                        }
                }