Skip to content
Closed
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
23 changes: 0 additions & 23 deletions src/crystal/fiber_channel.cr

This file was deleted.

35 changes: 13 additions & 22 deletions src/crystal/scheduler.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "crystal/system/event_loop"
require "crystal/system/print_error"
require "./fiber_channel"
require "fiber"
require "fiber/stack_pool"
require "crystal/system/thread"
Expand Down Expand Up @@ -91,13 +90,8 @@ 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

# :nodoc:
def initialize(@thread : Thread)
Expand Down Expand Up @@ -175,6 +169,8 @@ class Crystal::Scheduler

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

protected def find_target_thread
if workers = @@workers
Expand All @@ -186,40 +182,35 @@ class Crystal::Scheduler
end

def run_loop
@worker_fiber = 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
@wait_count += 1
@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") { reschedule }
end
end
end

def send_fiber(fiber : Fiber)
@lock.lock
if @sleeping
fiber_channel.send(fiber)
else
@lock.sync do
@runnables << fiber
if @wait_count > 0
@wait_count -= 1
@runnables.unshift(@worker_fiber.not_nil!)
@event_loop.interrupt
end
end
@lock.unlock
end

def self.init : Nil
Expand Down