thread.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef THREAD_H
  19. #define THREAD_H
  20. #if defined(_MSC_VER)
  21. #pragma once
  22. #endif
  23. #ifdef _UNIX
  24. #include "osdep.h"
  25. #endif
  26. #include "always.h"
  27. #include "vector.h"
  28. struct _EXCEPTION_POINTERS;
  29. // ****************************************************************************
  30. //
  31. // To create a new thread just derive a new class from this and define
  32. // Thread_Function. Creating the TheadClass object doesn't start the
  33. // thread. To start the thread you must call Execute().
  34. //
  35. // In your own thread remember to check for "running" flag of the base class.
  36. // If the flag is false you must exit the asap. Stop() is the function that
  37. // will clear the flag and expect you to exit from the thread. If you are
  38. // not exiting in certain time (defined as a parameter to Stop()) it will
  39. // force-kill the thread to prevent the program from halting.
  40. //
  41. // ****************************************************************************
  42. class ThreadClass
  43. {
  44. public:
  45. typedef int (*ExceptionHandlerType)(int exception_code, struct _EXCEPTION_POINTERS *e_info);
  46. ThreadClass(const char *name = NULL, ExceptionHandlerType exception_handler = NULL);
  47. virtual ~ThreadClass();
  48. // Execute Thread_Function(). Note that only one instance can be executed at a time.
  49. void Execute();
  50. // Thread priority 0 is normal, positive numbers are higher and normal and negative are lower.
  51. void Set_Priority(int priority);
  52. // Stop thread execution. Kill after ms milliseconds if not responding.
  53. void Stop(unsigned ms=3000);
  54. // Put current thread sleep for ms milliseconds (can be called from any thread, ThreadClass or other)
  55. static void Sleep_Ms(unsigned ms=0);
  56. // Put current thread in sleep and switch to next one (Useful for balansing the thread switches with game update)
  57. static void Switch_Thread();
  58. // Return calling thread's unique thread id
  59. static unsigned _Get_Current_Thread_ID();
  60. // Returns true if the thread is running.
  61. bool Is_Running();
  62. // Gets the name of the thread.
  63. const char *Get_Name(void) {return(ThreadName);};
  64. // Get info about a registered thread by it's index.
  65. static int Get_Thread_By_Index(int index, char *name_ptr = NULL);
  66. protected:
  67. // User defined thread function. The thread function should check for "running" flag every now and then
  68. // and exit the thread if running is false.
  69. virtual void Thread_Function() = 0;
  70. volatile bool running;
  71. // Name of thread.
  72. char ThreadName[64];
  73. // ID of thread.
  74. unsigned ThreadID;
  75. // Exception handler for this thread.
  76. ExceptionHandlerType ExceptionHandler;
  77. private:
  78. static void __cdecl Internal_Thread_Function(void*);
  79. volatile unsigned long handle;
  80. int thread_priority;
  81. };
  82. #endif