Skip to content

Commit b92814a

Browse files
committed
Add woken waiters to the head of the queue
1 parent 5049dae commit b92814a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/crystal/fd_lock.cr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ struct Crystal::FdLock
103103
# acquire spinlock + forward declare pending waiter
104104
m, success = @m.compare_and_set(m, (m | xspin | xwait) & ~clear, :acquire, :relaxed)
105105
if success
106-
waiters.value.push(pointerof(waiter))
106+
# new waiters are added to the tail, while woken waiters that failed
107+
# to lock again are added to the head to give them some edge
108+
if (clear & xwaker) == 0_u32
109+
waiters.value.push(pointerof(waiter))
110+
else
111+
waiters.value.unshift(pointerof(waiter))
112+
end
107113

108114
# release spinlock before suspending the fiber
109115
@m.and(~xspin, :release)

0 commit comments

Comments
 (0)