diff options
Diffstat (limited to 'indra/llcommon/llmutex.h')
-rw-r--r-- | indra/llcommon/llmutex.h | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/indra/llcommon/llmutex.h b/indra/llcommon/llmutex.h index db5b9357d9..3659a319a2 100644 --- a/indra/llcommon/llmutex.h +++ b/indra/llcommon/llmutex.h @@ -48,18 +48,19 @@ public: LLMutex(apr_pool_t *apr_poolp = NULL); // NULL pool constructs a new pool for the mutex virtual ~LLMutex(); - void lock(); // blocks - void unlock(); - bool isLocked(); // non-blocking, but does do a lock/unlock so not free - bool isSelfLocked(); //return true if locked in a same thread + void lock(); // blocks + bool trylock(); // non-blocking, returns true if lock held. + void unlock(); // undefined behavior when called on mutex not being held + bool isLocked(); // non-blocking, but does do a lock/unlock so not free + bool isSelfLocked(); //return true if locked in a same thread U32 lockingThread() const; //get ID of locking thread protected: - apr_thread_mutex_t* mAPRMutexp; + apr_thread_mutex_t *mAPRMutexp; mutable U32 mCount; mutable U32 mLockingThread; - apr_pool_t* mAPRPoolp; + apr_pool_t *mAPRPoolp; BOOL mIsLocalPool; #if MUTEX_DEBUG @@ -101,4 +102,40 @@ private: LLMutex* mMutex; }; +//============================================================================ + +// Scoped locking class similar in function to LLMutexLock but uses +// the trylock() method to conditionally acquire lock without +// blocking. Caller resolves the resulting condition by calling +// the isLocked() method and either punts or continues as indicated. +// +// Mostly of interest to callers needing to avoid stalls who can +// guarantee another attempt at a later time. + +class LLMutexTrylock +{ +public: + LLMutexTrylock(LLMutex* mutex) + : mMutex(mutex), + mLocked(false) + { + if (mMutex) + mLocked = mMutex->trylock(); + } + + ~LLMutexTrylock() + { + if (mMutex && mLocked) + mMutex->unlock(); + } + + bool isLocked() const + { + return mLocked; + } + +private: + LLMutex* mMutex; + bool mLocked; +}; #endif // LL_LLTHREAD_H |