ThreadImpl.h 2.0 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. uint64_t Id();
  19. ErrorCode Run(Thread::StartFunc func, void* arg);
  20. void SetName(const std::string& 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. void QueueUserAPC(Thread::APCFunc func, void* context);
  35. ApartmentState GetApartment();
  36. ApartmentState GetExplicitApartment();
  37. ApartmentState SetApartment(ApartmentState state);
  38. void SetExplicitApartment(ApartmentState state);
  39. static void Sleep(uint32_t ms, bool interruptible);
  40. static uint64_t CurrentThreadId();
  41. static ThreadImpl* CreateForCurrentThread();
  42. #if NET_4_0
  43. static bool YieldInternal();
  44. #endif
  45. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  46. static void SetNativeThreadCleanup(Thread::ThreadCleanupFunc cleanupFunction);
  47. static void RegisterCurrentThreadForCleanup(void* arg);
  48. static void UnregisterCurrentThreadForCleanup();
  49. static void OnCurrentThreadExiting();
  50. #endif
  51. private:
  52. HANDLE m_ThreadHandle;
  53. volatile DWORD m_ThreadId;
  54. SIZE_T m_StackSize;
  55. ApartmentState m_ApartmentState;
  56. ThreadPriority m_Priority;
  57. };
  58. }
  59. }
  60. #endif