BsThreadPool.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "BsThreadPool.h"
  2. namespace BansheeEngine
  3. {
  4. HThread::HThread()
  5. :mPool(nullptr), mThreadId(0)
  6. { }
  7. HThread::HThread(ThreadPool* pool, UINT32 threadId)
  8. :mPool(pool), mThreadId(threadId)
  9. { }
  10. void HThread::blockUntilComplete()
  11. {
  12. PooledThread* parentThread = nullptr;
  13. {
  14. BS_LOCK_MUTEX(mPool->mMutex);
  15. for (auto& thread : mPool->mThreads)
  16. {
  17. if (thread->getId() == mThreadId)
  18. parentThread = thread;
  19. }
  20. }
  21. if (parentThread != nullptr)
  22. {
  23. BS_LOCK_MUTEX_NAMED(parentThread->mMutex, lock);
  24. if (parentThread->mId == mThreadId) // Check again in case it changed
  25. {
  26. while (!parentThread->mIdle)
  27. BS_THREAD_WAIT(parentThread->mWorkerEndedCond, parentThread->mMutex, lock);
  28. }
  29. }
  30. }
  31. PooledThread::PooledThread(const String& name)
  32. :mName(name), mIdle(true), mThreadStarted(false),
  33. mThreadReady(false), mIdleTime(0), mId(0)
  34. { }
  35. PooledThread::~PooledThread()
  36. { }
  37. void PooledThread::initialize()
  38. {
  39. BS_THREAD_CREATE(t, std::bind(&PooledThread::run, this));
  40. mThread = t;
  41. BS_LOCK_MUTEX_NAMED(mMutex, lock);
  42. while(!mThreadStarted)
  43. BS_THREAD_WAIT(mStartedCond, mMutex, lock);
  44. }
  45. void PooledThread::start(std::function<void()> workerMethod)
  46. {
  47. {
  48. BS_LOCK_MUTEX(mMutex);
  49. mWorkerMethod = workerMethod;
  50. mIdle = false;
  51. mIdleTime = std::time(nullptr);
  52. mThreadReady = true;
  53. mId++;
  54. }
  55. BS_THREAD_NOTIFY_ONE(mReadyCond);
  56. }
  57. void PooledThread::run()
  58. {
  59. onThreadStarted(mName);
  60. {
  61. BS_LOCK_MUTEX(mMutex);
  62. mThreadStarted = true;
  63. }
  64. BS_THREAD_NOTIFY_ONE(mStartedCond);
  65. while(true)
  66. {
  67. std::function<void()> worker = nullptr;
  68. {
  69. BS_LOCK_MUTEX_NAMED(mMutex, lock);
  70. while(!mThreadReady)
  71. BS_THREAD_WAIT(mReadyCond, mMutex, lock);
  72. if(mWorkerMethod == nullptr)
  73. {
  74. onThreadEnded(mName);
  75. return;
  76. }
  77. worker = mWorkerMethod;
  78. }
  79. worker();
  80. {
  81. BS_LOCK_MUTEX(mMutex);
  82. mIdle = true;
  83. mIdleTime = std::time(nullptr);
  84. mThreadReady = false;
  85. BS_THREAD_NOTIFY_ONE(mWorkerEndedCond);
  86. }
  87. }
  88. }
  89. void PooledThread::destroy()
  90. {
  91. {
  92. BS_LOCK_MUTEX(mMutex);
  93. mWorkerMethod = nullptr;
  94. mThreadReady = true;
  95. }
  96. BS_THREAD_NOTIFY_ONE(mReadyCond);
  97. BS_THREAD_JOIN((*mThread));
  98. BS_THREAD_DESTROY(mThread);
  99. }
  100. bool PooledThread::isIdle()
  101. {
  102. BS_LOCK_MUTEX(mMutex);
  103. return mIdle;
  104. }
  105. time_t PooledThread::idleTime()
  106. {
  107. BS_LOCK_MUTEX(mMutex);
  108. return (time(nullptr) - mIdleTime);
  109. }
  110. void PooledThread::setName(const String& name)
  111. {
  112. mName = name;
  113. }
  114. UINT32 PooledThread::getId() const
  115. {
  116. BS_LOCK_MUTEX(mMutex);
  117. return mId;
  118. }
  119. ThreadPool::ThreadPool(UINT32 threadCapacity, UINT32 maxCapacity, UINT32 idleTimeout)
  120. :mDefaultCapacity(threadCapacity), mMaxCapacity(maxCapacity), mIdleTimeout(idleTimeout), mAge(0)
  121. {
  122. }
  123. ThreadPool::~ThreadPool()
  124. {
  125. stopAll();
  126. }
  127. HThread ThreadPool::run(const String& name, std::function<void()> workerMethod)
  128. {
  129. PooledThread* thread = getThread(name);
  130. thread->start(workerMethod);
  131. return HThread(this, thread->getId());
  132. }
  133. void ThreadPool::stopAll()
  134. {
  135. BS_LOCK_MUTEX(mMutex);
  136. for(auto& thread : mThreads)
  137. {
  138. destroyThread(thread);
  139. }
  140. mThreads.clear();
  141. }
  142. void ThreadPool::clearUnused()
  143. {
  144. BS_LOCK_MUTEX(mMutex);
  145. mAge = 0;
  146. if(mThreads.size() <= mDefaultCapacity)
  147. return;
  148. Vector<PooledThread*> idleThreads;
  149. Vector<PooledThread*> expiredThreads;
  150. Vector<PooledThread*> activeThreads;
  151. idleThreads.reserve(mThreads.size());
  152. expiredThreads.reserve(mThreads.size());
  153. activeThreads.reserve(mThreads.size());
  154. for(auto& thread : mThreads)
  155. {
  156. if(thread->isIdle())
  157. {
  158. if(thread->idleTime() >= mIdleTimeout)
  159. expiredThreads.push_back(thread);
  160. else
  161. idleThreads.push_back(thread);
  162. }
  163. else
  164. activeThreads.push_back(thread);
  165. }
  166. idleThreads.insert(idleThreads.end(), expiredThreads.begin(), expiredThreads.end());
  167. UINT32 limit = std::min((UINT32)idleThreads.size(), mDefaultCapacity);
  168. UINT32 i = 0;
  169. mThreads.clear();
  170. for(auto& thread : idleThreads)
  171. {
  172. if(i < limit)
  173. mThreads.push_back(thread);
  174. else
  175. destroyThread(thread);
  176. }
  177. mThreads.insert(mThreads.end(), activeThreads.begin(), activeThreads.end());
  178. }
  179. void ThreadPool::destroyThread(PooledThread* thread)
  180. {
  181. thread->destroy();
  182. bs_delete(thread);
  183. }
  184. PooledThread* ThreadPool::getThread(const String& name)
  185. {
  186. UINT32 age = 0;
  187. {
  188. BS_LOCK_MUTEX(mMutex);
  189. age = ++mAge;
  190. }
  191. if(age == 32)
  192. clearUnused();
  193. PooledThread* newThread = nullptr;
  194. BS_LOCK_MUTEX(mMutex);
  195. for(auto& thread : mThreads)
  196. {
  197. if(thread->isIdle())
  198. {
  199. thread->setName(name);
  200. return thread;
  201. }
  202. }
  203. if(newThread == nullptr)
  204. {
  205. if(mThreads.size() >= mMaxCapacity)
  206. BS_EXCEPT(InvalidStateException, "Unable to create a new thread in the pool because maximum capacity has been reached.");
  207. newThread = createThread(name);
  208. mThreads.push_back(newThread);
  209. }
  210. return newThread;
  211. }
  212. UINT32 ThreadPool::getNumAvailable() const
  213. {
  214. UINT32 numAvailable = 0;
  215. BS_LOCK_MUTEX(mMutex);
  216. for(auto& thread : mThreads)
  217. {
  218. if(thread->isIdle())
  219. numAvailable++;
  220. }
  221. return numAvailable;
  222. }
  223. UINT32 ThreadPool::getNumActive() const
  224. {
  225. UINT32 numActive = 0;
  226. BS_LOCK_MUTEX(mMutex);
  227. for(auto& thread : mThreads)
  228. {
  229. if(!thread->isIdle())
  230. numActive++;
  231. }
  232. return numActive;
  233. }
  234. UINT32 ThreadPool::getNumAllocated() const
  235. {
  236. BS_LOCK_MUTEX(mMutex);
  237. return (UINT32)mThreads.size();
  238. }
  239. }