diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-09-23 19:51:20 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-09-23 19:58:54 +0200 |
| commit | fcd9aa9f9c604ed5d104343542962a26b2acda62 (patch) | |
| tree | 7b0eea9b63bacc27cdc6471e2b2409b7b5d15c9e /src/io | |
| parent | 8e8c7f0911f4a20446a83090d722fecaf203f6f3 (diff) | |
| download | chawan-fcd9aa9f9c604ed5d104343542962a26b2acda62.tar.gz | |
Replace std/selectors with poll
std/selectors uses OS-specific selector APIs, which sounds good in
theory (faster than poll!), but sucks for portability in practice.
Sure, you can fix portability bugs, but who knows how many there are
on untested platforms... poll is standard, so if it works on one
computer it should work on all other ones. (I hope.)
As a bonus, I rewrote the timeout API for poll, which incidentally
fixes setTimeout across forks. Also, SIGWINCH should now work on all
platforms (as we self-pipe instead of signalfd/kqueue magic).
Diffstat (limited to 'src/io')
| -rw-r--r-- | src/io/bufreader.nim | 1 | ||||
| -rw-r--r-- | src/io/bufwriter.nim | 1 | ||||
| -rw-r--r-- | src/io/dynstream.nim | 9 | ||||
| -rw-r--r-- | src/io/poll.nim | 40 |
4 files changed, 49 insertions, 2 deletions
diff --git a/src/io/bufreader.nim b/src/io/bufreader.nim index 46f8e0f3..718e6d55 100644 --- a/src/io/bufreader.nim +++ b/src/io/bufreader.nim @@ -1,5 +1,4 @@ import std/options -import std/sets import std/tables import io/dynstream diff --git a/src/io/bufwriter.nim b/src/io/bufwriter.nim index 77b8ebd8..3eea01fa 100644 --- a/src/io/bufwriter.nim +++ b/src/io/bufwriter.nim @@ -2,7 +2,6 @@ # Each packet is prefixed with its length as a pointer-sized integer. import std/options -import std/sets import std/tables import io/dynstream diff --git a/src/io/dynstream.nim b/src/io/dynstream.nim index 66a2a932..b0b07140 100644 --- a/src/io/dynstream.nim +++ b/src/io/dynstream.nim @@ -251,6 +251,15 @@ proc dealloc*(mem: MaybeMappedMemory) = else: dealloc(mem.p0) +proc drain*(ps: PosixStream) = + assert not ps.blocking + var buffer {.noinit.}: array[4096, uint8] + try: + while true: + discard ps.recvData(addr buffer[0], buffer.len) + except ErrorAgain: + discard + type SocketStream* = ref object of PosixStream source*: Socket diff --git a/src/io/poll.nim b/src/io/poll.nim new file mode 100644 index 00000000..3c2c29a8 --- /dev/null +++ b/src/io/poll.nim @@ -0,0 +1,40 @@ +import std/posix + +type PollData* = object + fds: seq[TPollFd] + +iterator events*(ctx: PollData): TPollFd = + let L = ctx.fds.len + for i in 0 ..< L: + let event = ctx.fds[i] + if event.fd == -1 or ctx.fds[i].revents == 0: + continue + assert (event.revents and POLLNVAL) == 0 + yield event + +proc register*(ctx: var PollData; fd: int; events: cshort) = + if fd >= ctx.fds.len: + ctx.fds.setLen(fd + 1) + ctx.fds[fd].fd = cint(fd) + ctx.fds[fd].events = events + +proc register*(ctx: var PollData; fd: cint; events: cshort) = + ctx.register(int(fd), events) + +proc unregister*(ctx: var PollData; fd: int) = + ctx.fds[fd].fd = -1 + +proc trim(ctx: var PollData) = + var i = ctx.fds.high + while i >= 0: + if ctx.fds[i].fd != -1: + break + dec i + ctx.fds.setLen(i + 1) + +proc clear*(ctx: var PollData) = + ctx.fds.setLen(0) + +proc poll*(ctx: var PollData; timeout: cint) = + ctx.trim() + discard poll(addr ctx.fds[0], Tnfds(ctx.fds.len), cint(timeout)) |