PosixWaitObject.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #if IL2CPP_TARGET_POSIX
  3. #include <pthread.h>
  4. #include <limits.h>
  5. #include "utils/NonCopyable.h"
  6. #include "os/WaitStatus.h"
  7. #if (IL2CPP_USE_POSIX_COND_TIMEDWAIT_REL)
  8. int pthread_cond_timedwait_relative_np(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *spec);
  9. #endif
  10. namespace il2cpp
  11. {
  12. namespace os
  13. {
  14. class ThreadImpl;
  15. namespace posix
  16. {
  17. const uint32_t kNoTimeout = UINT_MAX;
  18. ////TODO: generalize this so that it can be used with c++11 condition variables
  19. /// Base class for all synchronization primitives when running on POSIX.
  20. ///
  21. /// To support interruption and timeouts for all synchronization primitives (events, mutexes, and
  22. /// semaphores) we implement these primitives ourselves instead of using their standard POSIX/Mach
  23. /// system counterparts. See PosixWaitObject.cpp for an explanation why.
  24. class PosixWaitObject : public il2cpp::utils::NonCopyable
  25. {
  26. public:
  27. ~PosixWaitObject();
  28. WaitStatus Wait(bool interruptible = false);
  29. WaitStatus Wait(uint32_t ms, bool interruptible = false);
  30. /// Cause an ongoing blocking wait on this object to exit and check for pending APCs.
  31. /// If the object is not currently being waited on, will cause the next wait to exit
  32. /// right away and check for APCs. After APCs have been handled, the object will go
  33. /// back to waiting except if the wait timeout has expired.
  34. void InterruptWait();
  35. static void LockWaitObjectDeletion();
  36. static void UnlockWaitObjectDeletion();
  37. protected:
  38. enum Type
  39. {
  40. kMutex, /// All mutexes are recursive.
  41. kManualResetEvent,
  42. kAutoResetEvent,
  43. kSemaphore
  44. };
  45. PosixWaitObject(Type type);
  46. Type m_Type;
  47. /// Always have to acquire this mutex to touch m_Count.
  48. pthread_mutex_t m_Mutex;
  49. /// Signal other threads of changes to m_Count.
  50. pthread_cond_t m_Condition;
  51. /// "Release" count for the primitive. Means different things depending on the type of primitive
  52. /// but for all primitives, we wait until this is zero. Semaphores are the only primitive for which
  53. /// this can go past 1.
  54. uint32_t m_Count;
  55. /// Number of threads waiting on this object. This is used to prevent unnecessary signals
  56. /// on m_Condition.
  57. uint32_t m_WaitingThreadCount;
  58. bool HaveWaitingThreads() const { return (m_WaitingThreadCount != 0); }
  59. };
  60. struct AutoLockWaitObjectDeletion
  61. {
  62. AutoLockWaitObjectDeletion() { PosixWaitObject::LockWaitObjectDeletion(); }
  63. ~AutoLockWaitObjectDeletion() { PosixWaitObject::UnlockWaitObjectDeletion(); }
  64. };
  65. }
  66. }
  67. }
  68. #endif // IL2CPP_TARGET_POSIX