]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/timezone.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / i18n / timezone.cpp
index f129d8b607605845e521d0e690648269226358fa..284334ebf7efbba309e30505cdcc1da101627e42 100644 (file)
@@ -527,6 +527,8 @@ TimeZone::detectHostTimeZone()
 
 // -------------------------------------
 
+static UMutex gDefaultZoneMutex;
+
 /**
  * Initialize DEFAULT_ZONE from the system default time zone.  
  * Upon return, DEFAULT_ZONE will not be NULL, unless operator new()
@@ -536,6 +538,7 @@ static void U_CALLCONV initDefault()
 {
     ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
 
+    Mutex lock(&gDefaultZoneMutex);
     // If setDefault() has already been called we can skip getting the
     // default zone information from the system.
     if (DEFAULT_ZONE != NULL) {
@@ -557,9 +560,6 @@ static void U_CALLCONV initDefault()
 
     TimeZone *default_zone = TimeZone::detectHostTimeZone();
 
-    // The only way for DEFAULT_ZONE to be non-null at this point is if the user
-    // made a thread-unsafe call to setDefault() or adoptDefault() in another
-    // thread while this thread was doing something that required getting the default.
     U_ASSERT(DEFAULT_ZONE == NULL);
 
     DEFAULT_ZONE = default_zone;
@@ -571,7 +571,10 @@ TimeZone* U_EXPORT2
 TimeZone::createDefault()
 {
     umtx_initOnce(gDefaultZoneInitOnce, initDefault);
-    return (DEFAULT_ZONE != NULL) ? DEFAULT_ZONE->clone() : NULL;
+    {
+        Mutex lock(&gDefaultZoneMutex);
+        return (DEFAULT_ZONE != NULL) ? DEFAULT_ZONE->clone() : NULL;
+    }
 }
 
 // -------------------------------------
@@ -581,9 +584,12 @@ TimeZone::adoptDefault(TimeZone* zone)
 {
     if (zone != NULL)
     {
-        TimeZone *old = DEFAULT_ZONE;
-        DEFAULT_ZONE = zone;
-        delete old;
+        {
+            Mutex lock(&gDefaultZoneMutex);
+            TimeZone *old = DEFAULT_ZONE;
+            DEFAULT_ZONE = zone;
+            delete old;
+        }
         ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
     }
 }