1
0

thread.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "precomp_wasabi_bfc.h"
  2. #include "thread.h"
  3. #if !defined(WIN32) && !defined(LINUX)
  4. #error port me!
  5. #endif
  6. #pragma warning(push)
  7. #pragma warning(disable : 4229)
  8. THREADCALL Thread::ThreadProc(void *param) {
  9. Thread *th = static_cast<Thread*>(param);
  10. th->isrunning = true;
  11. int ret = th->threadProc();
  12. th->isrunning = false;
  13. return (THREADCALL)ret;
  14. }
  15. #pragma warning(pop)
  16. Thread::Thread(HANDLE _parent_handle) :
  17. isrunning(false),
  18. killswitch(0),
  19. handle(0), parent_handle(0), nicekill(1),
  20. auto_close_parent(FALSE)
  21. {
  22. setParentHandle(_parent_handle);
  23. #ifdef WIN32
  24. handle = CreateThread(NULL, 0, ThreadProc, (LPVOID)this, CREATE_SUSPENDED, &threadid);
  25. #endif
  26. }
  27. Thread::~Thread() {
  28. if (handle != 0) {
  29. if (isrunning) {
  30. setKillSwitch(TRUE);
  31. end();
  32. // FUCKO: might need to get rough here
  33. // FG> this serves no purpose, end() will not return unless the thread exits nicely
  34. #ifdef WIN32
  35. CloseHandle(handle);
  36. #elif defined(LINUX)
  37. pthread_kill(handle, SIGTERM);
  38. #endif
  39. }
  40. }
  41. setParentHandle(0);
  42. }
  43. int Thread::start() {
  44. if (running()) return 1;
  45. #ifdef WIN32
  46. return !(ResumeThread(handle) == 0xffffffff);
  47. #endif
  48. #ifdef LINUX
  49. pthread_create(&handle, NULL, ThreadProc, (LPVOID)this);
  50. return 1;
  51. #endif
  52. }
  53. bool Thread::running() {
  54. return isrunning;
  55. }
  56. int Thread::end() {
  57. while (running()) Sleep(66);
  58. return 1;
  59. }
  60. int Thread::kill() {
  61. #ifdef WIN32
  62. CloseHandle(handle);
  63. #elif defined(LINUX)
  64. pthread_kill(handle, SIGTERM);
  65. #endif
  66. handle = 0;
  67. isrunning = false;
  68. return 1;
  69. }
  70. void Thread::setKillSwitch(int k) {
  71. killswitch = k;
  72. }
  73. int Thread::getKillSwitch() {
  74. if (killswitch) return 1;
  75. if (parent_handle != 0 && !parentRunning()) return 1;
  76. return 0;
  77. }
  78. THREADID Thread::getThreadId() const {
  79. return threadid;
  80. }
  81. void Thread::setParentHandle(HANDLE _parent_handle, int _auto_close) {
  82. if (parent_handle != 0 && auto_close_parent) {
  83. #ifdef WIN32
  84. CloseHandle(parent_handle);
  85. #else
  86. //#error port me!
  87. DebugString("setParentHandle???\n");
  88. #endif
  89. }
  90. parent_handle = _parent_handle;
  91. auto_close_parent = _auto_close;
  92. }
  93. int Thread::parentRunning() {
  94. if (parent_handle == 0) return -1;
  95. #ifdef WIN32
  96. DWORD exitcode;
  97. int r = GetExitCodeThread(parent_handle, &exitcode);
  98. if (r == 0 || exitcode != STILL_ACTIVE) return 0;
  99. return 1;
  100. #else
  101. //#error port me!
  102. DebugString("parentRunning???\n");
  103. #endif
  104. }
  105. void Thread::setPriority(int priority) {
  106. Wasabi::Std::setThreadPriority(priority, handle);
  107. }