Replace ARP Mutex and Condition with std::mutx and std::conditional_variable

master
Nicky 2018-04-05 12:02:12 +02:00
parent 03c79ba6cb
commit ef20e3f523
30 changed files with 101 additions and 153 deletions

View File

@ -33,8 +33,8 @@
apr_pool_t *gAPRPoolp = NULL; // Global APR memory pool
LLVolatileAPRPool *LLAPRFile::sAPRFilePoolp = NULL ; //global volatile APR memory pool.
apr_thread_mutex_t *gLogMutexp = NULL;
apr_thread_mutex_t *gCallStacksLogMutexp = NULL;
std::mutex *gLogMutexp = nullptr;
std::mutex *gCallStacksLogMutexp = nullptr;
const S32 FULL_VOLATILE_APR_POOL = 1024 ; //number of references to LLVolatileAPRPool
@ -49,9 +49,8 @@ void ll_init_apr()
{
apr_pool_create(&gAPRPoolp, NULL);
// Initialize the logging mutex
apr_thread_mutex_create(&gLogMutexp, APR_THREAD_MUTEX_UNNESTED, gAPRPoolp);
apr_thread_mutex_create(&gCallStacksLogMutexp, APR_THREAD_MUTEX_UNNESTED, gAPRPoolp);
gLogMutexp = new std::mutex();
gCallStacksLogMutexp = new std::mutex();
}
if(!LLAPRFile::sAPRFilePoolp)
@ -75,22 +74,12 @@ void ll_cleanup_apr()
LL_INFOS("APR") << "Cleaning up APR" << LL_ENDL;
if (gLogMutexp)
{
// Clean up the logging mutex
// All other threads NEED to be done before we clean up APR, so this is okay.
apr_thread_mutex_destroy(gLogMutexp);
gLogMutexp = NULL;
}
if (gCallStacksLogMutexp)
{
// Clean up the logging mutex
// All other threads NEED to be done before we clean up APR, so this is okay.
apr_thread_mutex_destroy(gCallStacksLogMutexp);
gCallStacksLogMutexp = NULL;
}
// Clean up the logging mutex
// All other threads NEED to be done before we clean up APR, so this is okay.
delete gLogMutexp;
gLogMutexp = nullptr;
delete gCallStacksLogMutexp;
gCallStacksLogMutexp = nullptr;
LLThreadLocalPointerBase::destroyAllThreadLocalStorage();
@ -176,18 +165,14 @@ LLVolatileAPRPool::LLVolatileAPRPool(BOOL is_local, apr_pool_t *parent, apr_size
if(!is_local) //not a local apr_pool, that is: shared by multiple threads.
{
apr_pool_create(&mMutexPool, NULL); // Create a pool for mutex
apr_thread_mutex_create(&mMutexp, APR_THREAD_MUTEX_UNNESTED, mMutexPool);
mMutexp = new std::mutex();
}
}
LLVolatileAPRPool::~LLVolatileAPRPool()
{
//delete mutex
if(mMutexp)
{
apr_thread_mutex_destroy(mMutexp);
apr_pool_destroy(mMutexPool);
}
delete mMutexp;
mMutexp = nullptr;
}
//
@ -254,18 +239,12 @@ BOOL LLVolatileAPRPool::isFull()
//
// LLScopedLock
//
LLScopedLock::LLScopedLock(apr_thread_mutex_t* mutex) : mMutex(mutex)
LLScopedLock::LLScopedLock(std::mutex* mutex) : mMutex(mutex)
{
if(mutex)
{
if(ll_apr_warn_status(apr_thread_mutex_lock(mMutex)))
{
mLocked = false;
}
else
{
mLocked = true;
}
mutex->lock();
mLocked = true;
}
else
{
@ -282,10 +261,7 @@ void LLScopedLock::unlock()
{
if(mLocked)
{
if(!ll_apr_warn_status(apr_thread_mutex_unlock(mMutex)))
{
mLocked = false;
}
mMutex->unlock();
}
}

View File

@ -36,15 +36,25 @@
#include <boost/noncopyable.hpp>
#include "llwin32headerslean.h"
#include "apr_thread_proc.h"
#include "apr_thread_mutex.h"
#include "apr_getopt.h"
#include "apr_signal.h"
#include <atomic>
#if LL_WINDOWS
#pragma warning(disable:4265)
#endif
#include <mutex>
#if LL_WINDOWS
#pragma warning(default:4265)
#endif
#include "llstring.h"
extern LL_COMMON_API apr_thread_mutex_t* gLogMutexp;
extern apr_thread_mutex_t* gCallStacksLogMutexp;
extern LL_COMMON_API std::mutex* gLogMutexp;
extern std::mutex* gCallStacksLogMutexp;
struct apr_dso_handle_t;
/**
@ -120,7 +130,7 @@ private:
S32 mNumActiveRef ; //number of active pointers pointing to the apr_pool.
S32 mNumTotalRef ; //number of total pointers pointing to the apr_pool since last creating.
apr_thread_mutex_t *mMutexp;
std::mutex *mMutexp;
apr_pool_t *mMutexPool;
} ;
@ -142,7 +152,7 @@ public:
* @param mutex An allocated APR mutex. If you pass in NULL,
* this wrapper will not lock.
*/
LLScopedLock(apr_thread_mutex_t* mutex);
LLScopedLock( std::mutex* mutex );
/**
* @brief Destructor which unlocks the mutex if still locked.
@ -161,7 +171,7 @@ public:
protected:
bool mLocked;
apr_thread_mutex_t* mMutex;
std::mutex* mMutex;
};
template <typename Type, typename AtomicType> class LLAtomic

View File

@ -1018,8 +1018,7 @@ namespace {
const int MAX_RETRIES = 5;
for (int attempts = 0; attempts < MAX_RETRIES; ++attempts)
{
apr_status_t s = apr_thread_mutex_trylock(gLogMutexp);
if (!APR_STATUS_IS_EBUSY(s))
if( gLogMutexp->try_lock() )
{
mLocked = true;
mOK = true;
@ -1041,7 +1040,7 @@ namespace {
{
if (mLocked)
{
apr_thread_mutex_unlock(gLogMutexp);
gLogMutexp->unlock();
}
}
}

View File

@ -30,8 +30,7 @@
LLFixedBuffer::LLFixedBuffer(const U32 max_lines)
: LLLineBuffer(),
mMaxLines(max_lines),
mMutex(NULL)
mMaxLines(max_lines)
{
mTimer.reset();
}

View File

@ -1122,7 +1122,7 @@ LLPrivateMemoryPool::LLPrivateMemoryPool(S32 type, U32 max_pool_size) :
{
if(type == STATIC_THREADED || type == VOLATILE_THREADED)
{
mMutexp = new LLMutex(NULL) ;
mMutexp = new LLMutex() ;
}
for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)

View File

@ -33,20 +33,11 @@
//============================================================================
LLMutex::LLMutex(apr_pool_t *poolp) :
mAPRMutexp(NULL), mCount(0), mLockingThread(NO_THREAD)
LLMutex::LLMutex() :
mCount(0), mLockingThread(NO_THREAD)
{
//if (poolp)
//{
// mIsLocalPool = FALSE;
// mAPRPoolp = poolp;
//}
//else
{
mIsLocalPool = TRUE;
apr_pool_create(&mAPRPoolp, NULL); // Create a subpool for this thread
}
apr_thread_mutex_create(&mAPRMutexp, APR_THREAD_MUTEX_UNNESTED, mAPRPoolp);
mMutexp = new std::mutex();
}
@ -56,15 +47,8 @@ LLMutex::~LLMutex()
//bad assertion, the subclass LLSignal might be "locked", and that's OK
//llassert_always(!isLocked()); // better not be locked!
#endif
if (ll_apr_is_initialized())
{
apr_thread_mutex_destroy(mAPRMutexp);
if (mIsLocalPool)
{
apr_pool_destroy(mAPRPoolp);
}
}
mAPRMutexp = NULL;
delete mMutexp;
mMutexp = nullptr;
}
@ -76,7 +60,7 @@ void LLMutex::lock()
return;
}
apr_thread_mutex_lock(mAPRMutexp);
mMutexp->lock();
#if MUTEX_DEBUG
// Have to have the lock before we can access the debug info
@ -106,19 +90,18 @@ void LLMutex::unlock()
#endif
mLockingThread = NO_THREAD;
apr_thread_mutex_unlock(mAPRMutexp);
mMutexp->unlock();
}
bool LLMutex::isLocked()
{
apr_status_t status = apr_thread_mutex_trylock(mAPRMutexp);
if (APR_STATUS_IS_EBUSY(status))
if (!mMutexp->try_lock())
{
return true;
}
else
{
apr_thread_mutex_unlock(mAPRMutexp);
mMutexp->unlock();
return false;
}
}
@ -141,8 +124,7 @@ bool LLMutex::trylock()
return true;
}
apr_status_t status(apr_thread_mutex_trylock(mAPRMutexp));
if (APR_STATUS_IS_EBUSY(status))
if (!mMutexp->try_lock())
{
return false;
}
@ -161,44 +143,35 @@ bool LLMutex::trylock()
//============================================================================
LLCondition::LLCondition(apr_pool_t *poolp) :
LLMutex(poolp)
LLCondition::LLCondition()
{
// base class (LLMutex) has already ensured that mAPRPoolp is set up.
apr_thread_cond_create(&mAPRCondp, mAPRPoolp);
mCondp = new std::condition_variable();
}
LLCondition::~LLCondition()
{
apr_thread_cond_destroy(mAPRCondp);
mAPRCondp = NULL;
delete mCondp;
mCondp = nullptr;
}
void LLCondition::wait()
{
if (!isLocked())
{ //mAPRMutexp MUST be locked before calling apr_thread_cond_wait
apr_thread_mutex_lock(mAPRMutexp);
#if MUTEX_DEBUG
// avoid asserts on destruction in non-release builds
U32 id = LLThread::currentID();
mIsLocked[id] = TRUE;
#endif
}
apr_thread_cond_wait(mAPRCondp, mAPRMutexp);
std::unique_lock< std::mutex > lock( *mMutexp );
mCondp->wait( lock );
}
void LLCondition::signal()
{
apr_thread_cond_signal(mAPRCondp);
mCondp->notify_one() ;
}
void LLCondition::broadcast()
{
apr_thread_cond_broadcast(mAPRCondp);
mCondp->notify_all();
}

View File

@ -29,6 +29,16 @@
#include "stdtypes.h"
#if LL_WINDOWS
#pragma warning(disable:4265)
#endif
#include <mutex>
#include <condition_variable>
#if LL_WINDOWS
#pragma warning(default:4265)
#endif
//============================================================================
#define MUTEX_DEBUG (LL_DEBUG || LL_RELEASE_WITH_DEBUG_INFO)
@ -37,9 +47,6 @@
#include <map>
#endif
struct apr_thread_mutex_t;
struct apr_pool_t;
struct apr_thread_cond_t;
class LL_COMMON_API LLMutex
{
@ -49,7 +56,7 @@ public:
NO_THREAD = 0xFFFFFFFF
} e_locking_thread;
LLMutex(apr_pool_t *apr_poolp = NULL); // NULL pool constructs a new pool for the mutex
LLMutex(); // NULL pool constructs a new pool for the mutex
virtual ~LLMutex();
void lock(); // blocks
@ -60,11 +67,10 @@ public:
U32 lockingThread() const; //get ID of locking thread
protected:
apr_thread_mutex_t *mAPRMutexp;
std::mutex *mMutexp;
mutable U32 mCount;
mutable U32 mLockingThread;
apr_pool_t *mAPRPoolp;
BOOL mIsLocalPool;
#if MUTEX_DEBUG
@ -76,7 +82,7 @@ protected:
class LL_COMMON_API LLCondition : public LLMutex
{
public:
LLCondition(apr_pool_t* apr_poolp); // Defaults to global pool, could use the thread pool as well.
LLCondition(); // Defaults to global pool, could use the thread pool as well.
~LLCondition();
void wait(); // blocks
@ -84,7 +90,7 @@ public:
void broadcast();
protected:
apr_thread_cond_t* mAPRCondp;
std::condition_variable* mCondp;
};
class LLMutexLock

View File

@ -36,10 +36,10 @@
LLQueuedThread::LLQueuedThread(const std::string& name, bool threaded, bool should_pause) :
LLThread(name),
mThreaded(threaded),
mIdleThread(TRUE),
mNextHandle(0),
mStarted(FALSE)
{
mIdleThread = true;
if (mThreaded)
{
if(should_pause)

View File

@ -183,8 +183,8 @@ LLThread::LLThread(const std::string& name, apr_pool_t *poolp) :
mIsLocalPool = TRUE;
apr_pool_create(&mAPRPoolp, NULL); // Create a subpool for this thread
}
mRunCondition = new LLCondition(mAPRPoolp);
mDataLock = new LLMutex(mAPRPoolp);
mRunCondition = new LLCondition();
mDataLock = new LLMutex();
mLocalAPRFilePoolp = NULL ;
}
@ -409,7 +409,7 @@ void LLThreadSafeRefCount::initThreadSafeRefCount()
{
if (!sMutex)
{
sMutex = new LLMutex(0);
sMutex = new LLMutex();
}
}

View File

@ -738,7 +738,7 @@ void LLUUID::getCurrentTime(uuid_time_t *timestamp)
getSystemTime(&time_last);
uuids_this_tick = uuids_per_tick;
init = TRUE;
mMutex = new LLMutex(NULL);
mMutex = new LLMutex();
}
uuid_time_t time_now = {0,0};

View File

@ -37,7 +37,7 @@
LLWorkerThread::LLWorkerThread(const std::string& name, bool threaded, bool should_pause) :
LLQueuedThread(name, threaded, should_pause)
{
mDeleteMutex = new LLMutex(NULL);
mDeleteMutex = new LLMutex();
if(!mLocalAPRFilePoolp)
{
@ -204,7 +204,6 @@ LLWorkerClass::LLWorkerClass(LLWorkerThread* workerthread, const std::string& na
mWorkerClassName(name),
mRequestHandle(LLWorkerThread::nullHandle()),
mRequestPriority(LLWorkerThread::PRIORITY_NORMAL),
mMutex(NULL),
mWorkFlags(0)
{
if (!mWorkerThread)

View File

@ -333,7 +333,7 @@ LLMutex *getCurlMutex()
if (!sHandleMutexp)
{
sHandleMutexp = new LLMutex(NULL);
sHandleMutexp = new LLMutex();
}
return sHandleMutexp;
@ -389,7 +389,7 @@ void initialize()
S32 mutex_count = CRYPTO_num_locks();
for (S32 i = 0; i < mutex_count; i++)
{
sSSLMutex.push_back(LLMutex_ptr(new LLMutex(NULL)));
sSSLMutex.push_back(LLMutex_ptr(new LLMutex()));
}
CRYPTO_set_id_callback(&ssl_thread_id);
CRYPTO_set_locking_callback(&ssl_locking_callback);

View File

@ -595,7 +595,7 @@ void LLImage::initClass(bool use_new_byte_range, S32 minimal_reverse_byte_range_
{
sUseNewByteRange = use_new_byte_range;
sMinimalReverseByteRangePercent = minimal_reverse_byte_range_percent;
sMutex = new LLMutex(NULL);
sMutex = new LLMutex();
LLImageBase::createPrivatePool() ;
}

View File

@ -35,7 +35,7 @@
LLImageDecodeThread::LLImageDecodeThread(bool threaded)
: LLQueuedThread("imagedecode", threaded)
{
mCreationMutex = new LLMutex(getAPRPool());
mCreationMutex = new LLMutex();
}
//virtual

View File

@ -214,7 +214,7 @@ void LLVolumeMgr::useMutex()
{
if (!mDataMutex)
{
mDataMutex = new LLMutex(gAPRPoolp);
mDataMutex = new LLMutex();
}
}

View File

@ -265,7 +265,7 @@ void LLBufferArray::setThreaded(bool threaded)
{
if(!mMutexp)
{
mMutexp = new LLMutex(NULL);
mMutexp = new LLMutex();
}
}
else

View File

@ -48,7 +48,6 @@ static void tcp_close_channel(LLSocket::ptr_t* handle_ptr); // Close an open TCP
LLProxy::LLProxy():
mHTTPProxyEnabled(false),
mProxyMutex(NULL),
mUDPProxy(),
mTCPProxy(),
mHTTPProxy(),

View File

@ -394,8 +394,8 @@ protected:
S32 mCurrentPoolReallocCount;
#if LL_THREADS_APR
apr_thread_mutex_t* mChainsMutex;
apr_thread_mutex_t* mCallbackMutex;
std::mutex* mChainsMutex;
std::mutex* mCallbackMutex;
#else
int* mChainsMutex;
int* mCallbackMutex;

View File

@ -92,8 +92,6 @@ void LLPluginMessagePipeOwner::killMessagePipe(void)
}
LLPluginMessagePipe::LLPluginMessagePipe(LLPluginMessagePipeOwner *owner, LLSocket::ptr_t socket):
mInputMutex(gAPRPoolp),
mOutputMutex(gAPRPoolp),
mOutputStartIndex(0),
mOwner(owner),
mSocket(socket)

View File

@ -79,12 +79,11 @@ protected:
};
LLPluginProcessParent::LLPluginProcessParent(LLPluginProcessParentOwner *owner):
mIncomingQueueMutex(gAPRPoolp)
LLPluginProcessParent::LLPluginProcessParent(LLPluginProcessParentOwner *owner)
{
if(!sInstancesMutex)
{
sInstancesMutex = new LLMutex(gAPRPoolp);
sInstancesMutex = new LLMutex();
}
mOwner = owner;

View File

@ -234,7 +234,7 @@ LLVFS::LLVFS(const std::string& index_filename, const std::string& data_filename
mDataFP(NULL),
mIndexFP(NULL)
{
mDataMutex = new LLMutex(0);
mDataMutex = new LLMutex();
S32 i;
for (i = 0; i < VFSLOCK_COUNT; i++)

View File

@ -2152,7 +2152,7 @@ bool LLAppViewer::initThreads()
if (LLTrace::BlockTimer::sLog || LLTrace::BlockTimer::sMetricLog)
{
LLTrace::BlockTimer::setLogLock(new LLMutex(NULL));
LLTrace::BlockTimer::setLogLock(new LLMutex());
mFastTimerLogThread = new LLFastTimerLogThread(LLTrace::BlockTimer::sLogName);
mFastTimerLogThread->start();
}

View File

@ -46,7 +46,6 @@ LLFloaterConversationPreview::LLFloaterConversationPreview(const LLSD& session_i
mPageSize(gSavedSettings.getS32("ConversationHistoryPageSize")),
mAccountName(session_id[LL_FCP_ACCOUNT_NAME]),
mCompleteName(session_id[LL_FCP_COMPLETE_NAME]),
mMutex(NULL),
mShowHistory(false),
mMessages(NULL),
mHistoryThreadsBusy(false),

View File

@ -258,7 +258,7 @@ mCalculateBtn(NULL)
sInstance = this;
mLastMouseX = 0;
mLastMouseY = 0;
mStatusLock = new LLMutex(NULL);
mStatusLock = new LLMutex();
mModelPreview = NULL;
mLODMode[LLModel::LOD_HIGH] = 0;
@ -1195,7 +1195,7 @@ void LLFloaterModelPreview::onMouseCaptureLostModelPreview(LLMouseHandler* handl
//-----------------------------------------------------------------------------
LLModelPreview::LLModelPreview(S32 width, S32 height, LLFloater* fmp)
: LLViewerDynamicTexture(width, height, 3, ORDER_MIDDLE, FALSE), LLMutex(NULL)
: LLViewerDynamicTexture(width, height, 3, ORDER_MIDDLE, FALSE)
, mLodsQuery()
, mLodsWithParsingError()
, mPelvisZOffset( 0.0f )

View File

@ -516,7 +516,7 @@ LLMutex* LLLogChat::historyThreadsMutex()
{
if (sHistoryThreadsMutex == NULL)
{
sHistoryThreadsMutex = new LLMutex(NULL);
sHistoryThreadsMutex = new LLMutex();
}
return sHistoryThreadsMutex;
}
@ -1012,8 +1012,6 @@ void LLDeleteHistoryThread::run()
LLActionThread::LLActionThread(const std::string& name)
: LLThread(name),
mMutex(NULL),
mRunCondition(NULL),
mFinished(false)
{
}

View File

@ -809,9 +809,9 @@ LLMeshRepoThread::LLMeshRepoThread()
{
LLAppCoreHttp & app_core_http(LLAppViewer::instance()->getAppCoreHttp());
mMutex = new LLMutex(NULL);
mHeaderMutex = new LLMutex(NULL);
mSignal = new LLCondition(NULL);
mMutex = new LLMutex();
mHeaderMutex = new LLMutex();
mSignal = new LLCondition();
mHttpRequest = new LLCore::HttpRequest;
mHttpOptions = LLCore::HttpOptions::ptr_t(new LLCore::HttpOptions);
mHttpOptions->setTransferTimeout(SMALL_MESH_XFER_TIMEOUT);
@ -1914,7 +1914,7 @@ LLMeshUploadThread::LLMeshUploadThread(LLMeshUploadThread::instance_list& data,
mUploadSkin = upload_skin;
mUploadJoints = upload_joints;
mLockScaleIfJointPosition = lock_scale_if_joint_position;
mMutex = new LLMutex(NULL);
mMutex = new LLMutex();
mPendingUploads = 0;
mFinished = false;
mOrigin = gAgent.getPositionAgent();
@ -3308,7 +3308,7 @@ LLMeshRepository::LLMeshRepository()
void LLMeshRepository::init()
{
mMeshMutex = new LLMutex(NULL);
mMeshMutex = new LLMutex();
LLConvexDecomposition::getInstance()->initSystem();
@ -4189,8 +4189,8 @@ LLPhysicsDecomp::LLPhysicsDecomp()
mQuitting = false;
mDone = false;
mSignal = new LLCondition(NULL);
mMutex = new LLMutex(NULL);
mSignal = new LLCondition();
mMutex = new LLMutex();
}
LLPhysicsDecomp::~LLPhysicsDecomp()

View File

@ -825,10 +825,6 @@ void LLTextureCacheWorker::endWork(S32 param, bool aborted)
LLTextureCache::LLTextureCache(bool threaded)
: LLWorkerThread("TextureCache", threaded),
mWorkersMutex(NULL),
mHeaderMutex(NULL),
mListMutex(NULL),
mFastCacheMutex(NULL),
mHeaderAPRFile(NULL),
mReadOnly(TRUE), //do not allow to change the texture cache until setReadOnly() is called.
mTexturesSizeTotal(0),

View File

@ -925,7 +925,6 @@ LLTextureFetchWorker::LLTextureFetchWorker(LLTextureFetch* fetcher,
mCanUseHTTP(true),
mRetryAttempt(0),
mActiveCount(0),
mWorkMutex(NULL),
mFirstPacket(0),
mLastPacket(-1),
mTotalPackets(0),
@ -2543,8 +2542,6 @@ LLTextureFetch::LLTextureFetch(LLTextureCache* cache, LLImageDecodeThread* image
mDebugPause(FALSE),
mPacketCount(0),
mBadPacketCount(0),
mQueueMutex(getAPRPool()),
mNetworkQueueMutex(getAPRPool()),
mTextureCache(cache),
mImageDecodeThread(imagedecodethread),
mTextureBandwidth(0),

View File

@ -157,7 +157,7 @@ void LLFilePickerThread::run()
//static
void LLFilePickerThread::initClass()
{
sMutex = new LLMutex(NULL);
sMutex = new LLMutex();
}
//static

View File

@ -185,7 +185,7 @@ void LLWatchdog::init(killer_event_callback func)
mKillerCallback = func;
if(!mSuspectsAccessMutex && !mTimer)
{
mSuspectsAccessMutex = new LLMutex(NULL);
mSuspectsAccessMutex = new LLMutex();
mTimer = new LLWatchdogTimerThread();
mTimer->setSleepTime(WATCHDOG_SLEEP_TIME_USEC / 1000);
mLastClockCount = LLTimer::getTotalTime();