threadfac.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define THREADFAC_CODE
  19. #include "threadfac.h"
  20. int Runnable::ThreadCount_ = 0;
  21. CritSec Runnable::CritSec_; // to protect ThreadCount_
  22. // MDC: Made all this dependent on _REENTRANT being defined so VC++ doesn't complain on
  23. // single-threaded programs...
  24. //
  25. // Note: I chose the following type signature for thread functions
  26. // void function(void *data);
  27. //
  28. //
  29. // Since Win32 & POSIX have different type signatures for the thread entry points
  30. // an intermediate system-dependent function in this file gets called first.
  31. // That function then calls the system independent version. So the system dependent
  32. // version needs 2 Items 1) The address of the _real_ thread func 2) the data
  33. // to pass. We only have 1 argument available, so you figure out the rest...
  34. //
  35. // This is for internal use only
  36. //
  37. struct ThreadInformation
  38. {
  39. void *startPoint; // The address of the _real_ thread function, or class
  40. void *data; // data to pass to real thread function or class
  41. bit8 destroy; // only applies to classes, should delete after execution?
  42. };
  43. //
  44. // Start a thread inside a class
  45. //
  46. bit8 ThreadFactory::startThread(Runnable &runable, void *data, bit8 destroy)
  47. {
  48. #ifdef _REENTRANT
  49. {
  50. Runnable::CritSec_.lock();
  51. Runnable::ThreadCount_++;
  52. Runnable::CritSec_.unlock();
  53. }
  54. ThreadInformation *tInfo=new ThreadInformation;
  55. tInfo->startPoint=(void *)&runable;
  56. tInfo->data=data;
  57. tInfo->destroy=destroy;
  58. #ifdef _WIN32
  59. // Under windows call _beginthreadex instead of CreateThread so you can
  60. // use all the normal C library stuff. (IMPORTANT!!!)
  61. uint32 handle;
  62. uint32 stup1d;
  63. handle=_beginthreadex(NULL,0, threadClassLauncher, tInfo, 0, &stup1d);
  64. if (handle!=NULL)
  65. return(TRUE);
  66. else
  67. {
  68. {
  69. runable.CritSec_.lock();
  70. runable.ThreadCount_--; // Ok, so it didn't really start
  71. runable.CritSec_.unlock();
  72. }
  73. return(FALSE);
  74. }
  75. #else // UNIX
  76. // Setup thread attributes for client threads
  77. int retval;
  78. pthread_attr_t threadAttr;
  79. pthread_attr_init(&threadAttr);
  80. pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);
  81. pthread_attr_setscope(&threadAttr,PTHREAD_SCOPE_SYSTEM);
  82. retval=pthread_create(NULL,&threadAttr, threadClassLauncher, tInfo);
  83. if (retval==0)
  84. return(TRUE);
  85. else
  86. {
  87. {
  88. runable.CritSec_.lock();
  89. runable.ThreadCount_--; // Ok, so it didn't really start
  90. runable.CritSec_.unlock();
  91. }
  92. return(FALSE);
  93. }
  94. #endif
  95. #else
  96. return (FALSE);
  97. #endif /* _REENTRANT */
  98. }
  99. //
  100. // Start a thread inside a function
  101. //
  102. bit8 ThreadFactory::startThread(void (*start_func)(void *), void *data)
  103. {
  104. #ifdef _REENTRANT
  105. ThreadInformation *tInfo=new ThreadInformation;
  106. tInfo->startPoint=start_func;
  107. tInfo->data=data;
  108. #ifdef _WIN32
  109. // Under windows call _beginthreadex instead of CreateThread so you can
  110. // use all the normal C library stuff. (IMPORTANT!!!)
  111. uint32 handle;
  112. unsigned temp;
  113. handle=_beginthreadex(NULL,0, threadFuncLauncher, tInfo, 0, &temp);
  114. if (handle!=NULL)
  115. return(TRUE);
  116. return(FALSE);
  117. #else // UNIX
  118. // Setup thread attributes for client threads
  119. int retval;
  120. pthread_attr_t threadAttr;
  121. pthread_attr_init(&threadAttr);
  122. pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);
  123. pthread_attr_setscope(&threadAttr,PTHREAD_SCOPE_SYSTEM);
  124. retval=pthread_create(NULL,&threadAttr, threadFuncLauncher, tInfo);
  125. if (retval==0)
  126. return(TRUE);
  127. else
  128. return(FALSE);
  129. #endif
  130. #else
  131. return(FALSE);
  132. #endif /* REENTRANT */
  133. }
  134. #ifdef _WIN32
  135. unsigned __stdcall threadFuncLauncher(void *temp)
  136. #else // UNIX
  137. void *threadFuncLauncher(void *temp)
  138. #endif
  139. {
  140. ThreadInformation *tInfo=(ThreadInformation *)temp;
  141. void (*start_func)(void *);
  142. start_func=(void (*)(void *))tInfo->startPoint;
  143. void *data=tInfo->data;
  144. delete(tInfo);
  145. start_func(data);
  146. return(0);
  147. }
  148. #ifdef _WIN32
  149. unsigned __stdcall threadClassLauncher(void *temp)
  150. #else // UNIX
  151. void *threadClassLauncher(void *temp)
  152. #endif
  153. {
  154. ThreadInformation *tInfo=(ThreadInformation *)temp;
  155. Runnable *thrClass=(Runnable *)tInfo->startPoint;
  156. void *data=tInfo->data;
  157. bit8 destroy=tInfo->destroy;
  158. delete(tInfo);
  159. thrClass->run(data);
  160. if (destroy) // May want to free memory after thread finishes
  161. delete(thrClass);
  162. {
  163. Runnable::CritSec_.lock();
  164. Runnable::ThreadCount_--;
  165. Runnable::CritSec_.unlock();
  166. }
  167. #ifdef _WIN32
  168. ExitThread(0); // is this really needed?
  169. #endif
  170. return(0);
  171. }
  172. Runnable::Runnable()
  173. { }
  174. Runnable::~Runnable()
  175. { }
  176. // Is there a thread running in this class
  177. bit8 Runnable::isRunning(void)
  178. {
  179. // Don't need to lock a simple assignment
  180. int temp=ThreadCount_;
  181. return((temp>0)?TRUE:FALSE);
  182. }
  183. // How many threads are running in this class
  184. int Runnable::getThreadCount(void)
  185. {
  186. // Don't need to lock a simple assignment
  187. int temp=ThreadCount_;
  188. return(temp);
  189. }
  190. #undef THREADFAC_CODE