thread.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ** Command & Conquer Generals(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. // ****************************************************************************
  28. //
  29. // To create a new thread just derive a new class from this and define
  30. // Thread_Function. Creating the TheadClass object doesn't start the
  31. // thread. To start the thread you must call Execute().
  32. //
  33. // In your own thread remember to check for "running" flag of the base class.
  34. // If the flag is false you must exit the asap. Stop() is the function that
  35. // will clear the flag and expect you to exit from the thread. If you are
  36. // not exiting in certain time (defined as a parameter to Stop()) it will
  37. // force-kill the thread to prevent the program from halting.
  38. //
  39. // ****************************************************************************
  40. class ThreadClass
  41. {
  42. public:
  43. ThreadClass();
  44. virtual ~ThreadClass();
  45. // Execute Thread_Function(). Note that only one instance can be executed at a time.
  46. void Execute();
  47. // Thread priority 0 is normal, positive numbers are higher and normal and negative are lower.
  48. void Set_Priority(int priority);
  49. // Stop thread execution. Kill after ms milliseconds if not responding.
  50. void Stop(unsigned ms=3000);
  51. // Put current thread sleep for ms milliseconds (can be called from any thread, ThreadClass or other)
  52. static void Sleep_Ms(unsigned ms=0);
  53. // Put current thread in sleep and switch to next one (Useful for balansing the thread switches with game update)
  54. static void Switch_Thread();
  55. // Return calling thread's unique thread id
  56. static unsigned _Get_Current_Thread_ID();
  57. // Returns true if the thread is running.
  58. bool Is_Running();
  59. protected:
  60. // User defined thread function. The thread function should check for "running" flag every now and then
  61. // and exit the thread if running is false.
  62. virtual void Thread_Function() = 0;
  63. volatile bool running;
  64. private:
  65. static void __cdecl Internal_Thread_Function(void*);
  66. volatile unsigned long handle;
  67. int thread_priority;
  68. };
  69. #endif