// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** * Copyright (C) 1997-2015, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * * File UMUTEX.H * * Modification History: * * Date Name Description * 04/02/97 aliu Creation. * 04/07/99 srl rewrite - C interface, multiple mutices * 05/13/99 stephen Changed to umutex (from cmutex) ****************************************************************************** */ #ifndef UMUTEX_H #define UMUTEX_H #include <atomic> #include <condition_variable> #include <mutex> #include <type_traits> #include "unicode/utypes.h" #include "unicode/uclean.h" #include "unicode/uobject.h" #include "putilimp.h" #if defined(U_USER_ATOMICS_H) || defined(U_USER_MUTEX_H) // Support for including an alternate implementation of atomic & mutex operations has been withdrawn. // See issue ICU-20185. #error U_USER_ATOMICS and U_USER_MUTEX_H are not supported #endif // Export an explicit template instantiation of std::atomic<int32_t>. // When building DLLs for Windows this is required as it is used as a data member of the exported SharedObject class. // See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples. // // Similar story for std::atomic<std::mutex *>, and the exported UMutex class. #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN && !defined(U_IN_DOXYGEN) #if defined(__clang__) || defined(_MSC_VER) #if defined(__clang__) // Suppress the warning that the explicit instantiation after explicit specialization has no effect. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Winstantiation-after-specialization" #endif template struct U_COMMON_API std::atomic<int32_t>; template struct U_COMMON_API std::atomic<std::mutex *>; #if defined(__clang__) #pragma clang diagnostic pop #endif #elif defined(__GNUC__) // For GCC this class is already exported/visible, so no need for U_COMMON_API. template struct std::atomic<int32_t>; template struct std::atomic<std::mutex *>; #endif #endif U_NAMESPACE_BEGIN /**************************************************************************** * * Low Level Atomic Operations, ICU wrappers for. * ****************************************************************************/ u_atomic_int32_t; inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) { … } inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) { … } inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) { … } inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) { … } /************************************************************************************************* * * UInitOnce Definitions. * *************************************************************************************************/ struct U_COMMON_API UInitOnce { … }; U_COMMON_API UBool U_EXPORT2 umtx_initImplPreInit(UInitOnce &); U_COMMON_API void U_EXPORT2 umtx_initImplPostInit(UInitOnce &); template<class T> void umtx_initOnce(UInitOnce &uio, T *obj, void (U_CALLCONV T::*fp)()) { … } // umtx_initOnce variant for plain functions, or static class functions. // No context parameter. inline void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)()) { … } // umtx_initOnce variant for plain functions, or static class functions. // With ErrorCode, No context parameter. inline void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(UErrorCode &), UErrorCode &errCode) { … } // umtx_initOnce variant for plain functions, or static class functions, // with a context parameter. template<class T> void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(T), T context) { … } // umtx_initOnce variant for plain functions, or static class functions, // with a context parameter and an error code. template<class T> void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(T, UErrorCode &), T context, UErrorCode &errCode) { … } // UMutex should be constexpr-constructible, so that no initialization code // is run during startup. // This works on all C++ libraries except MS VS before VS2019. #if (defined(_CPPLIB_VER) && !defined(_MSVC_STL_VERSION)) || \ (defined(_MSVC_STL_VERSION) && _MSVC_STL_VERSION < 142) // (VS std lib older than VS2017) || (VS std lib version < VS2019) #define UMUTEX_CONSTEXPR #else #define UMUTEX_CONSTEXPR … #endif /** * UMutex - ICU Mutex class. * * This is the preferred Mutex class for use within ICU implementation code. * It is a thin wrapper over C++ std::mutex, with these additions: * - Static instances are safe, not triggering static construction or destruction, * and the associated order of construction or destruction issues. * - Plumbed into u_cleanup() for destructing the underlying std::mutex, * which frees any OS level resources they may be holding. * * Limitations: * - Static or global instances only. Cannot be heap allocated. Cannot appear as a * member of another class. * - No condition variables or other advanced features. If needed, you will need to use * std::mutex and std::condition_variable directly. For an example, see unifiedcache.cpp * * Typical Usage: * static UMutex myMutex; * * { * Mutex lock(myMutex); * ... // Do stuff that is protected by myMutex; * } // myMutex is released when lock goes out of scope. */ class U_COMMON_API 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_CAPI 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_CAPI void U_EXPORT2 umtx_unlock (UMutex* mutex); U_NAMESPACE_END #endif /* UMUTEX_H */ /*eof*/