Improve spin lock for atomic_mutex_lock

This commit is contained in:
gingerBill
2022-03-30 16:15:48 +01:00
parent 3a4630e6b4
commit 561b725b0e
+4 -5
View File
@@ -38,6 +38,9 @@ atomic_mutex_lock :: proc(m: ^Atomic_Mutex) {
} }
} }
// Set just in case 100 iterations did not do it
new_state = .Waiting
for { for {
if atomic_exchange_acquire(&m.state, .Waiting) == .Unlocked { if atomic_exchange_acquire(&m.state, .Waiting) == .Unlocked {
return return
@@ -49,11 +52,7 @@ atomic_mutex_lock :: proc(m: ^Atomic_Mutex) {
} }
switch v := atomic_exchange_acquire(&m.state, .Locked); v { if v := atomic_exchange_acquire(&m.state, .Locked); v != .Unlocked {
case .Unlocked:
// Okay
case: fallthrough
case .Locked, .Waiting:
lock_slow(m, v) lock_slow(m, v)
} }
} }