diff options
author | bptato <nincsnevem662@gmail.com> | 2024-10-20 17:02:00 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-10-20 17:02:00 +0200 |
commit | c968be128652ef58480bd807069316ccd5b262af (patch) | |
tree | 236942e3e3ca1b08045c3be6edaabafacd4084a7 /src | |
parent | d29105fa71f315816ce03c1e5f89423b65b3b389 (diff) | |
download | chawan-c968be128652ef58480bd807069316ccd5b262af.tar.gz |
poll: nimify
until it's fixed upstream...
Diffstat (limited to 'src')
-rw-r--r-- | src/io/poll.nim | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/src/io/poll.nim b/src/io/poll.nim index 5e115a11..209218cd 100644 --- a/src/io/poll.nim +++ b/src/io/poll.nim @@ -1,9 +1,27 @@ -import std/posix +from std/posix import TPollfd, POLLNVAL + +# NB: nfds_t on SVR4 this was unsigned long, but BSDs use unsigned int. +# Linux and Haiku emulate the former, BSDs inherit the latter. +# Since there are less SVR4 imitators than BSD descendants, I'll just +# hardcode the former. +when defined(linux) or defined(haiku): + type nfds_t {.importc, header: "<poll.h>".} = culong +else: + type nfds_t {.importc, header: "<poll.h>".} = cuint + +const sizeofNfdsT = sizeof(nfds_t) +{.emit: """ +NIM_STATIC_ASSERT(`sizeofNfdsT` == sizeof(nfds_t), + "nfds_t size mismatch; please report at https://todo.sr.ht/~bptato/chawan"); +""".} + +proc poll(fds: ptr TPollfd; nfds: nfds_t; timeout: cint): cint + {.cdecl, importc, header: "<poll.h>".} type PollData* = object - fds: seq[TPollFd] + fds: seq[TPollfd] -iterator events*(ctx: PollData): TPollFd = +iterator events*(ctx: PollData): TPollfd = let L = ctx.fds.len for i in 0 ..< L: let event = ctx.fds[i] @@ -41,11 +59,7 @@ proc clear*(ctx: var PollData) = proc poll*(ctx: var PollData; timeout: cint) = ctx.trim() let fds = addr ctx.fds[0] - let nfds = cint(ctx.fds.len) - var res: cint - {.emit: """ - `res` = (int)poll(`fds`, `nfds`, `timeout`); - """.} + let res = poll(fds, nfds_t(ctx.fds.len), timeout) if res < 0: # error for event in ctx.fds.mitems: event.revents = 0 |