Thread.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include <stdint.h>
  3. #include <vector>
  4. #include "il2cpp-config.h"
  5. #include "utils/NonCopyable.h"
  6. struct MethodInfo;
  7. struct Il2CppArray;
  8. struct Il2CppDomain;
  9. struct Il2CppObject;
  10. struct Il2CppThread;
  11. struct Il2CppInternalThread;
  12. struct Il2CppString;
  13. namespace il2cpp
  14. {
  15. namespace vm
  16. {
  17. // System.Threading.ThreadState
  18. enum ThreadState
  19. {
  20. kThreadStateRunning = 0x00000000,
  21. kThreadStateStopRequested = 0x00000001,
  22. kThreadStateSuspendRequested = 0x00000002,
  23. kThreadStateBackground = 0x00000004,
  24. kThreadStateUnstarted = 0x00000008,
  25. kThreadStateStopped = 0x00000010,
  26. kThreadStateWaitSleepJoin = 0x00000020,
  27. kThreadStateSuspended = 0x00000040,
  28. kThreadStateAbortRequested = 0x00000080,
  29. kThreadStateAborted = 0x00000100
  30. };
  31. // System.Threading.ApartmentState
  32. enum ThreadApartmentState
  33. {
  34. kThreadApartmentStateSTA = 0x00000000,
  35. kThreadApartmentStateMTA = 0x00000001,
  36. kThreadApartmentStateUnknown = 0x00000002
  37. };
  38. class LIBIL2CPP_CODEGEN_API Thread
  39. {
  40. public:
  41. static char *GetName(uint32_t *len);
  42. static void SetName(Il2CppThread* thread, Il2CppString* name);
  43. static void SetName(Il2CppInternalThread* thread, Il2CppString* name);
  44. static Il2CppThread* Current();
  45. static Il2CppThread* Attach(Il2CppDomain *domain);
  46. static void Detach(Il2CppThread *thread);
  47. static void WalkFrameStack(Il2CppThread *thread, Il2CppFrameWalkFunc func, void *user_data);
  48. static Il2CppThread** GetAllAttachedThreads(size_t &size);
  49. static void KillAllBackgroundThreadsAndWaitForForegroundThreads();
  50. static Il2CppThread* Main();
  51. static bool IsVmThread(Il2CppThread *thread);
  52. static void RequestInterrupt(Il2CppThread* thread);
  53. static void CheckCurrentThreadForInterruptAndThrowIfNecessary();
  54. static bool RequestAbort(Il2CppThread* thread);
  55. static void CheckCurrentThreadForAbortAndThrowIfNecessary();
  56. static void ResetAbort(Il2CppThread* thread);
  57. #if NET_4_0
  58. static bool RequestAbort(Il2CppInternalThread* thread);
  59. static void SetPriority(Il2CppThread* thread, int32_t priority);
  60. static int32_t GetPriority(Il2CppThread* thread);
  61. #endif
  62. struct NativeThreadAbortException {};
  63. public:
  64. // internal
  65. static void Initialize();
  66. static void UnInitialize();
  67. static void AdjustStaticData();
  68. static int32_t AllocThreadStaticData(int32_t size);
  69. static void FreeThreadStaticData(Il2CppThread *thread);
  70. static void* GetThreadStaticData(int32_t offset);
  71. static void Register(Il2CppThread *thread);
  72. static void Unregister(Il2CppThread *thread);
  73. static void Setup(Il2CppThread* thread);
  74. /// Initialize and register thread.
  75. /// NOTE: Must be called on thread!
  76. static void Initialize(Il2CppThread *thread, Il2CppDomain* domain);
  77. static void Uninitialize(Il2CppThread *thread);
  78. static void SetMain(Il2CppThread* thread);
  79. static void SetState(Il2CppThread *thread, ThreadState value);
  80. static ThreadState GetState(Il2CppThread *thread);
  81. static void ClrState(Il2CppThread* thread, ThreadState clr);
  82. static void FullMemoryBarrier();
  83. static int32_t GetNewManagedId();
  84. #if NET_4_0
  85. static Il2CppInternalThread* CurrentInternal();
  86. static void ClrState(Il2CppInternalThread* thread, ThreadState clr);
  87. static void SetState(Il2CppInternalThread *thread, ThreadState value);
  88. static ThreadState GetState(Il2CppInternalThread *thread);
  89. static bool TestState(Il2CppInternalThread* thread, ThreadState value);
  90. static Il2CppInternalThread* CreateInternal(void(*func)(void*), void* arg, bool threadpool_thread, uint32_t stack_size);
  91. static void Stop(Il2CppInternalThread* thread);
  92. static void Sleep(uint32_t ms);
  93. static bool YieldInternal();
  94. #endif
  95. private:
  96. static Il2CppThread* s_MainThread;
  97. };
  98. class ThreadStateSetter : il2cpp::utils::NonCopyable
  99. {
  100. public:
  101. ThreadStateSetter(ThreadState state) : m_State(state)
  102. {
  103. m_Thread = il2cpp::vm::Thread::Current();
  104. Thread::SetState(m_Thread, m_State);
  105. }
  106. ~ThreadStateSetter()
  107. {
  108. Thread::ClrState(m_Thread, m_State);
  109. }
  110. private:
  111. ThreadState m_State;
  112. Il2CppThread* m_Thread;
  113. };
  114. } /* namespace vm */
  115. } /* namespace il2cpp */