Handle.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "il2cpp-config.h"
  2. #include "os/Handle.h"
  3. #if IL2CPP_SUPPORT_THREADS
  4. #include <algorithm>
  5. #include "os/Thread.h"
  6. namespace il2cpp
  7. {
  8. namespace os
  9. {
  10. int32_t Handle::WaitAny(const std::vector<Handle*>& handles, int32_t ms)
  11. {
  12. int timeWaitedMs = 0;
  13. while (ms == -1 || timeWaitedMs <= ms)
  14. {
  15. int32_t numberOfOsHandles = (int32_t)handles.size();
  16. for (int32_t i = 0; i < numberOfOsHandles; ++i)
  17. {
  18. if (handles[i]->Wait(0U))
  19. return i;
  20. }
  21. os::Thread::Sleep(m_waitIntervalMs, true);
  22. timeWaitedMs += m_waitIntervalMs;
  23. }
  24. return 258; // WAIT_TIMEOUT value
  25. }
  26. bool Handle::WaitAll(std::vector<Handle*>& handles, int32_t ms)
  27. {
  28. int timeWaitedMs = 0;
  29. while (ms == -1 || timeWaitedMs <= ms)
  30. {
  31. size_t numberOfOsHandles = handles.size();
  32. std::vector<Handle*> signaledHandles;
  33. for (size_t i = 0; i < numberOfOsHandles; ++i)
  34. {
  35. if (handles[i]->Wait(0U))
  36. signaledHandles.push_back(handles[i]);
  37. }
  38. if (signaledHandles.size() == numberOfOsHandles)
  39. return true; // All handles have been signaled
  40. for (size_t i = 0; i < signaledHandles.size(); ++i)
  41. handles.erase(std::remove(handles.begin(), handles.end(), signaledHandles[i]), handles.end());
  42. os::Thread::Sleep(m_waitIntervalMs, true);
  43. timeWaitedMs += m_waitIntervalMs;
  44. }
  45. return false; // Timed out waiting for all handles to be signaled
  46. }
  47. } // namespace os
  48. } // naemspace il2cpp
  49. #else
  50. namespace il2cpp
  51. {
  52. namespace os
  53. {
  54. int32_t Handle::WaitAny(const std::vector<Handle*>& handles, int32_t ms)
  55. {
  56. IL2CPP_ASSERT(0 && "Threads are not enabled for this platform.");
  57. return 0;
  58. }
  59. bool Handle::WaitAll(std::vector<Handle*>& handles, int32_t ms)
  60. {
  61. IL2CPP_ASSERT(0 && "Threads are not enabled for this platform.");
  62. return false;
  63. }
  64. } // namespace os
  65. } // naemspace il2cpp
  66. #endif