summaryrefslogtreecommitdiff
path: root/indra/llcommon/llthread.h
diff options
context:
space:
mode:
authorDave Parks <davep@lindenlab.com>2011-10-14 11:52:40 -0500
committerDave Parks <davep@lindenlab.com>2011-10-14 11:52:40 -0500
commit4331c112aba074562e9a8826fe6d271a94f790f0 (patch)
treeaa6e1d387821c1ea5906a7aa9b3dd474ece431b7 /indra/llcommon/llthread.h
parentef490e308ccce8e6df85144784a0f4580f5ac6a1 (diff)
Backed out changeset b782a75c99e6
Diffstat (limited to 'indra/llcommon/llthread.h')
-rw-r--r--indra/llcommon/llthread.h124
1 files changed, 28 insertions, 96 deletions
diff --git a/indra/llcommon/llthread.h b/indra/llcommon/llthread.h
index 757832b8ca..f1c6cd75af 100644
--- a/indra/llcommon/llthread.h
+++ b/indra/llcommon/llthread.h
@@ -29,34 +29,12 @@
#include "llapp.h"
#include "llapr.h"
-#include "llmemory.h"
#include "apr_thread_cond.h"
-#include "llaprpool.h"
-
-#ifdef SHOW_ASSERT
-extern LL_COMMON_API bool is_main_thread(void);
-#endif
class LLThread;
class LLMutex;
class LLCondition;
-class LL_COMMON_API LLThreadLocalData
-{
-private:
- static apr_threadkey_t* sThreadLocalDataKey;
-
-public:
- // Thread-local memory pools.
- LLAPRRootPool mRootPool;
- LLVolatileAPRPool mVolatileAPRPool;
-
- static void init(void);
- static void destroy(void* thread_local_data);
- static void create(LLThread* pthread);
- static LLThreadLocalData& tldata(void);
-};
-
class LL_COMMON_API LLThread
{
public:
@@ -67,7 +45,7 @@ public:
QUITTING= 2 // Someone wants this thread to quit
} EThreadStatus;
- LLThread(std::string const& name);
+ LLThread(const std::string& name, apr_pool_t *poolp = NULL);
virtual ~LLThread(); // Warning! You almost NEVER want to destroy a thread unless it's in the STOPPED state.
virtual void shutdown(); // stops the thread
@@ -82,7 +60,7 @@ public:
// Called from MAIN THREAD.
void pause();
void unpause();
- bool isPaused() { return isStopped() || mPaused; }
+ bool isPaused() { return isStopped() || mPaused == TRUE; }
// Cause the thread to wake up and check its condition
void wake();
@@ -96,11 +74,11 @@ public:
// this kicks off the apr thread
void start(void);
- // Return thread-local data for the current thread.
- static LLThreadLocalData& tldata(void) { return LLThreadLocalData::tldata(); }
+ apr_pool_t *getAPRPool() { return mAPRPoolp; }
+ LLVolatileAPRPool* getLocalAPRFilePool() { return mLocalAPRFilePoolp ; }
private:
- bool mPaused;
+ BOOL mPaused;
// static function passed to APR thread creation routine
static void *APR_THREAD_FUNC staticRun(apr_thread_t *apr_threadp, void *datap);
@@ -110,10 +88,14 @@ protected:
LLCondition* mRunCondition;
apr_thread_t *mAPRThreadp;
+ apr_pool_t *mAPRPoolp;
+ BOOL mIsLocalPool;
EThreadStatus mStatus;
- friend void LLThreadLocalData::create(LLThread* threadp);
- LLThreadLocalData* mThreadLocalData;
+ //a local apr_pool for APRFile operations in this thread. If it exists, LLAPRFile::sAPRFilePoolp should not be used.
+ //Note: this pool is used by APRFile ONLY, do NOT use it for any other purposes.
+ // otherwise it will cause severe memory leaking!!! --bao
+ LLVolatileAPRPool *mLocalAPRFilePoolp ;
void setQuitting();
@@ -143,80 +125,30 @@ protected:
#define MUTEX_DEBUG (LL_DEBUG || LL_RELEASE_WITH_DEBUG_INFO)
-#ifdef MUTEX_DEBUG
-// We really shouldn't be using recursive locks. Make sure of that in debug mode.
-#define MUTEX_FLAG APR_THREAD_MUTEX_UNNESTED
-#else
-// Use the fastest platform-optimal lock behavior (can be recursive or non-recursive).
-#define MUTEX_FLAG APR_THREAD_MUTEX_DEFAULT
-#endif
-
-class LL_COMMON_API LLMutexBase
-{
-public:
- void lock() { apr_thread_mutex_lock(mAPRMutexp); }
- void unlock() { apr_thread_mutex_unlock(mAPRMutexp); }
- // Returns true if lock was obtained successfully.
- bool trylock() { return !APR_STATUS_IS_EBUSY(apr_thread_mutex_trylock(mAPRMutexp)); }
-
- // non-blocking, but does do a lock/unlock so not free
- bool isLocked() { bool is_not_locked = trylock(); if (is_not_locked) unlock(); return !is_not_locked; }
-
-protected:
- // mAPRMutexp is initialized and uninitialized in the derived class.
- apr_thread_mutex_t* mAPRMutexp;
-};
-
-class LL_COMMON_API LLMutex : public LLMutexBase
+class LL_COMMON_API LLMutex
{
public:
- LLMutex(LLAPRPool& parent = LLThread::tldata().mRootPool) : mPool(parent)
- {
- apr_thread_mutex_create(&mAPRMutexp, MUTEX_FLAG, mPool());
- }
- ~LLMutex()
- {
- llassert(!isLocked()); // better not be locked!
- apr_thread_mutex_destroy(mAPRMutexp);
- mAPRMutexp = NULL;
- }
-
+ LLMutex(apr_pool_t *apr_poolp); // 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
+
protected:
- LLAPRPool mPool;
-};
-
-#if APR_HAS_THREADS
-// No need to use a root pool in this case.
-typedef LLMutex LLMutexRootPool;
-#else // APR_HAS_THREADS
-class LL_COMMON_API LLMutexRootPool : public LLMutexBase
-{
-public:
- LLMutexRootPool(void)
- {
- apr_thread_mutex_create(&mAPRMutexp, MUTEX_FLAG, mRootPool());
- }
- ~LLMutexRootPool()
- {
-#if APR_POOL_DEBUG
- // It is allowed to destruct root pools from a different thread.
- mRootPool.grab_ownership();
+ apr_thread_mutex_t *mAPRMutexp;
+ apr_pool_t *mAPRPoolp;
+ BOOL mIsLocalPool;
+#if MUTEX_DEBUG
+ std::map<U32, BOOL> mIsLocked;
#endif
- llassert(!isLocked());
- apr_thread_mutex_destroy(mAPRMutexp);
- mAPRMutexp = NULL;
- }
-
-protected:
- LLAPRRootPool mRootPool;
};
-#endif // APR_HAS_THREADS
// Actually a condition/mutex pair (since each condition needs to be associated with a mutex).
class LL_COMMON_API LLCondition : public LLMutex
{
public:
- LLCondition(LLAPRPool& parent = LLThread::tldata().mRootPool);
+ LLCondition(apr_pool_t *apr_poolp); // Defaults to global pool, could use the thread pool as well.
~LLCondition();
void wait(); // blocks
@@ -227,10 +159,10 @@ protected:
apr_thread_cond_t *mAPRCondp;
};
-class LL_COMMON_API LLMutexLock
+class LLMutexLock
{
public:
- LLMutexLock(LLMutexBase* mutex)
+ LLMutexLock(LLMutex* mutex)
{
mMutex = mutex;
mMutex->lock();
@@ -240,7 +172,7 @@ public:
mMutex->unlock();
}
private:
- LLMutexBase* mMutex;
+ LLMutex* mMutex;
};
//============================================================================