BsThreadPool.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. {
  73. BS_LOCK_MUTEX_NAMED(mMutex, lock);
  74. while (!mThreadReady)
  75. BS_THREAD_WAIT(mReadyCond, mMutex, lock);
  76. worker = mWorkerMethod;
  77. }
  78. if (worker == nullptr)
  79. {
  80. onThreadEnded(mName);
  81. return;
  82. }
  83. }
  84. worker();
  85. {
  86. BS_LOCK_MUTEX(mMutex);
  87. mIdle = true;
  88. mIdleTime = std::time(nullptr);
  89. mThreadReady = false;
  90. mWorkerMethod = nullptr; // Make sure to clear as it could have bound shared pointers and similar
  91. BS_THREAD_NOTIFY_ONE(mWorkerEndedCond);
  92. }
  93. }
  94. }
  95. void PooledThread::destroy()
  96. {
  97. blockUntilComplete();
  98. {
  99. BS_LOCK_MUTEX(mMutex);
  100. mWorkerMethod = nullptr;
  101. mThreadReady = true;
  102. }
  103. BS_THREAD_NOTIFY_ONE(mReadyCond);
  104. BS_THREAD_JOIN((*mThread));
  105. BS_THREAD_DESTROY(mThread);
  106. }
  107. void PooledThread::blockUntilComplete()
  108. {
  109. BS_LOCK_MUTEX_NAMED(mMutex, lock);
  110. while (!mIdle)
  111. BS_THREAD_WAIT(mWorkerEndedCond, mMutex, lock);
  112. }
  113. bool PooledThread::isIdle()
  114. {
  115. BS_LOCK_MUTEX(mMutex);
  116. return mIdle;
  117. }
  118. time_t PooledThread::idleTime()
  119. {
  120. BS_LOCK_MUTEX(mMutex);
  121. return (time(nullptr) - mIdleTime);
  122. }
  123. void PooledThread::setName(const String& name)
  124. {
  125. mName = name;
  126. }
  127. UINT32 PooledThread::getId() const
  128. {
  129. BS_LOCK_MUTEX(mMutex);
  130. return mId;
  131. }
  132. ThreadPool::ThreadPool(UINT32 threadCapacity, UINT32 maxCapacity, UINT32 idleTimeout)
  133. :mDefaultCapacity(threadCapacity), mMaxCapacity(maxCapacity), mIdleTimeout(idleTimeout), mAge(0)
  134. {
  135. }
  136. ThreadPool::~ThreadPool()
  137. {
  138. stopAll();
  139. }
  140. HThread ThreadPool::run(const String& name, std::function<void()> workerMethod)
  141. {
  142. PooledThread* thread = getThread(name);
  143. thread->start(workerMethod, mUniqueId++);
  144. return HThread(this, thread->getId());
  145. }
  146. void ThreadPool::stopAll()
  147. {
  148. BS_LOCK_MUTEX(mMutex);
  149. for(auto& thread : mThreads)
  150. {
  151. destroyThread(thread);
  152. }
  153. mThreads.clear();
  154. }
  155. void ThreadPool::clearUnused()
  156. {
  157. BS_LOCK_MUTEX(mMutex);
  158. mAge = 0;
  159. if(mThreads.size() <= mDefaultCapacity)
  160. return;
  161. Vector<PooledThread*> idleThreads;
  162. Vector<PooledThread*> expiredThreads;
  163. Vector<PooledThread*> activeThreads;
  164. idleThreads.reserve(mThreads.size());
  165. expiredThreads.reserve(mThreads.size());
  166. activeThreads.reserve(mThreads.size());
  167. for(auto& thread : mThreads)
  168. {
  169. if(thread->isIdle())
  170. {
  171. if(thread->idleTime() >= mIdleTimeout)
  172. expiredThreads.push_back(thread);
  173. else
  174. idleThreads.push_back(thread);
  175. }
  176. else
  177. activeThreads.push_back(thread);
  178. }
  179. idleThreads.insert(idleThreads.end(), expiredThreads.begin(), expiredThreads.end());
  180. UINT32 limit = std::min((UINT32)idleThreads.size(), mDefaultCapacity);
  181. UINT32 i = 0;
  182. mThreads.clear();
  183. for(auto& thread : idleThreads)
  184. {
  185. if(i < limit)
  186. mThreads.push_back(thread);
  187. else
  188. destroyThread(thread);
  189. }
  190. mThreads.insert(mThreads.end(), activeThreads.begin(), activeThreads.end());
  191. }
  192. void ThreadPool::destroyThread(PooledThread* thread)
  193. {
  194. thread->destroy();
  195. bs_delete(thread);
  196. }
  197. PooledThread* ThreadPool::getThread(const String& name)
  198. {
  199. UINT32 age = 0;
  200. {
  201. BS_LOCK_MUTEX(mMutex);
  202. age = ++mAge;
  203. }
  204. if(age == 32)
  205. clearUnused();
  206. PooledThread* newThread = nullptr;
  207. BS_LOCK_MUTEX(mMutex);
  208. for(auto& thread : mThreads)
  209. {
  210. if(thread->isIdle())
  211. {
  212. thread->setName(name);
  213. return thread;
  214. }
  215. }
  216. if(newThread == nullptr)
  217. {
  218. if(mThreads.size() >= mMaxCapacity)
  219. BS_EXCEPT(InvalidStateException, "Unable to create a new thread in the pool because maximum capacity has been reached.");
  220. newThread = createThread(name);
  221. mThreads.push_back(newThread);
  222. }
  223. return newThread;
  224. }
  225. UINT32 ThreadPool::getNumAvailable() const
  226. {
  227. UINT32 numAvailable = 0;
  228. BS_LOCK_MUTEX(mMutex);
  229. for(auto& thread : mThreads)
  230. {
  231. if(thread->isIdle())
  232. numAvailable++;
  233. }
  234. return numAvailable;
  235. }
  236. UINT32 ThreadPool::getNumActive() const
  237. {
  238. UINT32 numActive = 0;
  239. BS_LOCK_MUTEX(mMutex);
  240. for(auto& thread : mThreads)
  241. {
  242. if(!thread->isIdle())
  243. numActive++;
  244. }
  245. return numActive;
  246. }
  247. UINT32 ThreadPool::getNumAllocated() const
  248. {
  249. BS_LOCK_MUTEX(mMutex);
  250. return (UINT32)mThreads.size();
  251. }
  252. }