From: Andy Heninger Date: Thu, 14 Aug 2014 20:34:06 +0000 (+0000) Subject: ICU-11051 Add synchronization to u_getDataDirectory(). X-Git-Tag: milestone-59-0-1~1736 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bbefa308aa5221fd0b0de6686dc2bb5b2087d4ba;p=icu ICU-11051 Add synchronization to u_getDataDirectory(). X-SVN-Rev: 36165 --- diff --git a/icu4c/source/common/putil.cpp b/icu4c/source/common/putil.cpp index 388b6aabed0..0e01355278f 100644 --- a/icu4c/source/common/putil.cpp +++ b/icu4c/source/common/putil.cpp @@ -1112,7 +1112,10 @@ uprv_tzname(int n) /* Get and set the ICU data directory --------------------------------------- */ +static icu::UInitOnce gDataDirInitOnce = U_INITONCE_INITIALIZER; static char *gDataDirectory = NULL; + + #if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API static char *gCorrectedPOSIXLocale = NULL; /* Heap allocated */ #endif @@ -1123,6 +1126,8 @@ static UBool U_CALLCONV putil_cleanup(void) uprv_free(gDataDirectory); } gDataDirectory = NULL; + gDataDirInitOnce.reset(); + #if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API if (gCorrectedPOSIXLocale) { uprv_free(gCorrectedPOSIXLocale); @@ -1210,18 +1215,19 @@ uprv_pathIsAbsolute(const char *path) # endif #endif -U_CAPI const char * U_EXPORT2 -u_getDataDirectory(void) { +static void U_CALLCONV dataDirectoryInitFn() { + /* If we already have the directory, then return immediately. Will happen if user called + * u_setDataDirectory(). + */ + if (gDataDirectory) { + return; + } + const char *path = NULL; #if defined(ICU_DATA_DIR_PREFIX_ENV_VAR) char datadir_path_buffer[PATH_MAX]; #endif - /* if we have the directory, then return it immediately */ - if(gDataDirectory) { - return gDataDirectory; - } - /* When ICU_NO_USER_DATA_OVERRIDE is defined, users aren't allowed to override ICU's data with the ICU_DATA environment variable. This prevents @@ -1272,6 +1278,12 @@ u_getDataDirectory(void) { } u_setDataDirectory(path); + return; +} + +U_CAPI const char * U_EXPORT2 +u_getDataDirectory(void) { + umtx_initOnce(gDataDirInitOnce, &dataDirectoryInitFn); return gDataDirectory; }