Thread.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include "os/ErrorCodes.h"
  4. #include "os/Event.h"
  5. #include "os/WaitStatus.h"
  6. #include "utils/NonCopyable.h"
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. class ThreadImpl;
  12. enum ThreadPriority
  13. {
  14. kThreadPriorityLowest = 0,
  15. kThreadPriorityLow = 1,
  16. kThreadPriorityNormal = 2,
  17. kThreadPriorityHigh = 3,
  18. kThreadPriorityHighest = 4
  19. };
  20. enum ApartmentState
  21. {
  22. kApartmentStateInSTA = 0,
  23. kApartmentStateInMTA = 1,
  24. kApartmentStateUnknown = 2,
  25. kApartmentStateCoInitialized = 4,
  26. };
  27. class Thread : public il2cpp::utils::NonCopyable
  28. {
  29. public:
  30. Thread();
  31. ~Thread();
  32. typedef void (*StartFunc) (void* arg);
  33. // Use STDCALL calling convention on Windows, as it will be called back directly from the OS. This is defined as nothing on other platforms.
  34. typedef void (STDCALL * APCFunc)(void* context);
  35. typedef size_t ThreadId;
  36. typedef void (*CleanupFunc) (void* arg);
  37. /// Initialize/Shutdown thread subsystem. Must be called on main thread.
  38. static void Init();
  39. static void Shutdown();
  40. ErrorCode Run(StartFunc func, void* arg);
  41. ThreadId Id();
  42. /// Set thread name for debugging purposes. Won't do anything if not supported
  43. /// by platform.
  44. void SetName(const char* name);
  45. void SetPriority(ThreadPriority priority);
  46. ThreadPriority GetPriority();
  47. void SetStackSize(size_t stackSize);
  48. static int GetMaxStackSize();
  49. void SetCleanupFunction(CleanupFunc cleanupFunc, void* arg)
  50. {
  51. m_CleanupFunc = cleanupFunc;
  52. m_CleanupFuncArg = arg;
  53. }
  54. /// Interruptible, infinite wait join.
  55. WaitStatus Join();
  56. /// Interruptible, timed wait join.
  57. WaitStatus Join(uint32_t ms);
  58. /// Execute the given function on the thread the next time the thread executes
  59. /// an interruptible blocking operation.
  60. /// NOTE: The APC is allowed to raise exceptions!
  61. void QueueUserAPC(APCFunc func, void* context);
  62. // Explicit versions modify state without actually changing COM state.
  63. // Used to set thread state before it's started.
  64. ApartmentState GetApartment();
  65. ApartmentState GetExplicitApartment();
  66. ApartmentState SetApartment(ApartmentState state);
  67. void SetExplicitApartment(ApartmentState state);
  68. /// Interruptible, timed sleep.
  69. static void Sleep(uint32_t ms, bool interruptible = false);
  70. static ThreadId CurrentThreadId();
  71. static Thread* GetCurrentThread();
  72. static bool HasCurrentThread();
  73. static Thread* GetOrCreateCurrentThread();
  74. static void DetachCurrentThread();
  75. static bool YieldInternal();
  76. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  77. typedef void (*ThreadCleanupFunc) (void* arg);
  78. static void SetNativeThreadCleanup(ThreadCleanupFunc cleanupFunction);
  79. static void RegisterCurrentThreadForCleanup(void* arg);
  80. static void UnregisterCurrentThreadForCleanup();
  81. void SignalExited();
  82. #endif
  83. static const uint64_t kInvalidThreadId = 0;
  84. private:
  85. enum ThreadState
  86. {
  87. kThreadCreated,
  88. kThreadRunning,
  89. kThreadWaiting,
  90. kThreadExited
  91. };
  92. ThreadState m_State;
  93. friend class ThreadImpl; // m_Thread
  94. ThreadImpl* m_Thread;
  95. /// Event that the thread signals when it finishes execution. Used for joins.
  96. /// Supports interruption.
  97. Event m_ThreadExitedEvent;
  98. CleanupFunc m_CleanupFunc;
  99. void* m_CleanupFuncArg;
  100. Thread(ThreadImpl* thread);
  101. static void RunWrapper(void* arg);
  102. };
  103. }
  104. }