From 0061d3a9b71be46a9b74d0d4b49fe63c079273a8 Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Fri, 9 Aug 2013 19:53:07 +0000 Subject: [PATCH] ICU-10184 umutex.h, change to C++ only, in preparation for build time user mutex support. X-SVN-Rev: 34029 --- icu4c/source/common/cmutex.h | 39 ++++++++++++++++++++++ icu4c/source/common/common.vcxproj | 4 ++- icu4c/source/common/common.vcxproj.filters | 4 +-- icu4c/source/common/ucln_cmn.c | 19 +---------- icu4c/source/common/ucln_cmn.h | 3 +- icu4c/source/common/ucnvmbcs.c | 2 +- icu4c/source/common/{uinit.c => uinit.cpp} | 28 ++++++++++------ icu4c/source/common/umutex.h | 16 +-------- icu4c/source/common/ustr_cnv.c | 4 +-- icu4c/source/i18n/ucln_in.h | 4 +-- icu4c/source/i18n/ulocdata.c | 1 - icu4c/source/io/ucln_io.c | 3 +- icu4c/source/test/cintltst/cintltst.c | 1 - icu4c/source/test/cintltst/hpmufn.c | 1 - 14 files changed, 72 insertions(+), 57 deletions(-) create mode 100644 icu4c/source/common/cmutex.h rename icu4c/source/common/{uinit.c => uinit.cpp} (76%) diff --git a/icu4c/source/common/cmutex.h b/icu4c/source/common/cmutex.h new file mode 100644 index 00000000000..a3814dd3d36 --- /dev/null +++ b/icu4c/source/common/cmutex.h @@ -0,0 +1,39 @@ +/* +********************************************************************** +* Copyright (C) 2013, International Business Machines +* Corporation and others. All Rights Reserved. +********************************************************************** +* +* File cmutex.h +* +* Minimal plain C declarations for ICU mutex functions. +* This header provides a transition path for plain C files that +* formerly included mutex.h, which is now a C++ only header. +* +* This header should not be used for new code. +* +* C++ files should include umutex.h, not this header. +* +*/ + +#ifndef __CMUTEX_H__ +#define __CMUTEX_H__ + +typedef struct UMutex UMutex; + + +/* Lock a mutex. + * @param mutex The given mutex to be locked. Pass NULL to specify + * the global ICU mutex. Recursive locks are an error + * and may cause a deadlock on some platforms. + */ +U_INTERNAL void U_EXPORT2 umtx_lock(UMutex* mutex); + +/* Unlock a mutex. + * @param mutex The given mutex to be unlocked. Pass NULL to specify + * the global ICU mutex. + */ +U_INTERNAL void U_EXPORT2 umtx_unlock (UMutex* mutex); + +#endif + diff --git a/icu4c/source/common/common.vcxproj b/icu4c/source/common/common.vcxproj index 4c35ff8a9c7..6db5d935517 100644 --- a/icu4c/source/common/common.vcxproj +++ b/icu4c/source/common/common.vcxproj @@ -345,7 +345,9 @@ - + + false + false false diff --git a/icu4c/source/common/common.vcxproj.filters b/icu4c/source/common/common.vcxproj.filters index 3c2f7db766e..6e14aa8e8fd 100644 --- a/icu4c/source/common/common.vcxproj.filters +++ b/icu4c/source/common/common.vcxproj.filters @@ -259,7 +259,7 @@ data & memory - + data & memory @@ -1103,4 +1103,4 @@ collections - \ No newline at end of file + diff --git a/icu4c/source/common/ucln_cmn.c b/icu4c/source/common/ucln_cmn.c index 544c489ade6..0c04b0d7f91 100644 --- a/icu4c/source/common/ucln_cmn.c +++ b/icu4c/source/common/ucln_cmn.c @@ -16,7 +16,7 @@ #include "unicode/uclean.h" #include "utracimp.h" #include "ucln_cmn.h" -#include "umutex.h" +#include "cmutex.h" #include "ucln.h" #include "cmemory.h" #include "uassert.h" @@ -25,25 +25,9 @@ #define UCLN_TYPE_IS_COMMON #include "ucln_imp.h" -static UBool gICUInitialized = FALSE; -static UMutex gICUInitMutex = U_MUTEX_INITIALIZER; - static cleanupFunc *gCommonCleanupFunctions[UCLN_COMMON_COUNT]; static cleanupFunc *gLibCleanupFunctions[UCLN_COMMON]; -U_CFUNC UBool ucln_mutexedInit(initFunc *func, UErrorCode *status) { - UBool initialized = FALSE; - umtx_lock(&gICUInitMutex); - if (!gICUInitialized && U_SUCCESS(*status)) { - if (func != NULL) { - func(status); - } - gICUInitialized = TRUE; /* TODO: don't set if U_FAILURE? */ - initialized = TRUE; - } - umtx_unlock(&gICUInitMutex); - return initialized; -} /************************************************ The cleanup order is important in this function. @@ -59,7 +43,6 @@ u_cleanup(void) ucln_lib_cleanup(); cmemory_cleanup(); /* undo any heap functions set by u_setMemoryFunctions(). */ - gICUInitialized = FALSE; UTRACE_EXIT(); /* Must be before utrace_cleanup(), which turns off tracing. */ /*#if U_ENABLE_TRACING*/ utrace_cleanup(); diff --git a/icu4c/source/common/ucln_cmn.h b/icu4c/source/common/ucln_cmn.h index e35653a6a30..1903127a6e7 100644 --- a/icu4c/source/common/ucln_cmn.h +++ b/icu4c/source/common/ucln_cmn.h @@ -1,7 +1,7 @@ /* ****************************************************************************** * * -* Copyright (C) 2001-2012, International Business Machines * +* Copyright (C) 2001-2013, International Business Machines * * Corporation and others. All Rights Reserved. * * * ****************************************************************************** @@ -50,6 +50,7 @@ typedef enum ECleanupCommonType { UCLN_COMMON_UDATA, UCLN_COMMON_PUTIL, UCLN_COMMON_LIST_FORMATTER, + UCLN_COMMON_UINIT, UCLN_COMMON_COUNT /* This must be last */ } ECleanupCommonType; diff --git a/icu4c/source/common/ucnvmbcs.c b/icu4c/source/common/ucnvmbcs.c index eeda8f66f1d..143daf69af7 100644 --- a/icu4c/source/common/ucnvmbcs.c +++ b/icu4c/source/common/ucnvmbcs.c @@ -56,7 +56,7 @@ #include "ucnv_cnv.h" #include "cmemory.h" #include "cstring.h" -#include "umutex.h" +#include "cmutex.h" /* control optimizations according to the platform */ #define MBCS_UNROLL_SINGLE_TO_BMP 1 diff --git a/icu4c/source/common/uinit.c b/icu4c/source/common/uinit.cpp similarity index 76% rename from icu4c/source/common/uinit.c rename to icu4c/source/common/uinit.cpp index db3f252c5fb..d9af2670629 100644 --- a/icu4c/source/common/uinit.c +++ b/icu4c/source/common/uinit.cpp @@ -1,7 +1,7 @@ /* ****************************************************************************** * * -* Copyright (C) 2001-2012, International Business Machines * +* Copyright (C) 2001-2013, International Business Machines * * Corporation and others. All Rights Reserved. * * * ****************************************************************************** @@ -19,13 +19,25 @@ #include "unicode/uclean.h" #include "cmemory.h" #include "icuplugimp.h" -#include "ucln.h" +#include "ucln_cmn.h" #include "ucnv_io.h" +#include "umutex.h" #include "utracimp.h" +static UInitOnce gICUInitOnce = U_INITONCE_INITIALIZER; + +static UBool U_CALLCONV uinit_cleanup() { + gICUInitOnce.reset(); + return TRUE; +} + static void U_CALLCONV -initData(UErrorCode *status) +initData(UErrorCode &status) { + /* initialize plugins */ + uplug_init(&status); + +#if !UCONFIG_NO_CONVERSION /* * 2005-may-02 * @@ -38,9 +50,9 @@ initData(UErrorCode *status) * for errors there, to make sure that the actual items they need are * available. */ -#if !UCONFIG_NO_CONVERSION - ucnv_io_countKnownConverters(status); + ucnv_io_countKnownConverters(&status); #endif + ucln_common_registerCleanup(UCLN_COMMON_UINIT, uinit_cleanup); } /* @@ -49,10 +61,6 @@ initData(UErrorCode *status) U_CAPI void U_EXPORT2 u_init(UErrorCode *status) { UTRACE_ENTRY_OC(UTRACE_U_INIT); - - /* initialize plugins */ - uplug_init(status); - ucln_mutexedInit(initData, status); - + umtx_initOnce(gICUInitOnce, &initData, *status); UTRACE_EXIT_STATUS(*status); } diff --git a/icu4c/source/common/umutex.h b/icu4c/source/common/umutex.h index 4a6a89705b7..52ed0010076 100644 --- a/icu4c/source/common/umutex.h +++ b/icu4c/source/common/umutex.h @@ -85,7 +85,6 @@ inline int32_t umtx_atomic_dec(atomic_int32_t *var) { typedef volatile LONG atomic_int32_t; #define ATOMIC_INT32_T_INITIALIZER(val) val -#ifdef __cplusplus inline int32_t umtx_loadAcquire(atomic_int32_t &var) { return InterlockedCompareExchange(&var, 0, 0); } @@ -102,8 +101,6 @@ inline int32_t umtx_atomic_inc(atomic_int32_t *var) { inline int32_t umtx_atomic_dec(atomic_int32_t *var) { return InterlockedDecrement(var); } -#endif /* __cplusplus */ - #elif U_HAVE_GCC_ATOMICS @@ -113,7 +110,6 @@ inline int32_t umtx_atomic_dec(atomic_int32_t *var) { typedef int32_t atomic_int32_t; #define ATOMIC_INT32_T_INITIALIZER(val) val -#ifdef __cplusplus inline int32_t umtx_loadAcquire(atomic_int32_t &var) { int32_t val = var; __sync_synchronize(); @@ -133,8 +129,6 @@ inline int32_t umtx_atomic_dec(atomic_int32_t *p) { return __sync_sub_and_fetch(p, 1); } -#endif /* __cplusplus */ - #else /* @@ -147,11 +141,9 @@ inline int32_t umtx_atomic_dec(atomic_int32_t *p) { typedef int32_t atomic_int32_t; #define ATOMIC_INT32_T_INITIALIZER(val) val -#ifdef __cplusplus U_INTERNAL int32_t U_EXPORT2 umtx_loadAcquire(atomic_int32_t &var); U_INTERNAL void U_EXPORT2 umtx_storeRelease(atomic_int32_t &var, int32_t val); -#endif /* __cplusplus */ U_INTERNAL int32_t U_EXPORT2 umtx_atomic_inc(atomic_int32_t *p); @@ -171,19 +163,14 @@ U_INTERNAL int32_t U_EXPORT2 umtx_atomic_dec(atomic_int32_t *p); struct UInitOnce { atomic_int32_t fState; UErrorCode fErrCode; -#ifdef __cplusplus void reset() {fState = 0; fState=0;}; UBool isReset() {return umtx_loadAcquire(fState) == 0;}; // Note: isReset() is used by service registration code. // Thread safety of this usage needs review. -#endif }; -typedef struct UInitOnce UInitOnce; + #define U_INITONCE_INITIALIZER {ATOMIC_INT32_T_INITIALIZER(0), U_ZERO_ERROR} -#ifdef __cplusplus -// TODO: get all ICU files using umutex converted to C++, -// then remove the __cpluplus conditionals from this file. U_CAPI UBool U_EXPORT2 umtx_initImplPreInit(UInitOnce &); U_CAPI void U_EXPORT2 umtx_initImplPostInit(UInitOnce &, UBool success); @@ -261,7 +248,6 @@ template void umtx_initOnce(UInitOnce &uio, void (*fp)(T, UErrorCode &) } } -#endif /* __cplusplus */ diff --git a/icu4c/source/common/ustr_cnv.c b/icu4c/source/common/ustr_cnv.c index 0f11991258e..f46d937daed 100644 --- a/icu4c/source/common/ustr_cnv.c +++ b/icu4c/source/common/ustr_cnv.c @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 1998-2010, International Business Machines +* Copyright (C) 1998-2013, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -24,7 +24,7 @@ #include "unicode/ucnv.h" #include "cstring.h" #include "cmemory.h" -#include "umutex.h" +#include "cmutex.h" #include "ustr_cnv.h" /* mutexed access to a shared default converter ----------------------------- */ diff --git a/icu4c/source/i18n/ucln_in.h b/icu4c/source/i18n/ucln_in.h index c27bfa8027c..400b2727713 100644 --- a/icu4c/source/i18n/ucln_in.h +++ b/icu4c/source/i18n/ucln_in.h @@ -14,8 +14,8 @@ * created by: George Rhoten */ -#ifndef __UCLN_CMN_H__ -#define __UCLN_CMN_H__ +#ifndef __UCLN_IN_H__ +#define __UCLN_IN_H__ #include "unicode/utypes.h" #include "ucln.h" diff --git a/icu4c/source/i18n/ulocdata.c b/icu4c/source/i18n/ulocdata.c index 04d062fd812..80e782ceeb9 100644 --- a/icu4c/source/i18n/ulocdata.c +++ b/icu4c/source/i18n/ulocdata.c @@ -17,7 +17,6 @@ #include "cmemory.h" #include "unicode/ustring.h" #include "unicode/ulocdata.h" -#include "umutex.h" #include "uresimp.h" #include "ureslocs.h" diff --git a/icu4c/source/io/ucln_io.c b/icu4c/source/io/ucln_io.c index 791a285c11b..041f4e737ef 100644 --- a/icu4c/source/io/ucln_io.c +++ b/icu4c/source/io/ucln_io.c @@ -1,7 +1,7 @@ /* ****************************************************************************** * * -* Copyright (C) 2001-2011, International Business Machines * +* Copyright (C) 2001-2013, International Business Machines * * Corporation and others. All Rights Reserved. * * * ****************************************************************************** @@ -16,7 +16,6 @@ #include "ucln.h" #include "ucln_io.h" -#include "umutex.h" #include "uassert.h" #ifndef U_IO_IMPLEMENTATION diff --git a/icu4c/source/test/cintltst/cintltst.c b/icu4c/source/test/cintltst/cintltst.c index ecb655e7984..24e1069ca13 100644 --- a/icu4c/source/test/cintltst/cintltst.c +++ b/icu4c/source/test/cintltst/cintltst.c @@ -22,7 +22,6 @@ #include "unicode/putil.h" #include "cstring.h" #include "cintltst.h" -#include "umutex.h" #include "uassert.h" #include "cmemory.h" #include "unicode/uchar.h" diff --git a/icu4c/source/test/cintltst/hpmufn.c b/icu4c/source/test/cintltst/hpmufn.c index 154c7a69f2f..a0e2aa154ce 100644 --- a/icu4c/source/test/cintltst/hpmufn.c +++ b/icu4c/source/test/cintltst/hpmufn.c @@ -14,7 +14,6 @@ #include "unicode/uchar.h" #include "unicode/ures.h" #include "cintltst.h" -#include "umutex.h" #include "unicode/utrace.h" #include #include -- 2.40.0