Mutex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include "os/ErrorCodes.h"
  3. #include "os/Handle.h"
  4. #include "os/WaitStatus.h"
  5. #include "utils/NonCopyable.h"
  6. #include "Baselib.h"
  7. #include "Cpp/ReentrantLock.h"
  8. namespace il2cpp
  9. {
  10. namespace os
  11. {
  12. class MutexImpl;
  13. class FastMutexImpl;
  14. class Mutex : public il2cpp::utils::NonCopyable
  15. {
  16. public:
  17. Mutex(bool initiallyOwned = false);
  18. ~Mutex();
  19. void Lock(bool interruptible = false);
  20. bool TryLock(uint32_t milliseconds = 0, bool interruptible = false);
  21. void Unlock();
  22. void* GetOSHandle();
  23. private:
  24. MutexImpl* m_Mutex;
  25. };
  26. struct AutoLock : public il2cpp::utils::NonCopyable
  27. {
  28. AutoLock(Mutex* mutex) : m_Mutex(mutex)
  29. {
  30. #if !IL2CPP_SLIM_CLASS
  31. m_Mutex->Lock();
  32. #endif
  33. }
  34. #if !IL2CPP_SLIM_CLASS
  35. ~AutoLock() { m_Mutex->Unlock(); }
  36. #endif
  37. private:
  38. Mutex* m_Mutex;
  39. };
  40. class MutexHandle : public Handle
  41. {
  42. public:
  43. MutexHandle(Mutex* mutex) : m_Mutex(mutex) {}
  44. virtual ~MutexHandle() { delete m_Mutex; }
  45. virtual bool Wait() { m_Mutex->Lock(true); return true; }
  46. virtual bool Wait(uint32_t ms) { return m_Mutex->TryLock(ms, true); }
  47. virtual WaitStatus Wait(bool interruptible) { m_Mutex->Lock(interruptible); return kWaitStatusSuccess; }
  48. virtual WaitStatus Wait(uint32_t ms, bool interruptible) { return m_Mutex->TryLock(ms, interruptible) ? kWaitStatusSuccess : kWaitStatusFailure; }
  49. virtual void Signal() { m_Mutex->Unlock(); }
  50. virtual void* GetOSHandle() { return m_Mutex->GetOSHandle(); }
  51. Mutex* Get() { return m_Mutex; }
  52. private:
  53. Mutex* m_Mutex;
  54. };
  55. /// Lightweight mutex that has no support for interruption or timed waits. Meant for
  56. /// internal use only.
  57. class FastMutex
  58. {
  59. public:
  60. FastMutex();
  61. ~FastMutex();
  62. void Lock();
  63. void Unlock();
  64. FastMutexImpl* GetImpl();
  65. private:
  66. FastMutexImpl* m_Impl;
  67. };
  68. struct FastAutoLock : public il2cpp::utils::NonCopyable
  69. {
  70. FastAutoLock(baselib::ReentrantLock* mutex)
  71. : m_Mutex(mutex)
  72. {
  73. #if !IL2CPP_SLIM_CLASS
  74. m_Mutex->Acquire();
  75. #endif
  76. }
  77. #if !IL2CPP_SLIM_CLASS
  78. ~FastAutoLock()
  79. {
  80. m_Mutex->Release();
  81. }
  82. #endif
  83. #if IL2CPP_DEBUG
  84. bool IsLock(baselib::ReentrantLock* mutex) const
  85. {
  86. return mutex == m_Mutex;
  87. }
  88. #endif
  89. private:
  90. baselib::ReentrantLock* m_Mutex;
  91. };
  92. struct FastAutoUnlock : public il2cpp::utils::NonCopyable
  93. {
  94. FastAutoUnlock(baselib::ReentrantLock* mutex)
  95. : m_Mutex(mutex)
  96. {
  97. #if !IL2CPP_SLIM_CLASS
  98. m_Mutex->Release();
  99. #endif
  100. }
  101. #if !IL2CPP_SLIM_CLASS
  102. ~FastAutoUnlock()
  103. {
  104. m_Mutex->Acquire();
  105. }
  106. #endif
  107. private:
  108. baselib::ReentrantLock* m_Mutex;
  109. };
  110. }
  111. }