BsThreadPool.cpp 4.5 KB

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