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

Refactor the IOCP event loop (timers, ...) #15238

Merged
merged 16 commits into from
Dec 6, 2024
Merged
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions src/crystal/event_loop/iocp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ class Crystal::EventLoop::IOCP < Crystal::EventLoop
iocp
end

def run(blocking : Bool) : Bool
run_impl(blocking) do |fiber|
fiber.enqueue
end
end

# Runs the event loop and enqueues the fiber for the next upcoming event or
# completion.
def run(blocking : Bool) : Bool
private def run_impl(blocking : Bool, &) : Bool
# Pull the next upcoming event from the event queue. This determines the
# timeout for waiting on the completion port.
# OPTIMIZE: Implement @queue as a priority queue in order to avoid this
Expand Down Expand Up @@ -65,7 +71,7 @@ class Crystal::EventLoop::IOCP < Crystal::EventLoop
wait_time = blocking ? (next_event.wake_at - now).total_milliseconds : 0
timed_out = System::IOCP.wait_queued_completions(wait_time, alertable: blocking) do |fiber|
# This block may run multiple times. Every single fiber gets enqueued.
fiber.enqueue
yield fiber
end

@blocked_thread.set(nil, :release)
Expand Down Expand Up @@ -102,9 +108,9 @@ class Crystal::EventLoop::IOCP < Crystal::EventLoop
# was already activated.
if next_event.timeout? && (select_action = fiber.timeout_select_action)
fiber.timeout_select_action = nil
select_action.time_expired(fiber)
yield fiber if select_action.time_expired?
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
else
fiber.enqueue
yield fiber
end

# We enqueued a fiber.
Expand Down