FastReaderReaderWriterLockImpl.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #if IL2CPP_THREADS_PTHREAD && IL2CPP_SUPPORT_THREADS
  4. #include <pthread.h>
  5. #include "MutexImpl.h"
  6. namespace il2cpp
  7. {
  8. namespace os
  9. {
  10. // The pthread reader writer lock takes a lock to update its state on lock and unlock
  11. // So if you're read case time is on the order of taking a lock, then it's faster
  12. // to just use a non-recursive mutex
  13. class FastReaderReaderWriterLockImpl
  14. {
  15. public:
  16. FastReaderReaderWriterLockImpl()
  17. {
  18. pthread_mutex_init(&m_Mutex, NULL);
  19. }
  20. ~FastReaderReaderWriterLockImpl()
  21. {
  22. pthread_mutex_destroy(&m_Mutex);
  23. }
  24. void LockExclusive()
  25. {
  26. pthread_mutex_lock(&m_Mutex);
  27. }
  28. void LockShared()
  29. {
  30. pthread_mutex_lock(&m_Mutex);
  31. }
  32. void ReleaseExclusive()
  33. {
  34. pthread_mutex_unlock(&m_Mutex);
  35. }
  36. void ReleaseShared()
  37. {
  38. pthread_mutex_unlock(&m_Mutex);
  39. }
  40. pthread_mutex_t* GetOSHandle()
  41. {
  42. return &m_Mutex;
  43. }
  44. private:
  45. pthread_mutex_t m_Mutex;
  46. };
  47. }
  48. }
  49. #endif