| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #include "BsThreadPool.h"
- namespace BansheeEngine
- {
- HThread::HThread()
- :mPool(nullptr), mThreadId(0)
- { }
- HThread::HThread(ThreadPool* pool, UINT32 threadId)
- :mPool(pool), mThreadId(threadId)
- { }
- void HThread::blockUntilComplete()
- {
- PooledThread* parentThread = nullptr;
- {
- BS_LOCK_MUTEX(mPool->mMutex);
- for (auto& thread : mPool->mThreads)
- {
- if (thread->getId() == mThreadId)
- parentThread = thread;
- }
- }
- if (parentThread != nullptr)
- {
- BS_LOCK_MUTEX_NAMED(parentThread->mMutex, lock);
- if (parentThread->mId == mThreadId) // Check again in case it changed
- {
- while (!parentThread->mIdle)
- BS_THREAD_WAIT(parentThread->mWorkerEndedCond, parentThread->mMutex, lock);
- }
- }
- }
- PooledThread::PooledThread(const String& name)
- :mName(name), mIdle(true), mThreadStarted(false),
- mThreadReady(false), mIdleTime(0), mId(0)
- { }
- PooledThread::~PooledThread()
- { }
- void PooledThread::initialize()
- {
- BS_THREAD_CREATE(t, std::bind(&PooledThread::run, this));
- mThread = t;
- BS_LOCK_MUTEX_NAMED(mMutex, lock);
- while(!mThreadStarted)
- BS_THREAD_WAIT(mStartedCond, mMutex, lock);
- }
- void PooledThread::start(std::function<void()> workerMethod)
- {
- {
- BS_LOCK_MUTEX(mMutex);
- mWorkerMethod = workerMethod;
- mIdle = false;
- mIdleTime = std::time(nullptr);
- mThreadReady = true;
- mId++;
- }
- BS_THREAD_NOTIFY_ONE(mReadyCond);
- }
- void PooledThread::run()
- {
- onThreadStarted(mName);
- {
- BS_LOCK_MUTEX(mMutex);
- mThreadStarted = true;
- }
- BS_THREAD_NOTIFY_ONE(mStartedCond);
- while(true)
- {
- std::function<void()> worker = nullptr;
- {
- BS_LOCK_MUTEX_NAMED(mMutex, lock);
- while(!mThreadReady)
- BS_THREAD_WAIT(mReadyCond, mMutex, lock);
- if(mWorkerMethod == nullptr)
- {
- onThreadEnded(mName);
- return;
- }
- worker = mWorkerMethod;
- }
- worker();
- {
- BS_LOCK_MUTEX(mMutex);
- mIdle = true;
- mIdleTime = std::time(nullptr);
- mThreadReady = false;
- BS_THREAD_NOTIFY_ONE(mWorkerEndedCond);
- }
- }
- }
- void PooledThread::destroy()
- {
- {
- BS_LOCK_MUTEX(mMutex);
- mWorkerMethod = nullptr;
- mThreadReady = true;
- }
- BS_THREAD_NOTIFY_ONE(mReadyCond);
- BS_THREAD_JOIN((*mThread));
- BS_THREAD_DESTROY(mThread);
- }
- bool PooledThread::isIdle()
- {
- BS_LOCK_MUTEX(mMutex);
- return mIdle;
- }
- time_t PooledThread::idleTime()
- {
- BS_LOCK_MUTEX(mMutex);
- return (time(nullptr) - mIdleTime);
- }
- void PooledThread::setName(const String& name)
- {
- mName = name;
- }
- UINT32 PooledThread::getId() const
- {
- BS_LOCK_MUTEX(mMutex);
- return mId;
- }
- ThreadPool::ThreadPool(UINT32 threadCapacity, UINT32 maxCapacity, UINT32 idleTimeout)
- :mDefaultCapacity(threadCapacity), mMaxCapacity(maxCapacity), mIdleTimeout(idleTimeout), mAge(0)
- {
- }
- ThreadPool::~ThreadPool()
- {
- stopAll();
- }
- HThread ThreadPool::run(const String& name, std::function<void()> workerMethod)
- {
- PooledThread* thread = getThread(name);
- thread->start(workerMethod);
- return HThread(this, thread->getId());
- }
- void ThreadPool::stopAll()
- {
- BS_LOCK_MUTEX(mMutex);
- for(auto& thread : mThreads)
- {
- destroyThread(thread);
- }
- mThreads.clear();
- }
- void ThreadPool::clearUnused()
- {
- BS_LOCK_MUTEX(mMutex);
- mAge = 0;
- if(mThreads.size() <= mDefaultCapacity)
- return;
- Vector<PooledThread*> idleThreads;
- Vector<PooledThread*> expiredThreads;
- Vector<PooledThread*> activeThreads;
- idleThreads.reserve(mThreads.size());
- expiredThreads.reserve(mThreads.size());
- activeThreads.reserve(mThreads.size());
- for(auto& thread : mThreads)
- {
- if(thread->isIdle())
- {
- if(thread->idleTime() >= mIdleTimeout)
- expiredThreads.push_back(thread);
- else
- idleThreads.push_back(thread);
- }
- else
- activeThreads.push_back(thread);
- }
- idleThreads.insert(idleThreads.end(), expiredThreads.begin(), expiredThreads.end());
- UINT32 limit = std::min((UINT32)idleThreads.size(), mDefaultCapacity);
- UINT32 i = 0;
- mThreads.clear();
- for(auto& thread : idleThreads)
- {
- if(i < limit)
- mThreads.push_back(thread);
- else
- destroyThread(thread);
- }
- mThreads.insert(mThreads.end(), activeThreads.begin(), activeThreads.end());
- }
- void ThreadPool::destroyThread(PooledThread* thread)
- {
- thread->destroy();
- bs_delete(thread);
- }
- PooledThread* ThreadPool::getThread(const String& name)
- {
- UINT32 age = 0;
- {
- BS_LOCK_MUTEX(mMutex);
- age = ++mAge;
- }
- if(age == 32)
- clearUnused();
- PooledThread* newThread = nullptr;
- BS_LOCK_MUTEX(mMutex);
- for(auto& thread : mThreads)
- {
- if(thread->isIdle())
- {
- thread->setName(name);
- return thread;
- }
- }
- if(newThread == nullptr)
- {
- if(mThreads.size() >= mMaxCapacity)
- BS_EXCEPT(InvalidStateException, "Unable to create a new thread in the pool because maximum capacity has been reached.");
- newThread = createThread(name);
- mThreads.push_back(newThread);
- }
- return newThread;
- }
- UINT32 ThreadPool::getNumAvailable() const
- {
- UINT32 numAvailable = 0;
- BS_LOCK_MUTEX(mMutex);
- for(auto& thread : mThreads)
- {
- if(thread->isIdle())
- numAvailable++;
- }
- return numAvailable;
- }
- UINT32 ThreadPool::getNumActive() const
- {
- UINT32 numActive = 0;
- BS_LOCK_MUTEX(mMutex);
- for(auto& thread : mThreads)
- {
- if(!thread->isIdle())
- numActive++;
- }
- return numActive;
- }
- UINT32 ThreadPool::getNumAllocated() const
- {
- BS_LOCK_MUTEX(mMutex);
- return (UINT32)mThreads.size();
- }
- }
|