BsThreadPool.cpp 5.4 KB

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