ThreadImpl.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_THREADS_STD
  3. #include "os/Thread.h"
  4. #include "ThreadImpl.h"
  5. #include <thread>
  6. namespace il2cpp
  7. {
  8. namespace os
  9. {
  10. struct StartData
  11. {
  12. Thread::StartFunc m_StartFunc;
  13. void* m_StartArg;
  14. };
  15. static void ThreadStartWrapper(void* arg)
  16. {
  17. StartData* startData = (StartData*)arg;
  18. startData->m_StartFunc(startData->m_StartArg);
  19. free(startData);
  20. }
  21. uint64_t ThreadImpl::Id()
  22. {
  23. return m_Thread.get_id().hash();
  24. }
  25. ErrorCode ThreadImpl::Run(Thread::StartFunc func, void* arg, int64_t affinityMask)
  26. {
  27. StartData* startData = (StartData*)malloc(sizeof(StartData));
  28. startData->m_StartFunc = func;
  29. startData->m_StartArg = arg;
  30. std::thread t(ThreadStartWrapper, startData);
  31. if (affinityMask != Thread::kThreadAffinityAll)
  32. {
  33. IL2CPP_ASSERT(0 && "Using non-default thread affinity is not supported on the STD implementation.");
  34. }
  35. m_Thread.swap(t);
  36. return kErrorCodeSuccess;
  37. }
  38. WaitStatus ThreadImpl::Join(uint32_t ms)
  39. {
  40. m_Thread.join();
  41. return kWaitStatusSuccess;
  42. }
  43. ErrorCode ThreadImpl::Sleep(uint32_t milliseconds)
  44. {
  45. std::chrono::milliseconds dura(milliseconds);
  46. std::this_thread::sleep_for(dura);
  47. return kErrorCodeSuccess;
  48. }
  49. uint64_t ThreadImpl::CurrentThreadId()
  50. {
  51. return std::this_thread::get_id().hash();
  52. }
  53. }
  54. }
  55. #endif