MutexImpl.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #if IL2CPP_THREADS_PTHREAD
  3. #include "os/ErrorCodes.h"
  4. #include "os/WaitStatus.h"
  5. #include "PosixWaitObject.h"
  6. #include <pthread.h>
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. class Thread;
  12. class MutexImpl : public posix::PosixWaitObject
  13. {
  14. public:
  15. MutexImpl();
  16. void Lock(bool interruptible);
  17. bool TryLock(uint32_t milliseconds, bool interruptible);
  18. void Unlock();
  19. private:
  20. /// Thread that currently owns the object. Used for recursion checks.
  21. Thread* m_OwningThread;
  22. /// Number of recursive locks on the owning thread.
  23. uint32_t m_RecursionCount;
  24. };
  25. class FastMutexImpl
  26. {
  27. public:
  28. FastMutexImpl()
  29. {
  30. pthread_mutexattr_t attr;
  31. pthread_mutexattr_init(&attr);
  32. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  33. pthread_mutex_init(&m_Mutex, &attr);
  34. pthread_mutexattr_destroy(&attr);
  35. }
  36. ~FastMutexImpl()
  37. {
  38. pthread_mutex_destroy(&m_Mutex);
  39. }
  40. void Lock()
  41. {
  42. pthread_mutex_lock(&m_Mutex);
  43. }
  44. void Unlock()
  45. {
  46. pthread_mutex_unlock(&m_Mutex);
  47. }
  48. pthread_mutex_t* GetOSHandle()
  49. {
  50. return &m_Mutex;
  51. }
  52. private:
  53. pthread_mutex_t m_Mutex;
  54. };
  55. }
  56. }
  57. #endif