diff options
author | mcheshkov <mcheshkov@yandex-team.ru> | 2022-02-10 16:46:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:15 +0300 |
commit | e9d19cec64684c9c1e6b0c98297e5b895cf904fe (patch) | |
tree | 2768b1223e96a8a0610a93d18425d9647c1123c8 /contrib/libs/icu/common/umutex.h | |
parent | 60040c91ffe701a84689b2c6310ff845e65cff42 (diff) | |
download | ydb-e9d19cec64684c9c1e6b0c98297e5b895cf904fe.tar.gz |
Restoring authorship annotation for <mcheshkov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'contrib/libs/icu/common/umutex.h')
-rw-r--r-- | contrib/libs/icu/common/umutex.h | 214 |
1 files changed, 107 insertions, 107 deletions
diff --git a/contrib/libs/icu/common/umutex.h b/contrib/libs/icu/common/umutex.h index 2503aa4a29..e45aa5012b 100644 --- a/contrib/libs/icu/common/umutex.h +++ b/contrib/libs/icu/common/umutex.h @@ -1,4 +1,4 @@ -// © 2016 and later: Unicode, Inc. and others. +// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** @@ -20,53 +20,53 @@ #ifndef UMUTEX_H #define UMUTEX_H -#include <atomic> -#include <condition_variable> -#include <mutex> -#include <type_traits> - +#include <atomic> +#include <condition_variable> +#include <mutex> +#include <type_traits> + #include "unicode/utypes.h" #include "unicode/uclean.h" -#include "unicode/uobject.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 +#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. + * Low Level Atomic Operations, ICU wrappers for. * ****************************************************************************/ @@ -99,8 +99,8 @@ inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) { struct UInitOnce { u_atomic_int32_t fState; UErrorCode fErrCode; - void reset() {fState = 0;} - UBool isReset() {return umtx_loadAcquire(fState) == 0;} + void reset() {fState = 0;} + UBool isReset() {return umtx_loadAcquire(fState) == 0;} // Note: isReset() is used by service registration code. // Thread safety of this usage needs review. }; @@ -184,76 +184,76 @@ template<class T> void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(T, UE } } -// 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 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. +// 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 constexpr +#endif + +/** + * UMutex - ICU Mutex class. * - * 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 + * 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. * - * Typical Usage: - * static UMutex myMutex; - * - * { - * Mutex lock(myMutex); - * ... // Do stuff that is protected by myMutex; - * } // myMutex is released when lock goes out of scope. + * 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 { -public: - UMUTEX_CONSTEXPR UMutex() {} - ~UMutex() = default; - - UMutex(const UMutex &other) = delete; - UMutex &operator =(const UMutex &other) = delete; - void *operator new(size_t) = delete; - - // requirements for C++ BasicLockable, allows UMutex to work with std::lock_guard - void lock() { - std::mutex *m = fMutex.load(std::memory_order_acquire); - if (m == nullptr) { m = getMutex(); } - m->lock(); - } - void unlock() { fMutex.load(std::memory_order_relaxed)->unlock(); } - - static void cleanup(); - -private: - alignas(std::mutex) char fStorage[sizeof(std::mutex)] {}; - std::atomic<std::mutex *> fMutex { nullptr }; - - /** All initialized UMutexes are kept in a linked list, so that they can be found, - * and the underlying std::mutex destructed, by u_cleanup(). - */ - UMutex *fListLink { nullptr }; - static UMutex *gListHead; - - /** Out-of-line function to lazily initialize a UMutex on first use. - * Initial fast check is inline, in lock(). The returned value may never - * be nullptr. - */ - std::mutex *getMutex(); +class U_COMMON_API UMutex { +public: + UMUTEX_CONSTEXPR UMutex() {} + ~UMutex() = default; + + UMutex(const UMutex &other) = delete; + UMutex &operator =(const UMutex &other) = delete; + void *operator new(size_t) = delete; + + // requirements for C++ BasicLockable, allows UMutex to work with std::lock_guard + void lock() { + std::mutex *m = fMutex.load(std::memory_order_acquire); + if (m == nullptr) { m = getMutex(); } + m->lock(); + } + void unlock() { fMutex.load(std::memory_order_relaxed)->unlock(); } + + static void cleanup(); + +private: + alignas(std::mutex) char fStorage[sizeof(std::mutex)] {}; + std::atomic<std::mutex *> fMutex { nullptr }; + + /** All initialized UMutexes are kept in a linked list, so that they can be found, + * and the underlying std::mutex destructed, by u_cleanup(). + */ + UMutex *fListLink { nullptr }; + static UMutex *gListHead; + + /** Out-of-line function to lazily initialize a UMutex on first use. + * Initial fast check is inline, in lock(). The returned value may never + * be nullptr. + */ + std::mutex *getMutex(); }; @@ -271,7 +271,7 @@ U_INTERNAL void U_EXPORT2 umtx_lock(UMutex* mutex); U_INTERNAL void U_EXPORT2 umtx_unlock (UMutex* mutex); -U_NAMESPACE_END +U_NAMESPACE_END #endif /* UMUTEX_H */ /*eof*/ |