Mutex.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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) { m_Mutex->Lock(); }
  29. ~AutoLock() { m_Mutex->Unlock(); }
  30. private:
  31. Mutex* m_Mutex;
  32. };
  33. class MutexHandle : public Handle
  34. {
  35. public:
  36. MutexHandle(Mutex* mutex) : m_Mutex(mutex) {}
  37. virtual ~MutexHandle() { delete m_Mutex; }
  38. virtual bool Wait() { m_Mutex->Lock(true); return true; }
  39. virtual bool Wait(uint32_t ms) { return m_Mutex->TryLock(ms, true); }
  40. virtual WaitStatus Wait(bool interruptible) { m_Mutex->Lock(interruptible); return kWaitStatusSuccess; }
  41. virtual WaitStatus Wait(uint32_t ms, bool interruptible) { return m_Mutex->TryLock(ms, interruptible) ? kWaitStatusSuccess : kWaitStatusFailure; }
  42. virtual void Signal() { m_Mutex->Unlock(); }
  43. virtual void* GetOSHandle() { return m_Mutex->GetOSHandle(); }
  44. Mutex* Get() { return m_Mutex; }
  45. private:
  46. Mutex* m_Mutex;
  47. };
  48. /// Lightweight mutex that has no support for interruption or timed waits. Meant for
  49. /// internal use only.
  50. class FastMutex
  51. {
  52. public:
  53. FastMutex();
  54. ~FastMutex();
  55. void Lock();
  56. void Unlock();
  57. FastMutexImpl* GetImpl();
  58. private:
  59. FastMutexImpl* m_Impl;
  60. };
  61. struct FastAutoLockOld : public il2cpp::utils::NonCopyable
  62. {
  63. FastAutoLockOld(FastMutex* mutex)
  64. : m_Mutex(mutex)
  65. {
  66. m_Mutex->Lock();
  67. }
  68. ~FastAutoLockOld()
  69. {
  70. m_Mutex->Unlock();
  71. }
  72. private:
  73. FastMutex* m_Mutex;
  74. };
  75. struct FastAutoLock : public il2cpp::utils::NonCopyable
  76. {
  77. FastAutoLock(baselib::ReentrantLock* mutex)
  78. : m_Mutex(mutex)
  79. {
  80. m_Mutex->Acquire();
  81. }
  82. ~FastAutoLock()
  83. {
  84. m_Mutex->Release();
  85. }
  86. private:
  87. baselib::ReentrantLock* m_Mutex;
  88. };
  89. struct FastAutoUnlock : public il2cpp::utils::NonCopyable
  90. {
  91. FastAutoUnlock(baselib::ReentrantLock* mutex)
  92. : m_Mutex(mutex)
  93. {
  94. m_Mutex->Release();
  95. }
  96. ~FastAutoUnlock()
  97. {
  98. m_Mutex->Acquire();
  99. }
  100. private:
  101. baselib::ReentrantLock* m_Mutex;
  102. };
  103. }
  104. }