]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/io/locbund.cpp
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / io / locbund.cpp
index b2dfa1b74d408fd891e454d52c5c2d735d4438d3..8e880b17cf2af11054478958dd9fc969f92d9ada 100644 (file)
@@ -1,7 +1,9 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 *******************************************************************************
 *
-*   Copyright (C) 1998-2011, International Business Machines
+*   Copyright (C) 1998-2014, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
 
 #include "unicode/utypes.h"
 
-#if !UCONFIG_NO_FORMATTING
+#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
 
 #include "locbund.h"
 
 #include "cmemory.h"
 #include "cstring.h"
 #include "ucln_io.h"
+#include "mutex.h"
 #include "umutex.h"
 #include "unicode/ustring.h"
 #include "unicode/uloc.h"
 
-static UBool isFormatsInitialized = FALSE;
 static UNumberFormat *gPosixNumberFormat[ULOCALEBUNDLE_NUMBERFORMAT_COUNT];
 
 U_CDECL_BEGIN
@@ -39,32 +41,28 @@ static UBool U_CALLCONV locbund_cleanup(void) {
         unum_close(gPosixNumberFormat[style]);
         gPosixNumberFormat[style] = NULL;
     }
-    isFormatsInitialized = FALSE;
     return TRUE;
 }
 U_CDECL_END
 
-
+static UMutex gLock = U_MUTEX_INITIALIZER;
 static inline UNumberFormat * copyInvariantFormatter(ULocaleBundle *result, UNumberFormatStyle style) {
+    U_NAMESPACE_USE
+    Mutex lock(&gLock);
     if (result->fNumberFormat[style-1] == NULL) {
-        UErrorCode status = U_ZERO_ERROR;
-        UBool needsInit;
-
-        UMTX_CHECK(NULL, gPosixNumberFormat[style-1] == NULL, needsInit);
-        if (needsInit) {
+        if (gPosixNumberFormat[style-1] == NULL) {
+            UErrorCode status = U_ZERO_ERROR;
             UNumberFormat *formatAlias = unum_open(style, NULL, 0, "en_US_POSIX", NULL, &status);
-
-            /* Cache upon first request. */
             if (U_SUCCESS(status)) {
-                umtx_lock(NULL);
                 gPosixNumberFormat[style-1] = formatAlias;
                 ucln_io_registerCleanup(UCLN_IO_LOCBUND, locbund_cleanup);
-                umtx_unlock(NULL);
             }
         }
-
         /* Copy the needed formatter. */
-        result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status);
+        if (gPosixNumberFormat[style-1] != NULL) {
+            UErrorCode status = U_ZERO_ERROR;
+            result->fNumberFormat[style-1] = unum_clone(gPosixNumberFormat[style-1], &status);
+        }
     }
     return result->fNumberFormat[style-1];
 }