Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Crystal::PointerLinkedList instead of Deque in Mutex #15330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/fiber/waiting.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Fiber
# :nodoc:
struct Waiting
include Crystal::PointerLinkedList::Node

def initialize(@fiber : Fiber)
end

def enqueue : Nil
@fiber.enqueue
end
end
end
20 changes: 12 additions & 8 deletions src/mutex.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "fiber/waiting"
require "crystal/spin_lock"

# A fiber-safe mutex.
Expand All @@ -22,9 +23,9 @@ class Mutex
@state = Atomic(Int32).new(UNLOCKED)
@mutex_fiber : Fiber?
@lock_count = 0
@queue = Deque(Fiber).new
@queue = Crystal::PointerLinkedList(Fiber::Waiting).new
@queue_count = Atomic(Int32).new(0)
@lock = Crystal::SpinLock.new
@spin = Crystal::SpinLock.new

enum Protection
Checked
Expand Down Expand Up @@ -59,7 +60,9 @@ class Mutex
loop do
break if try_lock

@lock.sync do
waiting = Fiber::Waiting.new(Fiber.current)

@spin.sync do
@queue_count.add(1)

if @state.get(:relaxed) == UNLOCKED
Expand All @@ -71,7 +74,7 @@ class Mutex
end
end

@queue.push Fiber.current
@queue.push pointerof(waiting)
end

Fiber.suspend
Expand Down Expand Up @@ -116,17 +119,18 @@ class Mutex
return
end

fiber = nil
@lock.sync do
waiting = nil
@spin.sync do
if @queue_count.get == 0
return
end

if fiber = @queue.shift?
if waiting = @queue.shift?
@queue_count.add(-1)
end
end
fiber.enqueue if fiber

waiting.try(&.value.enqueue)
end

def synchronize(&)
Expand Down
16 changes: 3 additions & 13 deletions src/wait_group.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "fiber"
require "fiber/waiting"
require "crystal/spin_lock"
require "crystal/pointer_linked_list"

Expand Down Expand Up @@ -31,17 +32,6 @@ require "crystal/pointer_linked_list"
# wg.wait
# ```
class WaitGroup
private struct Waiting
include Crystal::PointerLinkedList::Node

def initialize(@fiber : Fiber)
end

def enqueue : Nil
@fiber.enqueue
end
end

# Yields a `WaitGroup` instance and waits at the end of the block for all of
# the work enqueued inside it to complete.
#
Expand All @@ -59,7 +49,7 @@ class WaitGroup
end

def initialize(n : Int32 = 0)
@waiting = Crystal::PointerLinkedList(Waiting).new
@waiting = Crystal::PointerLinkedList(Fiber::Waiting).new
@lock = Crystal::SpinLock.new
@counter = Atomic(Int32).new(n)
end
Expand Down Expand Up @@ -128,7 +118,7 @@ class WaitGroup
def wait : Nil
return if done?

waiting = Waiting.new(Fiber.current)
waiting = Fiber::Waiting.new(Fiber.current)

@lock.sync do
# must check again to avoid a race condition where #done may have
Expand Down
Loading