Thread.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include "os/ErrorCodes.h"
  4. #include "os/Handle.h"
  5. #include "os/Event.h"
  6. #include "os/WaitStatus.h"
  7. #include "utils/NonCopyable.h"
  8. namespace il2cpp
  9. {
  10. namespace os
  11. {
  12. class ThreadImpl;
  13. enum ThreadPriority
  14. {
  15. kThreadPriorityLowest = 0,
  16. kThreadPriorityLow = 1,
  17. kThreadPriorityNormal = 2,
  18. kThreadPriorityHigh = 3,
  19. kThreadPriorityHighest = 4
  20. };
  21. enum ApartmentState
  22. {
  23. kApartmentStateInSTA = 0,
  24. kApartmentStateInMTA = 1,
  25. kApartmentStateUnknown = 2,
  26. kApartmentStateCoInitialized = 4,
  27. };
  28. class Thread : public il2cpp::utils::NonCopyable
  29. {
  30. public:
  31. Thread();
  32. ~Thread();
  33. typedef void (*StartFunc) (void* arg);
  34. // Use STDCALL calling convention on Windows, as it will be called back directly from the OS. This is defined as nothing on other platforms.
  35. typedef void (STDCALL * APCFunc)(void* context);
  36. typedef uint64_t ThreadId;
  37. typedef void (*CleanupFunc) (void* arg);
  38. /// Initialize/Shutdown thread subsystem. Must be called on main thread.
  39. static void Init();
  40. static void Shutdown();
  41. ErrorCode Run(StartFunc func, void* arg);
  42. ThreadId Id();
  43. /// Set thread name for debugging purposes. Won't do anything if not supported
  44. /// by platform.
  45. void SetName(const std::string& name);
  46. void SetPriority(ThreadPriority priority);
  47. ThreadPriority GetPriority();
  48. void SetStackSize(size_t stackSize);
  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 Thread* GetOrCreateCurrentThread();
  73. static void DetachCurrentThread();
  74. #if NET_4_0
  75. static bool YieldInternal();
  76. #endif
  77. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  78. typedef void (*ThreadCleanupFunc) (void* arg);
  79. static void SetNativeThreadCleanup(ThreadCleanupFunc cleanupFunction);
  80. static void RegisterCurrentThreadForCleanup(void* arg);
  81. static void UnregisterCurrentThreadForCleanup();
  82. void SignalExited();
  83. #endif
  84. static const uint64_t kInvalidThreadId = 0;
  85. private:
  86. enum ThreadState
  87. {
  88. kThreadCreated,
  89. kThreadRunning,
  90. kThreadWaiting,
  91. kThreadExited
  92. };
  93. ThreadState m_State;
  94. friend class ThreadImpl; // m_Thread
  95. ThreadImpl* m_Thread;
  96. /// Event that the thread signals when it finishes execution. Used for joins.
  97. /// Supports interruption.
  98. Event m_ThreadExitedEvent;
  99. CleanupFunc m_CleanupFunc;
  100. void* m_CleanupFuncArg;
  101. Thread(ThreadImpl* thread);
  102. static void RunWrapper(void* arg);
  103. };
  104. }
  105. }