threadfac.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. //
  19. // Platform independent thread creation (Win32 & POSIX)
  20. //
  21. #ifndef THREADFAC_HEADER
  22. #define THREADFAC_HEADER
  23. #ifdef _WIN32
  24. #include <process.h>
  25. #endif
  26. #include "wstypes.h"
  27. #include <stdlib.h>
  28. #ifdef _WIN32
  29. #include <wtypes.h>
  30. #else // UNIX
  31. #include <pthread.h>
  32. #endif
  33. // Windows headers have a tendency to redefine IN
  34. #ifdef IN
  35. #undef IN
  36. #endif
  37. #define IN const
  38. #include "critsec.h"
  39. #ifdef THREADFAC_CODE
  40. // This is the fake thread entry point for functions
  41. #ifdef _WIN32
  42. static unsigned __stdcall threadFuncLauncher(void *temp);
  43. #else // UNIX
  44. static void *threadFuncLauncher(void *temp);
  45. #endif
  46. // Fake entry point for classes
  47. #ifdef _WIN32
  48. static unsigned __stdcall threadClassLauncher(void *temp);
  49. #else // UNIX
  50. static void *threadClassLauncher(void *temp);
  51. #endif
  52. #endif
  53. // Forward definition of base class for threaded classes
  54. class Runnable;
  55. //
  56. // Call the static method startThread to begin a new thread.
  57. //
  58. class ThreadFactory
  59. {
  60. public:
  61. static bit8 startThread(void (*start_func)(void *), void *data);
  62. static bit8 startThread(Runnable &runable, void *data, bit8 destroy=FALSE);
  63. };
  64. //
  65. // Base class for when you want a thread to execute inside a class
  66. // instead of a function.
  67. //
  68. class Runnable
  69. {
  70. public:
  71. Runnable();
  72. virtual ~Runnable();
  73. // ThreadFactory needs to be able to access the private
  74. // IsRunning_ field.
  75. friend class ThreadFactory;
  76. // So do the threadClassLaunchers
  77. #ifdef _WIN32
  78. friend static unsigned __stdcall threadClassLauncher(void *temp);
  79. #else // UNIX
  80. friend void *threadClassLauncher(void *temp);
  81. #endif
  82. virtual void run(void *data)=0; // Thread entry point
  83. void startThread(void *data,bit8 destroy=FALSE) // nice way to start a thread
  84. {
  85. ThreadFactory::startThread(*this,data,destroy);
  86. };
  87. // Is there a thread running in this class?
  88. static bit8 isRunning(void);
  89. // Get the count of threads running inside this class
  90. static int getThreadCount();
  91. private:
  92. static int ThreadCount_;
  93. static CritSec CritSec_; // to protect ThreadCount_
  94. };
  95. #endif