Mutex.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 FastAutoLock : public il2cpp::utils::NonCopyable
  62. {
  63. FastAutoLock(baselib::ReentrantLock* mutex)
  64. : m_Mutex(mutex)
  65. {
  66. m_Mutex->Acquire();
  67. }
  68. ~FastAutoLock()
  69. {
  70. m_Mutex->Release();
  71. }
  72. #if IL2CPP_DEBUG
  73. bool IsLock(baselib::ReentrantLock* mutex) const
  74. {
  75. return mutex == m_Mutex;
  76. }
  77. #endif
  78. private:
  79. baselib::ReentrantLock* m_Mutex;
  80. };
  81. struct FastAutoUnlock : public il2cpp::utils::NonCopyable
  82. {
  83. FastAutoUnlock(baselib::ReentrantLock* mutex)
  84. : m_Mutex(mutex)
  85. {
  86. m_Mutex->Release();
  87. }
  88. ~FastAutoUnlock()
  89. {
  90. m_Mutex->Acquire();
  91. }
  92. private:
  93. baselib::ReentrantLock* m_Mutex;
  94. };
  95. }
  96. }