Thread.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #pragma once
  2. #include "BeefySysLib/Common.h"
  3. #include "BeefySysLib/util/CritSect.h"
  4. #include "BfObjects.h"
  5. #include "ThreadLocalStorage.h"
  6. #ifdef BF_HAS_TLS_DECLSPEC
  7. #define BF_THREAD_TLS
  8. #endif
  9. #pragma push_macro("MemoryBarrier")
  10. #undef MemoryBarrier
  11. class BfDbgInternalThread;
  12. namespace bf
  13. {
  14. namespace System
  15. {
  16. namespace Threading
  17. {
  18. struct ThreadHandle
  19. {
  20. };
  21. class Thread : public Object
  22. {
  23. private:
  24. Thread();
  25. public:
  26. BF_DECLARE_CLASS(Thread, Object);
  27. #ifdef BF_THREAD_TLS
  28. static BF_TLS_DECLSPEC Thread* sCurrentThread;
  29. #endif
  30. private:
  31. BfInternalThread* SetupInternalThread();
  32. BFRT_EXPORT void ManualThreadInit();
  33. BFRT_EXPORT int GetPriorityNative();
  34. BFRT_EXPORT void SetPriorityNative(int priority);
  35. BFRT_EXPORT void SetJoinOnDelete(bool joinOnDelete);
  36. BFRT_EXPORT bool GetIsAlive();
  37. BFRT_EXPORT bool GetIsThreadPoolThread();
  38. BFRT_EXPORT bool JoinInternal(int millisecondsTimeout);
  39. BFRT_EXPORT static void SleepInternal(int millisecondsTimeout);
  40. BFRT_EXPORT static void SpinWaitInternal(int iterations);
  41. BFRT_EXPORT static bool YieldInternal();
  42. BFRT_EXPORT static Thread* GetCurrentThreadNative();
  43. BFRT_EXPORT unsigned long GetProcessDefaultStackSize();
  44. BFRT_EXPORT void StartInternal();
  45. BFRT_EXPORT void ThreadStarted();
  46. BFRT_EXPORT void SetStackStart(void* ptr);
  47. BFRT_EXPORT void InternalFinalize();
  48. BFRT_EXPORT bool IsBackgroundNative();
  49. BFRT_EXPORT void SetBackgroundNative(bool isBackground);
  50. BFRT_EXPORT int GetThreadStateNative();
  51. BFRT_EXPORT void InformThreadNameChange(String* name);
  52. BFRT_EXPORT intptr GetThreadId();
  53. BFRT_EXPORT void Dbg_CreateInternal();
  54. public:
  55. BFRT_EXPORT void Suspend();
  56. BFRT_EXPORT void Resume();
  57. BFRT_EXPORT void Abort();
  58. BFRT_EXPORT static void RequestExitNotify();
  59. BFRT_EXPORT static void MemoryBarrier();
  60. static Thread* Alloc()
  61. {
  62. return BFRTCALLBACKS.Thread_Alloc();
  63. }
  64. BfInternalThread* GetInternalThread()
  65. {
  66. return BFRTCALLBACKS.Thread_GetInternalThread(this);
  67. }
  68. BfDbgInternalThread* Dbg_GetInternalThread()
  69. {
  70. return (BfDbgInternalThread*)BFRTCALLBACKS.Thread_GetInternalThread(this);
  71. }
  72. void SetInternalThread(BfInternalThread* internalThread)
  73. {
  74. BFRTCALLBACKS.Thread_SetInternalThread(this, internalThread);
  75. }
  76. int GetMaxStackSize()
  77. {
  78. return BFRTCALLBACKS.Thread_GetMaxStackSize(this);
  79. }
  80. };
  81. }
  82. } // Namespace System
  83. }
  84. class BfInternalThread
  85. {
  86. public:
  87. bool mIsSuspended;
  88. intptr mStackStart;
  89. bf::System::Threading::Thread* mThread;
  90. bool mRunning;
  91. bool mDone;
  92. bool mStarted;
  93. bool mJoinOnDelete;
  94. bool mIsManualInit;
  95. bool mIsBackground;
  96. BfpThread* mThreadHandle;
  97. intptr mThreadId;
  98. Beefy::CritSect mCritSect;
  99. Beefy::SyncEvent mStartedEvent;
  100. BfInternalThread()
  101. {
  102. mThread = NULL;
  103. mThreadHandle = 0;
  104. mStarted = false;
  105. mRunning = false;
  106. mDone = false;
  107. mIsSuspended = false;
  108. mJoinOnDelete = true;
  109. mIsManualInit = false;
  110. mIsBackground = false;
  111. mStackStart = 0;
  112. mThreadId = 0;
  113. }
  114. virtual ~BfInternalThread()
  115. {
  116. if (mThreadHandle != 0)
  117. {
  118. BfpThread_Release(mThreadHandle);
  119. }
  120. }
  121. virtual void ManualThreadInit(bf::System::Threading::Thread* thread)
  122. {
  123. bf::System::Threading::Thread* newThread = thread;
  124. mIsManualInit = true;
  125. mStarted = true;
  126. mStartedEvent.Set(true);
  127. mThread = newThread;
  128. newThread->SetInternalThread(this);
  129. mThreadId = BfpThread_GetCurrentId();
  130. mThreadHandle = BfpThread_GetCurrent();
  131. mStackStart = ((intptr)&newThread + 0xFFF) & ~(intptr)0xFFF;
  132. ThreadStarted();
  133. }
  134. virtual void ThreadStarted()
  135. {
  136. mRunning = true;
  137. }
  138. virtual void ThreadStopped()
  139. {
  140. //printf("TheadStopped\n");
  141. mRunning = false;
  142. }
  143. static void WaitForAllDone();
  144. };
  145. #pragma pop_macro("MemoryBarrier")