Skip to content

Commit

Permalink
Drop Crystal::FiberChannel (#15245)
Browse files Browse the repository at this point in the history
Its main purpose was to keep the eventloop from exiting when empty,
which isn't needed anymore (evloop#run can now wait forever).

Since we need to grab the lock to send the fiber, we can just push it to
the runnables deque instead of passing it through a pipe; which didn't
work properly on Windows anyway.

The Windows IOCP eventloop will need a refactor to check on completions
even where there are no queued (timers).
  • Loading branch information
ysbaddaden authored Dec 16, 2024
1 parent 46459d6 commit 6437ab9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Crystal::EventLoop::FakeLoop < Crystal::EventLoop::Polling
private def system_run(blocking : Bool, & : Fiber ->) : Nil
end

private def interrupt : Nil
def interrupt : Nil
end

protected def system_add(fd : Int32, index : Arena::Index) : Nil
Expand Down
23 changes: 0 additions & 23 deletions src/crystal/fiber_channel.cr

This file was deleted.

28 changes: 10 additions & 18 deletions src/crystal/scheduler.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "crystal/event_loop"
require "crystal/system/print_error"
require "./fiber_channel"
require "fiber"
require "fiber/stack_pool"
require "crystal/system/thread"
Expand Down Expand Up @@ -97,10 +96,6 @@ class Crystal::Scheduler
{% end %}
end

{% if flag?(:preview_mt) %}
private getter(fiber_channel : Crystal::FiberChannel) { Crystal::FiberChannel.new }
{% end %}

@main : Fiber
@lock = Crystal::SpinLock.new
@sleeping = false
Expand Down Expand Up @@ -180,6 +175,7 @@ class Crystal::Scheduler
end

{% if flag?(:preview_mt) %}
private getter! worker_fiber : Fiber
@rr_target = 0

protected def find_target_thread
Expand All @@ -192,38 +188,34 @@ class Crystal::Scheduler
end

def run_loop
@worker_fiber = Fiber.current

spawn_stack_pool_collector

fiber_channel = self.fiber_channel
loop do
@lock.lock

if runnable = @runnables.shift?
@runnables << Fiber.current
@runnables << worker_fiber
@lock.unlock
resume(runnable)
else
@sleeping = true
@lock.unlock

Crystal.trace :sched, "mt:sleeping"
fiber = Crystal.trace(:sched, "mt:slept") { fiber_channel.receive }

@lock.lock
@sleeping = false
@runnables << Fiber.current
@lock.unlock
resume(fiber)
Crystal.trace(:sched, "mt:slept") { ::Fiber.suspend }
end
end
end

def send_fiber(fiber : Fiber)
@lock.lock
@runnables << fiber

if @sleeping
fiber_channel.send(fiber)
else
@runnables << fiber
@sleeping = false
@runnables << worker_fiber
@event_loop.interrupt
end
@lock.unlock
end
Expand Down

0 comments on commit 6437ab9

Please sign in to comment.