Epoll Reference
Epoll is the Linux way of providing a scalable event queue.

Quick reference of important Epoll syscalls

epoll_create

Similarly to CreateCompletionPortand kqueue, the epoll_createsyscall creates an epoll instance and returns a file handle to us.
epoll_create(2) - Linux manual page

epoll_ctl

The epoll_ctlsyscall is used to add, modify or remove events from the event queue.
epoll_ctl(2) - Linux manual page

epoll_wait

The epoll_waitsyscall suspends (deschedules) the thread it's called from, thereby blocking any further progress on that thread until either an event has occurred or a provided timeouthas expired.
epoll_wait(2) - Linux manual page

eventfd

While not strictly a part of epollthis system call creates an eventfdobject that can be used as an event "notify" mechanism. In other words, this lets us issue an timeout event which wakes up the thread which is waiting after calling the epoll_waitsyscall. We are going to use this to be able to wake up our thread when we want to close the event queue and clean up after ourselves.
eventfd(2) - Linux manual page

close

The closesyscall closes a file descriptor so it no longer points to a resource and can be reused.
close(2) - Linux manual page

Useful structures

Event struct

1
#[repr(C)]
2
pub struct Event {
3
/// The events we want to register interest in represented by a bitflag.
4
events: u32,
5
// User defined variable (we use this to identify this exact event)
6
epoll_data: Data,
7
}
Copied!
The Eventstruct represents information about an event we want to register interest about. Specifically, what kind of events we want to register interest for like Reador Writeevents.

Data union

1
#[repr(C)]
2
pub union Data {
3
void: *const c_void,
4
fd: i32,
5
uint32: u32,
6
uint64: u64,
7
}
Copied!
This type might be new to you. It's in Rusts standard library but works like a C union. It does have a few similarities with Rusts enum in the sense that this type can represent either c_void, i32, u32 or an u64, but thats also where the similarities end. It's useful for us when we want to work with C APIs that expects a C-type union like we do here.

Flags and constants

We need some flags and constants to actually work with epoll. We only need very few since we're just covering one small use case, but these will at least get you started to get something working.
1
pub const EPOLL_CTL_ADD: i32 = 1;
2
pub const EPOLL_CTL_DEL: i32 = 2;
3
pub const EPOLLIN: i32 = 0x1;
4
pub const EPOLLONESHOT: i32 = 0x40000000;
Copied!
The flags are used by OR-ing different flags together. This is a pretty common way of describing a set of options in C. Read more about bitflags in the paragraph about them in the Appendix.
Last modified 1yr ago