Skip to content

Commit

Permalink
Fix: EventLoop::Polling::FiberEvent (#15301)
Browse files Browse the repository at this point in the history
Reduces discrepancies with the IOCP::FiberEvent and fixes a couple
issues:

1. No need to tie the event to a specific event loop;
2. Clear wake_at _after_ dequeueing the timer (MT bug).
  • Loading branch information
ysbaddaden authored Jan 7, 2025
1 parent 11cf206 commit 8cf8262
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/crystal/event_loop/polling.cr
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ abstract class Crystal::EventLoop::Polling < Crystal::EventLoop
# fiber interface, see Crystal::EventLoop

def create_resume_event(fiber : Fiber) : FiberEvent
FiberEvent.new(self, fiber, :sleep)
FiberEvent.new(:sleep, fiber)
end

def create_timeout_event(fiber : Fiber) : FiberEvent
FiberEvent.new(self, fiber, :select_timeout)
FiberEvent.new(:select_timeout, fiber)
end

# file descriptor interface, see Crystal::EventLoop::FileDescriptor
Expand Down
8 changes: 4 additions & 4 deletions src/crystal/event_loop/polling/fiber_event.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Crystal::EventLoop::Polling::FiberEvent
include Crystal::EventLoop::Event

def initialize(@event_loop : EventLoop, fiber : Fiber, type : Event::Type)
def initialize(type : Event::Type, fiber : Fiber)
@event = Event.new(type, fiber)
end

Expand All @@ -10,15 +10,15 @@ class Crystal::EventLoop::Polling::FiberEvent
seconds, nanoseconds = System::Time.monotonic
now = Time::Span.new(seconds: seconds, nanoseconds: nanoseconds)
@event.wake_at = now + timeout
@event_loop.add_timer(pointerof(@event))
EventLoop.current.add_timer(pointerof(@event))
end

# select timeout has been cancelled
def delete : Nil
return unless @event.wake_at?

@event.wake_at = nil
@event_loop.delete_timer(pointerof(@event))
EventLoop.current.delete_timer(pointerof(@event))
clear
end

# fiber died
Expand Down

0 comments on commit 8cf8262

Please sign in to comment.