2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2012-10-05 21:22:02 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1997-2016, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2012-10-05 21:22:02 +00:00
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* File umutex.cpp
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 04/02/97 aliu Creation.
|
|
|
|
* 04/07/99 srl updated
|
|
|
|
* 05/13/99 stephen Changed to umutex (from cmutex).
|
|
|
|
* 11/22/99 aliu Make non-global mutex autoinitialize [j151]
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2013-06-01 03:37:16 +00:00
|
|
|
#include "umutex.h"
|
|
|
|
|
2012-10-05 21:22:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "uassert.h"
|
2019-05-10 23:23:21 +00:00
|
|
|
#include "ucln_cmn.h"
|
2013-06-01 03:37:16 +00:00
|
|
|
#include "cmemory.h"
|
2012-10-05 21:22:02 +00:00
|
|
|
|
2018-11-01 00:08:38 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
2013-09-06 01:41:14 +00:00
|
|
|
|
2012-10-05 21:22:02 +00:00
|
|
|
|
2013-08-30 01:23:42 +00:00
|
|
|
#if defined(U_USER_MUTEX_CPP)
|
2018-10-31 21:30:58 +00:00
|
|
|
// Support for including an alternate implementation of mutexes has been withdrawn.
|
|
|
|
// See issue ICU-20185.
|
|
|
|
#error U_USER_MUTEX_CPP not supported
|
|
|
|
#endif
|
2013-08-30 01:23:42 +00:00
|
|
|
|
2019-05-10 23:23:21 +00:00
|
|
|
|
2018-11-01 00:08:38 +00:00
|
|
|
/*************************************************************************************************
|
|
|
|
*
|
|
|
|
* ICU Mutex wrappers.
|
|
|
|
*
|
|
|
|
*************************************************************************************************/
|
2018-09-29 00:02:04 +00:00
|
|
|
|
2019-05-10 23:23:21 +00:00
|
|
|
namespace {
|
|
|
|
std::mutex *initMutex;
|
|
|
|
std::condition_variable *initCondition;
|
|
|
|
|
2019-10-01 23:33:53 +00:00
|
|
|
// The ICU global mutex.
|
|
|
|
// Used when ICU implementation code passes nullptr for the mutex pointer.
|
2019-05-10 23:23:21 +00:00
|
|
|
UMutex globalMutex;
|
|
|
|
|
|
|
|
std::once_flag initFlag;
|
|
|
|
std::once_flag *pInitFlag = &initFlag;
|
|
|
|
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static UBool U_CALLCONV umtx_cleanup() {
|
|
|
|
initMutex->~mutex();
|
|
|
|
initCondition->~condition_variable();
|
|
|
|
UMutex::cleanup();
|
|
|
|
|
|
|
|
// Reset the once_flag, by destructing it and creating a fresh one in its place.
|
|
|
|
// Do not use this trick anywhere else in ICU; use umtx_initOnce, not std::call_once().
|
|
|
|
pInitFlag->~once_flag();
|
|
|
|
pInitFlag = new(&initFlag) std::once_flag();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void U_CALLCONV umtx_init() {
|
|
|
|
initMutex = STATIC_NEW(std::mutex);
|
|
|
|
initCondition = STATIC_NEW(std::condition_variable);
|
|
|
|
ucln_common_registerCleanup(UCLN_COMMON_MUTEX, umtx_cleanup);
|
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
|
|
|
|
|
|
|
|
std::mutex *UMutex::getMutex() {
|
|
|
|
std::mutex *retPtr = fMutex.load(std::memory_order_acquire);
|
|
|
|
if (retPtr == nullptr) {
|
|
|
|
std::call_once(*pInitFlag, umtx_init);
|
|
|
|
std::lock_guard<std::mutex> guard(*initMutex);
|
2019-07-03 21:04:36 +00:00
|
|
|
retPtr = fMutex.load(std::memory_order_acquire);
|
|
|
|
if (retPtr == nullptr) {
|
2019-05-10 23:23:21 +00:00
|
|
|
fMutex = new(fStorage) std::mutex();
|
|
|
|
retPtr = fMutex;
|
|
|
|
fListLink = gListHead;
|
|
|
|
gListHead = this;
|
|
|
|
}
|
|
|
|
}
|
2019-08-15 01:32:43 +00:00
|
|
|
U_ASSERT(retPtr != nullptr);
|
2019-05-10 23:23:21 +00:00
|
|
|
return retPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
UMutex *UMutex::gListHead = nullptr;
|
|
|
|
|
|
|
|
void UMutex::cleanup() {
|
|
|
|
UMutex *next = nullptr;
|
|
|
|
for (UMutex *m = gListHead; m != nullptr; m = next) {
|
|
|
|
(*m->fMutex).~mutex();
|
|
|
|
m->fMutex = nullptr;
|
|
|
|
next = m->fListLink;
|
|
|
|
m->fListLink = nullptr;
|
|
|
|
}
|
|
|
|
gListHead = nullptr;
|
2019-03-11 23:36:33 +00:00
|
|
|
}
|
2018-09-29 00:02:04 +00:00
|
|
|
|
2019-05-10 23:23:21 +00:00
|
|
|
|
2018-09-29 00:02:04 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
umtx_lock(UMutex *mutex) {
|
|
|
|
if (mutex == nullptr) {
|
2019-05-10 23:23:21 +00:00
|
|
|
mutex = &globalMutex;
|
2018-09-29 00:02:04 +00:00
|
|
|
}
|
2019-05-10 23:23:21 +00:00
|
|
|
mutex->lock();
|
2018-09-29 00:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
umtx_unlock(UMutex* mutex)
|
|
|
|
{
|
|
|
|
if (mutex == nullptr) {
|
2019-05-10 23:23:21 +00:00
|
|
|
mutex = &globalMutex;
|
2018-09-29 00:02:04 +00:00
|
|
|
}
|
2019-05-10 23:23:21 +00:00
|
|
|
mutex->unlock();
|
2018-09-29 00:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-01 00:08:38 +00:00
|
|
|
/*************************************************************************************************
|
|
|
|
*
|
|
|
|
* UInitOnce Implementation
|
|
|
|
*
|
|
|
|
*************************************************************************************************/
|
2018-09-29 00:02:04 +00:00
|
|
|
|
|
|
|
// This function is called when a test of a UInitOnce::fState reveals that
|
|
|
|
// initialization has not completed, that we either need to call the init
|
|
|
|
// function on this thread, or wait for some other thread to complete.
|
|
|
|
//
|
|
|
|
// The actual call to the init function is made inline by template code
|
|
|
|
// that knows the C++ types involved. This function returns true if
|
|
|
|
// the caller needs to call the Init function.
|
|
|
|
//
|
|
|
|
U_COMMON_API UBool U_EXPORT2
|
|
|
|
umtx_initImplPreInit(UInitOnce &uio) {
|
2019-05-10 23:23:21 +00:00
|
|
|
std::call_once(*pInitFlag, umtx_init);
|
|
|
|
std::unique_lock<std::mutex> lock(*initMutex);
|
2018-09-29 00:02:04 +00:00
|
|
|
if (umtx_loadAcquire(uio.fState) == 0) {
|
|
|
|
umtx_storeRelease(uio.fState, 1);
|
|
|
|
return true; // Caller will next call the init function.
|
|
|
|
} else {
|
|
|
|
while (umtx_loadAcquire(uio.fState) == 1) {
|
|
|
|
// Another thread is currently running the initialization.
|
|
|
|
// Wait until it completes.
|
2019-05-10 23:23:21 +00:00
|
|
|
initCondition->wait(lock);
|
2018-09-29 00:02:04 +00:00
|
|
|
}
|
|
|
|
U_ASSERT(uio.fState == 2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function is called by the thread that ran an initialization function,
|
|
|
|
// just after completing the function.
|
|
|
|
// Some threads may be waiting on the condition, requiring the broadcast wakeup.
|
|
|
|
// Some threads may be racing to test the fState variable outside of the mutex,
|
|
|
|
// requiring the use of store/release when changing its value.
|
|
|
|
|
|
|
|
U_COMMON_API void U_EXPORT2
|
|
|
|
umtx_initImplPostInit(UInitOnce &uio) {
|
|
|
|
{
|
2019-05-10 23:23:21 +00:00
|
|
|
std::unique_lock<std::mutex> lock(*initMutex);
|
2018-09-29 00:02:04 +00:00
|
|
|
umtx_storeRelease(uio.fState, 2);
|
|
|
|
}
|
2019-05-10 23:23:21 +00:00
|
|
|
initCondition->notify_all();
|
2018-09-29 00:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2018-11-01 00:08:38 +00:00
|
|
|
/*************************************************************************************************
|
|
|
|
*
|
|
|
|
* Deprecated functions for setting user mutexes.
|
|
|
|
*
|
|
|
|
*************************************************************************************************/
|
2012-10-05 21:22:02 +00:00
|
|
|
|
2013-08-29 21:36:06 +00:00
|
|
|
U_DEPRECATED void U_EXPORT2
|
|
|
|
u_setMutexFunctions(const void * /*context */, UMtxInitFn *, UMtxFn *,
|
2013-06-01 03:37:16 +00:00
|
|
|
UMtxFn *, UMtxFn *, UErrorCode *status) {
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
2012-10-05 21:22:02 +00:00
|
|
|
}
|
2013-06-01 03:37:16 +00:00
|
|
|
return;
|
2012-10-05 21:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-01 03:37:16 +00:00
|
|
|
|
|
|
|
U_DEPRECATED void U_EXPORT2
|
|
|
|
u_setAtomicIncDecFunctions(const void * /*context */, UMtxAtomicFn *, UMtxAtomicFn *,
|
|
|
|
UErrorCode *status) {
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
*status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
|
|
|
return;
|
2012-10-05 21:22:02 +00:00
|
|
|
}
|