about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-10-20 17:02:00 +0200
committerbptato <nincsnevem662@gmail.com>2024-10-20 17:02:00 +0200
commitc968be128652ef58480bd807069316ccd5b262af (patch)
tree236942e3e3ca1b08045c3be6edaabafacd4084a7 /src
parentd29105fa71f315816ce03c1e5f89423b65b3b389 (diff)
downloadchawan-c968be128652ef58480bd807069316ccd5b262af.tar.gz
poll: nimify
until it's fixed upstream...
Diffstat (limited to 'src')
-rw-r--r--src/io/poll.nim30
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