BsThreadPool.cpp 5.5 KB

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