Thread.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #pragma once
  2. #include <stdint.h>
  3. #include <vector>
  4. #include <string>
  5. #include "il2cpp-config.h"
  6. #include "os/Thread.h"
  7. #include "utils/NonCopyable.h"
  8. struct MethodInfo;
  9. struct Il2CppArray;
  10. struct Il2CppDomain;
  11. struct Il2CppObject;
  12. struct Il2CppThread;
  13. struct Il2CppInternalThread;
  14. struct Il2CppString;
  15. namespace il2cpp
  16. {
  17. namespace vm
  18. {
  19. // System.Threading.ThreadState
  20. enum ThreadState
  21. {
  22. kThreadStateRunning = 0x00000000,
  23. kThreadStateStopRequested = 0x00000001,
  24. kThreadStateSuspendRequested = 0x00000002,
  25. kThreadStateBackground = 0x00000004,
  26. kThreadStateUnstarted = 0x00000008,
  27. kThreadStateStopped = 0x00000010,
  28. kThreadStateWaitSleepJoin = 0x00000020,
  29. kThreadStateSuspended = 0x00000040,
  30. kThreadStateAbortRequested = 0x00000080,
  31. kThreadStateAborted = 0x00000100,
  32. // This enum is used with the ~ operator to clear values. to avoid undefined
  33. // behavior in C++, the cleared state of each value should also be present
  34. // in the enum.
  35. kThreadStateRunningCleared = ~kThreadStateRunning,
  36. kThreadStateStopRequestedCleared = ~kThreadStateStopRequested,
  37. kThreadStateSuspendRequestedCleared = ~kThreadStateSuspendRequested,
  38. kThreadStateBackgroundCleared = ~kThreadStateBackground,
  39. kThreadStateUnstartedCleared = ~kThreadStateUnstarted,
  40. kThreadStateStoppedCleared = ~kThreadStateStopped,
  41. kThreadStateWaitSleepJoinCleared = ~kThreadStateWaitSleepJoin,
  42. kThreadStateSuspendedCleared = ~kThreadStateSuspended,
  43. kThreadStateAbortRequestedCleared = ~kThreadStateAbortRequested,
  44. kThreadStateAbortedCleared = ~kThreadStateAborted,
  45. };
  46. // System.Threading.ApartmentState
  47. enum ThreadApartmentState
  48. {
  49. kThreadApartmentStateSTA = 0x00000000,
  50. kThreadApartmentStateMTA = 0x00000001,
  51. kThreadApartmentStateUnknown = 0x00000002
  52. };
  53. class LIBIL2CPP_CODEGEN_API Thread
  54. {
  55. public:
  56. static std::string GetName(Il2CppInternalThread* thread);
  57. static void SetName(Il2CppThread* thread, Il2CppString* name);
  58. static void SetName(Il2CppInternalThread* thread, Il2CppString* name);
  59. static Il2CppThread* Current();
  60. static Il2CppThread* Attach(Il2CppDomain *domain);
  61. static void Detach(Il2CppThread *thread);
  62. static void WalkFrameStack(Il2CppThread *thread, Il2CppFrameWalkFunc func, void *user_data);
  63. static Il2CppThread** GetAllAttachedThreads(size_t &size);
  64. static void AbortAllThreads();
  65. static Il2CppThread* Main();
  66. static bool IsVmThread(Il2CppThread *thread);
  67. static uint64_t GetId(Il2CppThread *thread);
  68. static uint64_t GetId(Il2CppInternalThread* thread);
  69. static void RequestInterrupt(Il2CppThread* thread);
  70. static void CheckCurrentThreadForInterruptAndThrowIfNecessary();
  71. static bool RequestAbort(Il2CppThread* thread);
  72. static void CheckCurrentThreadForAbortAndThrowIfNecessary();
  73. static void ResetAbort(Il2CppThread* thread);
  74. static bool RequestAbort(Il2CppInternalThread* thread);
  75. static void ResetAbort(Il2CppInternalThread* thread);
  76. static void SetPriority(Il2CppThread* thread, int32_t priority);
  77. static int32_t GetPriority(Il2CppThread* thread);
  78. struct NativeThreadAbortException {};
  79. public:
  80. // internal
  81. static void Initialize();
  82. static void Uninitialize();
  83. static void AllocateStaticDataForCurrentThread();
  84. static int32_t AllocThreadStaticData(int32_t size);
  85. static void* GetThreadStaticData(int32_t offset);
  86. static void* GetThreadStaticDataForThread(int32_t offset, Il2CppInternalThread* thread);
  87. static void Register(Il2CppThread *thread);
  88. static void Unregister(Il2CppThread *thread);
  89. static void SetupInternalManagedThread(Il2CppThread* thread, os::Thread* osThread);
  90. /// Initialize and register thread.
  91. /// NOTE: Must be called on thread!
  92. static void InitializeManagedThread(Il2CppThread *thread, Il2CppDomain* domain);
  93. static void UninitializeManagedThread(Il2CppThread *thread);
  94. static void SetMain(Il2CppThread* thread);
  95. static void SetState(Il2CppThread *thread, ThreadState value);
  96. static ThreadState GetState(Il2CppThread *thread);
  97. static void ClrState(Il2CppThread* thread, ThreadState clr);
  98. static void FullMemoryBarrier();
  99. static int32_t GetNewManagedId();
  100. static Il2CppInternalThread* CurrentInternal();
  101. static void ClrState(Il2CppInternalThread* thread, ThreadState clr);
  102. static void SetState(Il2CppInternalThread *thread, ThreadState value);
  103. static ThreadState GetState(Il2CppInternalThread *thread);
  104. static bool TestState(Il2CppInternalThread* thread, ThreadState value);
  105. static Il2CppInternalThread* CreateInternal(void(*func)(void*), void* arg, bool threadpool_thread, uint32_t stack_size);
  106. static void Stop(Il2CppInternalThread* thread);
  107. static void Sleep(uint32_t ms);
  108. static bool YieldInternal();
  109. static void SetDefaultAffinityMask(int64_t affinityMask);
  110. static void Detach(Il2CppThread *thread, bool inNativeThreadCleanup);
  111. static void UninitializeManagedThread(Il2CppThread *thread, bool inNativeThreadCleanup);
  112. private:
  113. static Il2CppThread* s_MainThread;
  114. static void FreeCurrentThreadStaticData(Il2CppThread *thread, bool inNativeThreadCleanup);
  115. };
  116. class ThreadStateSetter : il2cpp::utils::NonCopyable
  117. {
  118. public:
  119. ThreadStateSetter(ThreadState state) : m_State(state)
  120. {
  121. m_Thread = il2cpp::vm::Thread::Current();
  122. Thread::SetState(m_Thread, m_State);
  123. }
  124. ~ThreadStateSetter()
  125. {
  126. Thread::ClrState(m_Thread, m_State);
  127. }
  128. private:
  129. ThreadState m_State;
  130. Il2CppThread* m_Thread;
  131. };
  132. } /* namespace vm */
  133. } /* namespace il2cpp */