#error U_USER_MUTEX_CPP not supported
#endif
-// Check that UMutex is trivially constructable & destructable, which ensures that
-// static instances are not running static constructors or destructors.
-#if (defined(__GNUG__) && __GNUC__ < 5) || (defined(__clang__) && __clang_major__ < 5)
-// skip
-#else
-static_assert(std::is_trivially_constructible<UMutex>::value, "UMutex not trivially constructable.");
-static_assert(std::is_trivially_destructible<UMutex>::value, "UMutex not trivially destructable.");
-#endif
-
/*************************************************************************************************
*
std::mutex *initMutex;
std::condition_variable *initCondition;
-// The ICU global mutex. Used when ICU implementation code passes NULL for the mutex pointer.
+// The ICU global mutex.
+// Used when ICU implementation code passes nullptr for the mutex pointer.
UMutex globalMutex;
std::once_flag initFlag;
}
}
+// 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.
class U_COMMON_API UMutex {
public:
- UMutex() = default;
+ UMUTEX_CONSTEXPR UMutex() {}
~UMutex() = default;
UMutex(const UMutex &other) = delete;
static void cleanup();
private:
- alignas(std::mutex) char fStorage[sizeof(std::mutex)];
- std::atomic<std::mutex *> fMutex;
+ 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;
+ UMutex *fListLink { nullptr };
static UMutex *gListHead;
/** Out-of-line function to lazily initialize a UMutex on first use.