primitives_internal.odin 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //+private
  2. package sync
  3. when #config(ODIN_SYNC_RECURSIVE_MUTEX_USE_FUTEX, true) {
  4. _Recursive_Mutex :: struct {
  5. owner: Futex,
  6. recursion: i32,
  7. }
  8. _recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
  9. tid := Futex(current_thread_id())
  10. for {
  11. prev_owner := atomic_compare_exchange_strong_explicit(&m.impl.owner, 0, tid, .Acquire, .Acquire)
  12. switch prev_owner {
  13. case 0, tid:
  14. m.impl.recursion += 1
  15. // inside the lock
  16. return
  17. }
  18. futex_wait(&m.impl.owner, u32(prev_owner))
  19. }
  20. }
  21. _recursive_mutex_unlock :: proc(m: ^Recursive_Mutex) {
  22. m.impl.recursion -= 1
  23. if m.impl.recursion != 0 {
  24. return
  25. }
  26. atomic_exchange_explicit(&m.impl.owner, 0, .Release)
  27. futex_signal(&m.impl.owner)
  28. // outside the lock
  29. }
  30. _recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
  31. tid := Futex(current_thread_id())
  32. prev_owner := atomic_compare_exchange_strong_explicit(&m.impl.owner, 0, tid, .Acquire, .Acquire)
  33. switch prev_owner {
  34. case 0, tid:
  35. m.impl.recursion += 1
  36. // inside the lock
  37. return true
  38. }
  39. return false
  40. }
  41. } else {
  42. _Recursive_Mutex :: struct {
  43. owner: int,
  44. recursion: int,
  45. mutex: Mutex,
  46. }
  47. _recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
  48. tid := current_thread_id()
  49. if tid != m.impl.owner {
  50. mutex_lock(&m.impl.mutex)
  51. }
  52. // inside the lock
  53. m.impl.owner = tid
  54. m.impl.recursion += 1
  55. }
  56. _recursive_mutex_unlock :: proc(m: ^Recursive_Mutex) {
  57. tid := current_thread_id()
  58. assert(tid == m.impl.owner)
  59. m.impl.recursion -= 1
  60. recursion := m.impl.recursion
  61. if recursion == 0 {
  62. m.impl.owner = 0
  63. }
  64. if recursion == 0 {
  65. mutex_unlock(&m.impl.mutex)
  66. }
  67. // outside the lock
  68. }
  69. _recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
  70. tid := current_thread_id()
  71. if m.impl.owner == tid {
  72. return mutex_try_lock(&m.impl.mutex)
  73. }
  74. if !mutex_try_lock(&m.impl.mutex) {
  75. return false
  76. }
  77. // inside the lock
  78. m.impl.owner = tid
  79. m.impl.recursion += 1
  80. return true
  81. }
  82. }
  83. when ODIN_OS != .Windows {
  84. _RW_Mutex :: struct {
  85. mutex: Atomic_RW_Mutex,
  86. }
  87. _rw_mutex_lock :: proc(rw: ^RW_Mutex) {
  88. atomic_rw_mutex_lock(&rw.impl.mutex)
  89. }
  90. _rw_mutex_unlock :: proc(rw: ^RW_Mutex) {
  91. atomic_rw_mutex_unlock(&rw.impl.mutex)
  92. }
  93. _rw_mutex_try_lock :: proc(rw: ^RW_Mutex) -> bool {
  94. return atomic_rw_mutex_try_lock(&rw.impl.mutex)
  95. }
  96. _rw_mutex_shared_lock :: proc(rw: ^RW_Mutex) {
  97. atomic_rw_mutex_shared_lock(&rw.impl.mutex)
  98. }
  99. _rw_mutex_shared_unlock :: proc(rw: ^RW_Mutex) {
  100. atomic_rw_mutex_shared_unlock(&rw.impl.mutex)
  101. }
  102. _rw_mutex_try_shared_lock :: proc(rw: ^RW_Mutex) -> bool {
  103. return atomic_rw_mutex_try_shared_lock(&rw.impl.mutex)
  104. }
  105. }