No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThreadImpl.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. #if !IL2CPP_THREADS_STD && IL2CPP_THREADS_WIN32
  3. #include "os/ErrorCodes.h"
  4. #include "os/Thread.h"
  5. #include "os/WaitStatus.h"
  6. #include "utils/NonCopyable.h"
  7. #include "WindowsHeaders.h"
  8. #include "os/Generic/WaitObject.h"
  9. #include "Baselib.h"
  10. #include "Cpp/CappedSemaphore.h"
  11. #include "Cpp/Atomic.h"
  12. #define IL2CPP_DEFAULT_STACK_SIZE ( 1 * 1024 * 1024) // default .NET stacksize is 1mb
  13. namespace il2cpp
  14. {
  15. namespace os
  16. {
  17. class ThreadImpl : public il2cpp::utils::NonCopyable
  18. {
  19. public:
  20. ThreadImpl();
  21. ~ThreadImpl();
  22. static void AllocateStaticData();
  23. static void FreeStaticData();
  24. size_t Id();
  25. ErrorCode Run(Thread::StartFunc func, void* arg, int64_t affinityMask);
  26. void SetName(const char* name);
  27. void SetPriority(ThreadPriority priority);
  28. ThreadPriority GetPriority();
  29. void SetStackSize(size_t newsize)
  30. {
  31. // only makes sense if it's called BEFORE the thread has been created
  32. IL2CPP_ASSERT(m_ThreadHandle == NULL);
  33. // if newsize is zero we use the per-platform default value for size of stack
  34. if (newsize == 0)
  35. {
  36. newsize = IL2CPP_DEFAULT_STACK_SIZE;
  37. }
  38. m_StackSize = newsize;
  39. }
  40. void CheckForUserAPCAndHandle();
  41. void SetWaitObject(WaitObject* waitObject);
  42. void ReleaseSemaphore() {m_ConditionSemaphore.Release(1);}
  43. void AcquireSemaphore() {m_ConditionSemaphore.Acquire();}
  44. bool TryTimedAcquireSemaphore(uint32_t timeout) { return m_ConditionSemaphore.TryTimedAcquire(baselib::timeout_ms(timeout));}
  45. static int GetMaxStackSize();
  46. void QueueUserAPC(Thread::APCFunc func, void* context);
  47. ApartmentState GetApartment();
  48. ApartmentState GetExplicitApartment();
  49. ApartmentState SetApartment(ApartmentState state);
  50. void SetExplicitApartment(ApartmentState state);
  51. static void Sleep(uint32_t ms, bool interruptible);
  52. static size_t CurrentThreadId();
  53. static ThreadImpl* CreateForCurrentThread();
  54. static ThreadImpl* GetCurrentThread();
  55. static bool YieldInternal();
  56. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  57. static void SetNativeThreadCleanup(Thread::ThreadCleanupFunc cleanupFunction);
  58. static void RegisterCurrentThreadForCleanup(void* arg);
  59. static void UnregisterCurrentThreadForCleanup();
  60. static void OnCurrentThreadExiting();
  61. #endif
  62. baselib::atomic<WaitObject*> m_CurrentWaitObject;
  63. struct APCRequest
  64. {
  65. Thread::APCFunc callback;
  66. void* context;
  67. APCRequest(Thread::APCFunc callback, void* context) :
  68. callback(callback), context(context)
  69. {
  70. }
  71. };
  72. baselib::Lock m_PendingAPCsMutex;
  73. std::vector<APCRequest> m_PendingAPCs;
  74. baselib::CappedSemaphore m_ConditionSemaphore;
  75. private:
  76. HANDLE m_ThreadHandle;
  77. volatile DWORD m_ThreadId;
  78. SIZE_T m_StackSize;
  79. ApartmentState m_ApartmentState;
  80. ThreadPriority m_Priority;
  81. void SetNameForDebugger(const char* name);
  82. };
  83. }
  84. }
  85. #endif