-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Epoll event loop (linux) #14814
Closed
ysbaddaden
wants to merge
20
commits into
crystal-lang:master
from
ysbaddaden:feature/epoll-event-loop
Closed
Epoll event loop (linux) #14814
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
265b4d3
Fix: simplify IO::FileDescriptor#finalize
ysbaddaden da4044e
Fix: don't cancel timeout select action event twice
ysbaddaden e89e680
Add :evloop to Crystal.trace
ysbaddaden 9172c4c
Epoll: initial attempt (doesn't compile)
ysbaddaden df41ba7
Fix: epoll_event is only packed on x86_64
ysbaddaden fc50413
Fix: disable EPOLLEXCLUSIVE for now
ysbaddaden 9a5053e
Fix: close in MT environment
ysbaddaden 5540b56
Fix: add optional Crystal::EventLoop#after_fork_before_exec (MT)
ysbaddaden bba4a62
Fix: after_fork (no MT) or after_fork_before_exec (MT only)
ysbaddaden 02f0f06
fixup! Fix: add optional Crystal::EventLoop#after_fork_before_exec (MT)
ysbaddaden d3458bd
Prefer eventfd over pipe (only one fd, smaller struct in kernel)
ysbaddaden 9969cf5
Save pointer to Node instead of fd (skips searches after wait)
ysbaddaden 4911214
fixup! Save pointer to Node instead of fd (skips searches after wait)
ysbaddaden 66046f3
fixup! Prefer eventfd over pipe (only one fd, smaller struct in kernel)
ysbaddaden 38f7224
Add Crystal::System::EventFD abstraction
ysbaddaden 75d2093
Use generic :system event type instead of :interrupt
ysbaddaden a36a214
fixup! Add Crystal::System::EventFD abstraction
ysbaddaden a5a68f2
Extract timers + cleanup + one timerfd per eventloop
ysbaddaden 4d4c068
Fix: also check that timers are empty (not only events)
ysbaddaden 0d36f67
Fix: missing mutex sync
ysbaddaden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{% skip_file unless flag?(:linux) || flag?(:solaris) %} | ||
|
||
require "c/sys/epoll" | ||
|
||
struct Crystal::System::Epoll | ||
def initialize | ||
@epfd = LibC.epoll_create1(LibC::EPOLL_CLOEXEC) | ||
raise RuntimeError.from_errno("epoll_create1") if @epfd == -1 | ||
end | ||
|
||
def add(fd : Int32, epoll_event : LibC::EpollEvent*) : Nil | ||
if LibC.epoll_ctl(@epfd, LibC::EPOLL_CTL_ADD, fd, epoll_event) == -1 | ||
raise RuntimeError.from_errno("epoll_ctl(EPOLL_CTL_ADD)") | ||
end | ||
end | ||
|
||
def modify(fd : Int32, epoll_event : LibC::EpollEvent*) : Nil | ||
if LibC.epoll_ctl(@epfd, LibC::EPOLL_CTL_MOD, fd, epoll_event) == -1 | ||
raise RuntimeError.from_errno("epoll_ctl(EPOLL_CTL_MOD)") | ||
end | ||
end | ||
|
||
def delete(fd : Int32) : Nil | ||
if LibC.epoll_ctl(@epfd, LibC::EPOLL_CTL_DEL, fd, nil) == -1 | ||
raise RuntimeError.from_errno("epoll_ctl(EPOLL_CTL_DEL)") | ||
end | ||
end | ||
|
||
# `timeout` is in milliseconds; -1 will wait indefinitely; 0 will never wait. | ||
def wait(events : Slice(LibC::EpollEvent), timeout : Int32) : Slice(LibC::EpollEvent) | ||
count = LibC.epoll_wait(@epfd, events.to_unsafe, events.size, timeout) | ||
raise RuntimeError.from_errno("epoll_wait") if count == -1 && Errno.value != Errno::EINTR | ||
events[0, count.clamp(0..)] | ||
end | ||
|
||
def close : Nil | ||
LibC.close(@epfd) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{% skip_file unless flag?(:linux) || flag?(:solaris) %} | ||
|
||
require "../epoll" | ||
require "../timerfd" | ||
|
||
struct Crystal::Epoll::Event | ||
enum Type | ||
IoRead | ||
IoWrite | ||
Sleep | ||
SelectTimeout | ||
System | ||
end | ||
|
||
getter fiber : Fiber | ||
getter type : Type | ||
getter fd : Int32 | ||
|
||
property! time : Time::Span? | ||
getter? timed_out : Bool = false | ||
|
||
include PointerLinkedList::Node | ||
|
||
# Allocates a system event into the HEAP. A system event doesn't have an | ||
# associated fiber. | ||
def self.system(fd : Int32) : self* | ||
event = Pointer(self).malloc(1) | ||
fiber = uninitialized Fiber | ||
event.value.initialize(fd, fiber, :system) | ||
event | ||
end | ||
|
||
def initialize(@fd : Int32, @fiber : Fiber, @type : Type, timeout : Time::Span? = nil) | ||
@time = ::Time.monotonic + timeout if timeout | ||
end | ||
|
||
def timed_out! : Bool | ||
@timed_out = true | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: IOCP uses
wake_at
and it's much better name thantime
!