ThreadImpl.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #define IL2CPP_DEFAULT_STACK_SIZE ( 1 * 1024 * 1024) // default .NET stacksize is 1mb
  9. namespace il2cpp
  10. {
  11. namespace os
  12. {
  13. class ThreadImpl : public il2cpp::utils::NonCopyable
  14. {
  15. public:
  16. ThreadImpl();
  17. ~ThreadImpl();
  18. size_t Id();
  19. ErrorCode Run(Thread::StartFunc func, void* arg, int64_t affinityMask);
  20. void SetName(const char* name);
  21. void SetPriority(ThreadPriority priority);
  22. ThreadPriority GetPriority();
  23. void SetStackSize(size_t newsize)
  24. {
  25. // only makes sense if it's called BEFORE the thread has been created
  26. IL2CPP_ASSERT(m_ThreadHandle == NULL);
  27. // if newsize is zero we use the per-platform default value for size of stack
  28. if (newsize == 0)
  29. {
  30. newsize = IL2CPP_DEFAULT_STACK_SIZE;
  31. }
  32. m_StackSize = newsize;
  33. }
  34. static int GetMaxStackSize();
  35. void QueueUserAPC(Thread::APCFunc func, void* context);
  36. ApartmentState GetApartment();
  37. ApartmentState GetExplicitApartment();
  38. ApartmentState SetApartment(ApartmentState state);
  39. void SetExplicitApartment(ApartmentState state);
  40. static void Sleep(uint32_t ms, bool interruptible);
  41. static size_t CurrentThreadId();
  42. static ThreadImpl* CreateForCurrentThread();
  43. static bool YieldInternal();
  44. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  45. static void SetNativeThreadCleanup(Thread::ThreadCleanupFunc cleanupFunction);
  46. static void RegisterCurrentThreadForCleanup(void* arg);
  47. static void UnregisterCurrentThreadForCleanup();
  48. static void OnCurrentThreadExiting();
  49. #endif
  50. private:
  51. HANDLE m_ThreadHandle;
  52. volatile DWORD m_ThreadId;
  53. SIZE_T m_StackSize;
  54. ApartmentState m_ApartmentState;
  55. ThreadPriority m_Priority;
  56. };
  57. }
  58. }
  59. #endif