ConditionVariableImpl.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "os/c-api/il2cpp-config-platforms.h"
  2. #if IL2CPP_THREADS_PTHREAD && !RUNTIME_TINY
  3. #include "os/Posix/MutexImpl.h"
  4. #include "ConditionVariableImpl.h"
  5. #include <time.h>
  6. #include <sys/time.h>
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. ConditionVariableImpl::ConditionVariableImpl()
  12. {
  13. pthread_cond_init(&m_ConditionVariable, NULL);
  14. }
  15. ConditionVariableImpl::~ConditionVariableImpl()
  16. {
  17. pthread_cond_destroy(&m_ConditionVariable);
  18. }
  19. int ConditionVariableImpl::Wait(FastMutexImpl* lock)
  20. {
  21. return pthread_cond_wait(&m_ConditionVariable, lock->GetOSHandle());
  22. }
  23. int ConditionVariableImpl::TimedWait(FastMutexImpl* lock, uint32_t timeout_ms)
  24. {
  25. struct timeval tv;
  26. struct timespec ts;
  27. int64_t usecs;
  28. int res;
  29. if (timeout_ms == (uint32_t)0xFFFFFFFF)
  30. return pthread_cond_wait(&m_ConditionVariable, lock->GetOSHandle());
  31. /* ms = 10^-3, us = 10^-6, ns = 10^-9 */
  32. gettimeofday(&tv, NULL);
  33. tv.tv_sec += timeout_ms / 1000;
  34. usecs = tv.tv_usec + ((timeout_ms % 1000) * 1000);
  35. if (usecs >= 1000000)
  36. {
  37. usecs -= 1000000;
  38. tv.tv_sec++;
  39. }
  40. ts.tv_sec = tv.tv_sec;
  41. ts.tv_nsec = usecs * 1000;
  42. return pthread_cond_timedwait(&m_ConditionVariable, lock->GetOSHandle(), &ts);
  43. }
  44. void ConditionVariableImpl::Broadcast()
  45. {
  46. pthread_cond_broadcast(&m_ConditionVariable);
  47. }
  48. void ConditionVariableImpl::Signal()
  49. {
  50. pthread_cond_signal(&m_ConditionVariable);
  51. }
  52. }
  53. }
  54. #endif