Skip to content

Commit

Permalink
Rename to wait_for_completion
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jun 17, 2024
1 parent 57ccd5c commit 2aef158
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/crystal/system/win32/iocp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module Crystal::IOCP
end

def wait_for_result(timeout, &)
IOCP.schedule_overlapped(timeout)
wait_for_completion(timeout)

raise Exception.new("Invalid state #{@state}") unless @state.done? || @state.started?
result = LibC.GetOverlappedResult(@handle, pointerof(@overlapped), out bytes, 0)
Expand All @@ -120,7 +120,7 @@ module Crystal::IOCP
end

def wait_for_wsa_result(timeout, &)
IOCP.schedule_overlapped(timeout)
wait_for_completion(timeout)

raise Exception.new("Invalid state #{@state}") unless @state.done? || @state.started?
flags = 0_u32
Expand Down Expand Up @@ -163,24 +163,23 @@ module Crystal::IOCP
def done!
@state = :done
end
end

# Returns `false` if the operation timed out.
def self.schedule_overlapped(timeout : Time::Span?, line = __LINE__) : Bool
if timeout
timeout_event = Crystal::IOCP::Event.new(Fiber.current)
timeout_event.add(timeout)
else
timeout_event = Crystal::IOCP::Event.new(Fiber.current, Time::Span::MAX)
end
# memoize event loop to make sure that we still target the same instance
# after wakeup (guaranteed by current MT model but let's be future proof)
event_loop = Crystal::EventLoop.current
event_loop.enqueue(timeout_event)
def wait_for_completion(timeout : Time::Span?)
if timeout
timeout_event = Crystal::IOCP::Event.new(Fiber.current)
timeout_event.add(timeout)
else
timeout_event = Crystal::IOCP::Event.new(Fiber.current, Time::Span::MAX)
end
# memoize event loop to make sure that we still target the same instance
# after wakeup (guaranteed by current MT model but let's be future proof)
event_loop = Crystal::EventLoop.current
event_loop.enqueue(timeout_event)

Fiber.suspend
Fiber.suspend

event_loop.dequeue(timeout_event)
event_loop.dequeue(timeout_event)
end
end

def self.overlapped_operation(target, handle, method, timeout, *, writing = false, &)
Expand Down

0 comments on commit 2aef158

Please sign in to comment.