Thread.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "precompiled.h"
  22. /*
  23. ================================================================================================
  24. Contains the vartious ThreadingClass implementations.
  25. ================================================================================================
  26. */
  27. /*
  28. ================================================================================================
  29. idSysThread
  30. ================================================================================================
  31. */
  32. /*
  33. ========================
  34. idSysThread::idSysThread
  35. ========================
  36. */
  37. idSysThread::idSysThread() :
  38. threadHandle( 0 ),
  39. isWorker( false ),
  40. isRunning( false ),
  41. isTerminating( false ),
  42. moreWorkToDo( false ),
  43. signalWorkerDone( true ) {
  44. }
  45. /*
  46. ========================
  47. idSysThread::~idSysThread
  48. ========================
  49. */
  50. idSysThread::~idSysThread() {
  51. StopThread( true );
  52. if ( threadHandle ) {
  53. Sys_DestroyThread( threadHandle );
  54. }
  55. }
  56. /*
  57. ========================
  58. idSysThread::StartThread
  59. ========================
  60. */
  61. bool idSysThread::StartThread( const char * name_, core_t core, xthreadPriority priority, int stackSize ) {
  62. if ( isRunning ) {
  63. return false;
  64. }
  65. name = name_;
  66. isTerminating = false;
  67. if ( threadHandle ) {
  68. Sys_DestroyThread( threadHandle );
  69. }
  70. threadHandle = Sys_CreateThread( (xthread_t)ThreadProc, this, priority, name, core, stackSize, false );
  71. isRunning = true;
  72. return true;
  73. }
  74. /*
  75. ========================
  76. idSysThread::StartWorkerThread
  77. ========================
  78. */
  79. bool idSysThread::StartWorkerThread( const char * name_, core_t core, xthreadPriority priority, int stackSize ) {
  80. if ( isRunning ) {
  81. return false;
  82. }
  83. isWorker = true;
  84. bool result = StartThread( name_, core, priority, stackSize );
  85. signalWorkerDone.Wait( idSysSignal::WAIT_INFINITE );
  86. return result;
  87. }
  88. /*
  89. ========================
  90. idSysThread::StopThread
  91. ========================
  92. */
  93. void idSysThread::StopThread( bool wait ) {
  94. if ( !isRunning ) {
  95. return;
  96. }
  97. if ( isWorker ) {
  98. signalMutex.Lock();
  99. moreWorkToDo = true;
  100. signalWorkerDone.Clear();
  101. isTerminating = true;
  102. signalMoreWorkToDo.Raise();
  103. signalMutex.Unlock();
  104. } else {
  105. isTerminating = true;
  106. }
  107. if ( wait ) {
  108. WaitForThread();
  109. }
  110. }
  111. /*
  112. ========================
  113. idSysThread::WaitForThread
  114. ========================
  115. */
  116. void idSysThread::WaitForThread() {
  117. if ( isWorker ) {
  118. signalWorkerDone.Wait( idSysSignal::WAIT_INFINITE );
  119. } else if ( isRunning ) {
  120. Sys_DestroyThread( threadHandle );
  121. threadHandle = 0;
  122. }
  123. }
  124. /*
  125. ========================
  126. idSysThread::SignalWork
  127. ========================
  128. */
  129. void idSysThread::SignalWork() {
  130. if ( isWorker ) {
  131. signalMutex.Lock();
  132. moreWorkToDo = true;
  133. signalWorkerDone.Clear();
  134. signalMoreWorkToDo.Raise();
  135. signalMutex.Unlock();
  136. }
  137. }
  138. /*
  139. ========================
  140. idSysThread::IsWorkDone
  141. ========================
  142. */
  143. bool idSysThread::IsWorkDone() {
  144. if ( isWorker ) {
  145. // a timeout of 0 will return immediately with true if signaled
  146. if ( signalWorkerDone.Wait( 0 ) ) {
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. /*
  153. ========================
  154. idSysThread::ThreadProc
  155. ========================
  156. */
  157. int idSysThread::ThreadProc( idSysThread * thread ) {
  158. int retVal = 0;
  159. try {
  160. if ( thread->isWorker ) {
  161. for( ; ; ) {
  162. thread->signalMutex.Lock();
  163. if ( thread->moreWorkToDo ) {
  164. thread->moreWorkToDo = false;
  165. thread->signalMoreWorkToDo.Clear();
  166. thread->signalMutex.Unlock();
  167. } else {
  168. thread->signalWorkerDone.Raise();
  169. thread->signalMutex.Unlock();
  170. thread->signalMoreWorkToDo.Wait( idSysSignal::WAIT_INFINITE );
  171. continue;
  172. }
  173. if ( thread->isTerminating ) {
  174. break;
  175. }
  176. retVal = thread->Run();
  177. }
  178. thread->signalWorkerDone.Raise();
  179. } else {
  180. retVal = thread->Run();
  181. }
  182. } catch ( idException & ex ) {
  183. idLib::Warning( "Fatal error in thread %s: %s", thread->GetName(), ex.GetError() );
  184. // We don't handle threads terminating unexpectedly very well, so just terminate the whole process
  185. _exit( 0 );
  186. }
  187. thread->isRunning = false;
  188. return retVal;
  189. }
  190. /*
  191. ========================
  192. idSysThread::Run
  193. ========================
  194. */
  195. int idSysThread::Run() {
  196. // The Run() is not pure virtual because on destruction of a derived class
  197. // the virtual function pointer will be set to NULL before the idSysThread
  198. // destructor actually stops the thread.
  199. return 0;
  200. }
  201. /*
  202. ================================================================================================
  203. test
  204. ================================================================================================
  205. */
  206. /*
  207. ================================================
  208. idMyThread test class.
  209. ================================================
  210. */
  211. class idMyThread : public idSysThread {
  212. public:
  213. virtual int Run() {
  214. // run threaded code here
  215. return 0;
  216. }
  217. // specify thread data here
  218. };
  219. /*
  220. ========================
  221. TestThread
  222. ========================
  223. */
  224. void TestThread() {
  225. idMyThread thread;
  226. thread.StartThread( "myThread", CORE_ANY );
  227. }
  228. /*
  229. ========================
  230. TestWorkers
  231. ========================
  232. */
  233. void TestWorkers() {
  234. idSysWorkerThreadGroup<idMyThread> workers( "myWorkers", 4 );
  235. for ( ; ; ) {
  236. for ( int i = 0; i < workers.GetNumThreads(); i++ ) {
  237. // workers.GetThread( i )-> // setup work for this thread
  238. }
  239. workers.SignalWorkAndWait();
  240. }
  241. }